Skip to content

Commit d2c0d10

Browse files
committed
[const-prop] Handle MIR Rvalue::Repeat
1 parent 488381c commit d2c0d10

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

src/librustc_mir/transform/const_prop.rs

+1-1
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::Repeat(..) |
465464
Rvalue::Aggregate(..) |
466465
Rvalue::NullaryOp(NullOp::Box, _) |
467466
Rvalue::Discriminant(..) => return None,
468467

469468
Rvalue::Use(_) |
470469
Rvalue::Len(_) |
470+
Rvalue::Repeat(..) |
471471
Rvalue::Cast(..) |
472472
Rvalue::NullaryOp(..) |
473473
Rvalue::CheckedBinaryOp(..) |

src/test/mir-opt/const_prop/repeat.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// compile-flags: -O
2+
3+
fn main() {
4+
let x: u32 = [42; 8][2] + 0;
5+
}
6+
7+
// END RUST SOURCE
8+
// START rustc.main.ConstProp.before.mir
9+
// bb0: {
10+
// ...
11+
// _3 = [const 42u32; 8];
12+
// ...
13+
// _4 = const 2usize;
14+
// _5 = const 8usize;
15+
// _6 = Lt(_4, _5);
16+
// assert(move _6, "index out of bounds: the len is move _5 but the index is _4") -> bb1;
17+
// }
18+
// bb1: {
19+
// _2 = _3[_4];
20+
// _1 = Add(move _2, const 0u32);
21+
// ...
22+
// return;
23+
// }
24+
// END rustc.main.ConstProp.before.mir
25+
// START rustc.main.ConstProp.after.mir
26+
// bb0: {
27+
// ...
28+
// _6 = const true;
29+
// assert(const true, "index out of bounds: the len is move _5 but the index is _4") -> bb1;
30+
// }
31+
// bb1: {
32+
// _2 = const 42u32;
33+
// _1 = Add(move _2, const 0u32);
34+
// ...
35+
// return;
36+
// }
37+
// END rustc.main.ConstProp.after.mir

src/test/ui/consts/const-prop-ice.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
fn main() {
22
[0; 3][3u64 as usize]; //~ ERROR the len is 3 but the index is 3
3+
//~| ERROR this expression will panic at runtime
34
}

src/test/ui/consts/const-prop-ice.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@ LL | [0; 3][3u64 as usize];
66
|
77
= note: `#[deny(const_err)]` on by default
88

9-
error: aborting due to previous error
9+
error: this expression will panic at runtime
10+
--> $DIR/const-prop-ice.rs:2:5
11+
|
12+
LL | [0; 3][3u64 as usize];
13+
| ^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the len is 3 but the index is 3
14+
15+
error: aborting due to 2 previous errors
1016

0 commit comments

Comments
 (0)