@@ -52,11 +52,8 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
5252 if let Some ( f_c) = f_c. literal . try_eval_bool ( tcx, param_env) {
5353 // This should also be a bool because it's writing to the same place
5454 let s_c = s_c. literal . try_eval_bool ( tcx, param_env) . unwrap ( ) ;
55- // Check that only const assignments of opposite bool values are
56- // permitted.
57- if f_c != s_c {
58- continue
59- }
55+ assert_ne ! ( f_c, s_c, "Unexpected match would've compared eq earlier" ) ;
56+ continue ;
6057 }
6158 continue ' outer;
6259 }
@@ -70,14 +67,19 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
7067 bbs[ bb_idx] . terminator_mut ( ) . kind = TerminatorKind :: Goto { target : first } ;
7168 for s in bbs[ first] . statements . iter_mut ( ) {
7269 if let StatementKind :: Assign ( box ( _, ref mut rhs) ) = s. kind {
73- let size = tcx. layout_of ( param_env. and ( switch_ty) ) . unwrap ( ) . size ;
74- let const_cmp = Operand :: const_from_scalar (
75- tcx,
76- switch_ty,
77- crate :: interpret:: Scalar :: from_uint ( val, size) ,
78- rustc_span:: DUMMY_SP ,
79- ) ;
80- * rhs = Rvalue :: BinaryOp ( BinOp :: Eq , Operand :: Move ( discr) , const_cmp) ;
70+ if let Rvalue :: Use ( Operand :: Constant ( c) ) = rhs {
71+ let size = tcx. layout_of ( param_env. and ( switch_ty) ) . unwrap ( ) . size ;
72+ let const_cmp = Operand :: const_from_scalar (
73+ tcx,
74+ switch_ty,
75+ crate :: interpret:: Scalar :: from_uint ( val, size) ,
76+ rustc_span:: DUMMY_SP ,
77+ ) ;
78+ if let Some ( c) = c. literal . try_eval_bool ( tcx, param_env) {
79+ let op = if c { BinOp :: Eq } else { BinOp :: Ne } ;
80+ * rhs = Rvalue :: BinaryOp ( op, Operand :: Move ( discr) , const_cmp) ;
81+ }
82+ }
8183 }
8284 }
8385 }
0 commit comments