@@ -162,21 +162,53 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
162162 }
163163
164164 TestKind :: SwitchInt { switch_ty, ref options, indices : _ } => {
165- let otherwise = self . cfg . start_new_block ( ) ;
166- let targets: Vec < _ > =
167- options. iter ( )
168- . map ( |_| self . cfg . start_new_block ( ) )
169- . chain ( Some ( otherwise) )
170- . collect ( ) ;
165+ let ( targets, term) = match switch_ty. sty {
166+ // If we're matching on boolean we can
167+ // use the If TerminatorKind instead
168+ ty:: TyBool => {
169+ assert ! ( options. len( ) > 0 && options. len( ) <= 2 ) ;
170+
171+ let ( true_bb, else_bb) =
172+ ( self . cfg . start_new_block ( ) ,
173+ self . cfg . start_new_block ( ) ) ;
174+
175+ let targets = match & options[ 0 ] {
176+ & ConstVal :: Bool ( true ) => vec ! [ true_bb, else_bb] ,
177+ & ConstVal :: Bool ( false ) => vec ! [ else_bb, true_bb] ,
178+ v => span_bug ! ( test. span, "expected boolean value but got {:?}" , v)
179+ } ;
180+
181+ ( targets,
182+ TerminatorKind :: If {
183+ cond : Operand :: Consume ( lvalue. clone ( ) ) ,
184+ targets : ( true_bb, else_bb)
185+ } )
186+
187+ }
188+ _ => {
189+ // The switch may be inexhaustive so we
190+ // add a catch all block
191+ let otherwise = self . cfg . start_new_block ( ) ;
192+ let targets: Vec < _ > =
193+ options. iter ( )
194+ . map ( |_| self . cfg . start_new_block ( ) )
195+ . chain ( Some ( otherwise) )
196+ . collect ( ) ;
197+
198+ ( targets. clone ( ) ,
199+ TerminatorKind :: SwitchInt {
200+ discr : lvalue. clone ( ) ,
201+ switch_ty : switch_ty,
202+ values : options. clone ( ) ,
203+ targets : targets
204+ } )
205+ }
206+ } ;
207+
171208 self . cfg . terminate ( block,
172209 scope_id,
173210 test. span ,
174- TerminatorKind :: SwitchInt {
175- discr : lvalue. clone ( ) ,
176- switch_ty : switch_ty,
177- values : options. clone ( ) ,
178- targets : targets. clone ( ) ,
179- } ) ;
211+ term) ;
180212 targets
181213 }
182214
0 commit comments