Skip to content

Commit 087d241

Browse files
committed
[const-prop] Support propagating into SwitchInt's discr Operand
1 parent eea75dc commit 087d241

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/librustc_mir/transform/const_prop.rs

+7
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,13 @@ impl<'b, 'a, 'tcx> MutVisitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
725725
}
726726
}
727727
},
728+
TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
729+
if let Some(value) = self.eval_operand(&discr, source_info) {
730+
if let ScalarMaybeUndef::Scalar(scalar) = self.ecx.read_scalar(value).unwrap() {
731+
*discr = self.operand_from_scalar(scalar, switch_ty);
732+
}
733+
}
734+
},
728735
_ => {}
729736
}
730737
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fn main() {
2+
match 1 {
3+
1 => std::process::exit(0),
4+
_ => std::process::exit(-1),
5+
}
6+
}
7+
8+
// END RUST SOURCE
9+
// START rustc.main.ConstProp.before.mir
10+
// bb0: {
11+
// ...
12+
// _1 = const 1i32;
13+
// switchInt(_1) -> [1i32: bb1, otherwise: bb2];
14+
// }
15+
// END rustc.main.ConstProp.before.mir
16+
// START rustc.main.ConstProp.after.mir
17+
// bb0: {
18+
// ...
19+
// switchInt(const 1i32) -> [1i32: bb1, otherwise: bb2];
20+
// }
21+
// END rustc.main.ConstProp.after.mir

0 commit comments

Comments
 (0)