Skip to content

Commit

Permalink
[const-prop] Handle MIR Rvalue::Discriminant
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleywiser committed Oct 18, 2019
1 parent a2e3ed5 commit 4d89031
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/librustc_mir/transform/const_prop.rs
Expand Up @@ -436,13 +436,13 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {

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

Rvalue::Use(_) |
Rvalue::Len(_) |
Rvalue::Repeat(..) |
Rvalue::Aggregate(..) |
Rvalue::Discriminant(..) |
Rvalue::Cast(..) |
Rvalue::NullaryOp(..) |
Rvalue::CheckedBinaryOp(..) |
Expand Down
53 changes: 53 additions & 0 deletions src/test/mir-opt/const_prop/discriminant.rs
@@ -0,0 +1,53 @@
// compile-flags: -O

fn main() {
let x = (if let Some(true) = Some(true) { 42 } else { 10 }) + 0;
}

// END RUST SOURCE
// START rustc.main.ConstProp.before.mir
// bb0: {
// ...
// _3 = std::option::Option::<bool>::Some(const true,);
// _4 = discriminant(_3);
// switchInt(move _4) -> [1isize: bb3, otherwise: bb2];
// }
// bb1: {
// _2 = const 42i32;
// goto -> bb4;
// }
// bb2: {
// _2 = const 10i32;
// goto -> bb4;
// }
// bb3: {
// switchInt(((_3 as Some).0: bool)) -> [false: bb2, otherwise: bb1];
// }
// bb4: {
// _1 = Add(move _2, const 0i32);
// ...
// }
// END rustc.main.ConstProp.before.mir
// START rustc.main.ConstProp.after.mir
// bb0: {
// ...
// _3 = const Scalar(0x01) : std::option::Option<bool>;
// _4 = const 1isize;
// switchInt(const 1isize) -> [1isize: bb3, otherwise: bb2];
// }
// bb1: {
// _2 = const 42i32;
// goto -> bb4;
// }
// bb2: {
// _2 = const 10i32;
// goto -> bb4;
// }
// bb3: {
// switchInt(const true) -> [false: bb2, otherwise: bb1];
// }
// bb4: {
// _1 = Add(move _2, const 0i32);
// ...
// }
// END rustc.main.ConstProp.after.mir

0 comments on commit 4d89031

Please sign in to comment.