Skip to content

Commit ea78010

Browse files
committed
[const-prop] Handle MIR Rvalue::Discriminant
1 parent 85f2945 commit ea78010

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/librustc_mir/transform/const_prop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -461,13 +461,13 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
461461

462462
// if this isn't a supported operation, then return None
463463
match rvalue {
464-
Rvalue::NullaryOp(NullOp::Box, _) |
465-
Rvalue::Discriminant(..) => return None,
464+
Rvalue::NullaryOp(NullOp::Box, _) => return None,
466465

467466
Rvalue::Use(_) |
468467
Rvalue::Len(_) |
469468
Rvalue::Repeat(..) |
470469
Rvalue::Aggregate(..) |
470+
Rvalue::Discriminant(..) |
471471
Rvalue::Cast(..) |
472472
Rvalue::NullaryOp(..) |
473473
Rvalue::CheckedBinaryOp(..) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// compile-flags: -O
2+
3+
fn main() {
4+
let x = (if let Some(true) = Some(true) { 42 } else { 10 }) + 0;
5+
}
6+
7+
// END RUST SOURCE
8+
// START rustc.main.ConstProp.before.mir
9+
// bb0: {
10+
// ...
11+
// _3 = std::option::Option::<bool>::Some(const true,);
12+
// _4 = discriminant(_3);
13+
// switchInt(move _4) -> [1isize: bb3, otherwise: bb2];
14+
// }
15+
// bb1: {
16+
// _2 = const 42i32;
17+
// goto -> bb4;
18+
// }
19+
// bb2: {
20+
// _2 = const 10i32;
21+
// goto -> bb4;
22+
// }
23+
// bb3: {
24+
// switchInt(((_3 as Some).0: bool)) -> [false: bb2, otherwise: bb1];
25+
// }
26+
// bb4: {
27+
// _1 = Add(move _2, const 0i32);
28+
// ...
29+
// }
30+
// END rustc.main.ConstProp.before.mir
31+
// START rustc.main.ConstProp.after.mir
32+
// bb0: {
33+
// ...
34+
// _3 = const Scalar(0x01) : std::option::Option<bool>;
35+
// _4 = const 1isize;
36+
// switchInt(const 1isize) -> [1isize: bb3, otherwise: bb2];
37+
// }
38+
// bb1: {
39+
// _2 = const 42i32;
40+
// goto -> bb4;
41+
// }
42+
// bb2: {
43+
// _2 = const 10i32;
44+
// goto -> bb4;
45+
// }
46+
// bb3: {
47+
// switchInt(const true) -> [false: bb2, otherwise: bb1];
48+
// }
49+
// bb4: {
50+
// _1 = Add(move _2, const 0i32);
51+
// ...
52+
// }
53+
// END rustc.main.ConstProp.after.mir

0 commit comments

Comments
 (0)