Skip to content

Commit c8f7e18

Browse files
committed
[const-prop] Handle MIR Rvalue::Repeat
1 parent da59656 commit c8f7e18

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,13 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
436436

437437
// if this isn't a supported operation, then return None
438438
match rvalue {
439-
Rvalue::Repeat(..) |
440439
Rvalue::Aggregate(..) |
441440
Rvalue::NullaryOp(NullOp::Box, _) |
442441
Rvalue::Discriminant(..) => return None,
443442

444443
Rvalue::Use(_) |
445444
Rvalue::Len(_) |
445+
Rvalue::Repeat(..) |
446446
Rvalue::Cast(..) |
447447
Rvalue::NullaryOp(..) |
448448
Rvalue::CheckedBinaryOp(..) |

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

Lines changed: 37 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 7 additions & 1 deletion
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)