Skip to content

Commit 4f5226a

Browse files
committed
Handle const eval failure
1 parent f24c0ea commit 4f5226a

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

compiler/rustc_mir/src/transform/const_goto.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ impl<'a, 'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'a, 'tcx> {
7676
{
7777
// We now know that the Switch matches on the const place, and it is statementless
7878
// Now find which value in the Switch matches the const value.
79-
let const_value = _const.literal.eval_bits(
79+
let const_value = _const.literal.try_eval_bits(
8080
self.tcx,
8181
self.param_env,
8282
switch_ty,
83-
);
83+
)?;
8484
let found_value_idx_option = targets
8585
.iter()
8686
.enumerate()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
- // MIR for `f` before ConstGoto
2+
+ // MIR for `f` after ConstGoto
3+
4+
fn f() -> u64 {
5+
let mut _0: u64; // return place in scope 0 at $DIR/const_goto_const_eval_fail.rs:6:44: 6:47
6+
let mut _1: bool; // in scope 0 at $DIR/const_goto_const_eval_fail.rs:7:11: 12:6
7+
let mut _2: i32; // in scope 0 at $DIR/const_goto_const_eval_fail.rs:8:15: 8:16
8+
9+
bb0: {
10+
StorageLive(_1); // scope 0 at $DIR/const_goto_const_eval_fail.rs:7:11: 12:6
11+
StorageLive(_2); // scope 0 at $DIR/const_goto_const_eval_fail.rs:8:15: 8:16
12+
_2 = const A; // scope 0 at $DIR/const_goto_const_eval_fail.rs:8:15: 8:16
13+
switchInt(_2) -> [1_i32: bb2, 2_i32: bb2, 3_i32: bb2, otherwise: bb1]; // scope 0 at $DIR/const_goto_const_eval_fail.rs:9:13: 9:14
14+
}
15+
16+
bb1: {
17+
_1 = const true; // scope 0 at $DIR/const_goto_const_eval_fail.rs:10:18: 10:22
18+
goto -> bb3; // scope 0 at $DIR/const_goto_const_eval_fail.rs:8:9: 11:10
19+
}
20+
21+
bb2: {
22+
_1 = const B; // scope 0 at $DIR/const_goto_const_eval_fail.rs:9:26: 9:27
23+
- goto -> bb3; // scope 0 at $DIR/const_goto_const_eval_fail.rs:8:9: 11:10
24+
+ switchInt(_1) -> [false: bb4, otherwise: bb3]; // scope 0 at $DIR/const_goto_const_eval_fail.rs:13:9: 13:14
25+
}
26+
27+
bb3: {
28+
- switchInt(_1) -> [false: bb5, otherwise: bb4]; // scope 0 at $DIR/const_goto_const_eval_fail.rs:13:9: 13:14
29+
- }
30+
-
31+
- bb4: {
32+
_0 = const 2_u64; // scope 0 at $DIR/const_goto_const_eval_fail.rs:14:17: 14:18
33+
- goto -> bb6; // scope 0 at $DIR/const_goto_const_eval_fail.rs:7:5: 15:6
34+
+ goto -> bb5; // scope 0 at $DIR/const_goto_const_eval_fail.rs:7:5: 15:6
35+
}
36+
37+
- bb5: {
38+
+ bb4: {
39+
_0 = const 1_u64; // scope 0 at $DIR/const_goto_const_eval_fail.rs:13:18: 13:19
40+
- goto -> bb6; // scope 0 at $DIR/const_goto_const_eval_fail.rs:7:5: 15:6
41+
+ goto -> bb5; // scope 0 at $DIR/const_goto_const_eval_fail.rs:7:5: 15:6
42+
}
43+
44+
- bb6: {
45+
+ bb5: {
46+
StorageDead(_2); // scope 0 at $DIR/const_goto_const_eval_fail.rs:16:1: 16:2
47+
StorageDead(_1); // scope 0 at $DIR/const_goto_const_eval_fail.rs:16:1: 16:2
48+
return; // scope 0 at $DIR/const_goto_const_eval_fail.rs:16:2: 16:2
49+
}
50+
}
51+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(min_const_generics)]
2+
#![crate_type = "lib"]
3+
4+
// If const eval fails, then don't crash
5+
// EMIT_MIR const_goto_const_eval_fail.f.ConstGoto.diff
6+
pub fn f<const A: i32, const B: bool>() -> u64 {
7+
match {
8+
match A {
9+
1 | 2 | 3 => B,
10+
_ => true,
11+
}
12+
} {
13+
false => 1,
14+
true => 2,
15+
}
16+
}

0 commit comments

Comments
 (0)