Skip to content

Commit 85f2945

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

File tree

13 files changed

+75
-7
lines changed

13 files changed

+75
-7
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 7 additions & 1 deletion
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::Aggregate(..) |
465464
Rvalue::NullaryOp(NullOp::Box, _) |
466465
Rvalue::Discriminant(..) => return None,
467466

468467
Rvalue::Use(_) |
469468
Rvalue::Len(_) |
470469
Rvalue::Repeat(..) |
470+
Rvalue::Aggregate(..) |
471471
Rvalue::Cast(..) |
472472
Rvalue::NullaryOp(..) |
473473
Rvalue::CheckedBinaryOp(..) |
@@ -560,6 +560,12 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
560560
return None;
561561
}
562562
}
563+
} else if let Rvalue::Aggregate(_, operands) = rvalue {
564+
// FIXME(wesleywiser): const eval will turn this into a `const Scalar(<ZST>)` that
565+
// `SimplifyLocals` doesn't know it can remove.
566+
if operands.len() == 0 {
567+
return None;
568+
}
563569
}
564570

565571
self.use_ecx(source_info, |this| {

src/test/compile-fail/consts/const-err3.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn main() {
1414
//~^ ERROR const_err
1515
let _e = [5u8][1];
1616
//~^ ERROR const_err
17+
//~| ERROR this expression will panic at runtime
1718
black_box(b);
1819
black_box(c);
1920
black_box(d);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// compile-flags: -O
2+
3+
fn main() {
4+
let x = (0, 1, 2).1 + 0;
5+
}
6+
7+
// END RUST SOURCE
8+
// START rustc.main.ConstProp.before.mir
9+
// bb0: {
10+
// ...
11+
// _3 = (const 0i32, const 1i32, const 2i32);
12+
// _2 = (_3.1: i32);
13+
// _1 = Add(move _2, const 0i32);
14+
// ...
15+
// }
16+
// END rustc.main.ConstProp.before.mir
17+
// START rustc.main.ConstProp.after.mir
18+
// bb0: {
19+
// ...
20+
// _3 = (const 0i32, const 1i32, const 2i32);
21+
// _2 = const 1i32;
22+
// _1 = Add(move _2, const 0i32);
23+
// ...
24+
// }
25+
// END rustc.main.ConstProp.after.mir

src/test/run-fail/overflowing-rsh-5.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// compile-flags: -C debug-assertions
33

44
#![warn(exceeding_bitshifts)]
5+
#![warn(const_err)]
56

67
fn main() {
78
let _n = 1i64 >> [64][0];

src/test/run-fail/overflowing-rsh-6.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// compile-flags: -C debug-assertions
33

44
#![warn(exceeding_bitshifts)]
5+
#![warn(const_err)]
56
#![feature(const_indexing)]
67

78
fn main() {

src/test/ui/consts/const-err2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fn main() {
2323
//~^ ERROR const_err
2424
let _e = [5u8][1];
2525
//~^ ERROR index out of bounds
26+
//~| ERROR this expression will panic at runtime
2627
black_box(a);
2728
black_box(b);
2829
black_box(c);

src/test/ui/consts/const-err2.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,11 @@ error: index out of bounds: the len is 1 but the index is 1
3434
LL | let _e = [5u8][1];
3535
| ^^^^^^^^
3636

37-
error: aborting due to 5 previous errors
37+
error: this expression will panic at runtime
38+
--> $DIR/const-err2.rs:24:14
39+
|
40+
LL | let _e = [5u8][1];
41+
| ^^^^^^^^ index out of bounds: the len is 1 but the index is 1
42+
43+
error: aborting due to 6 previous errors
3844

src/test/ui/consts/const-err3.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fn main() {
2323
//~^ ERROR const_err
2424
let _e = [5u8][1];
2525
//~^ ERROR const_err
26+
//~| ERROR this expression will panic at runtime
2627
black_box(a);
2728
black_box(b);
2829
black_box(c);

src/test/ui/consts/const-err3.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,11 @@ error: index out of bounds: the len is 1 but the index is 1
3434
LL | let _e = [5u8][1];
3535
| ^^^^^^^^
3636

37-
error: aborting due to 5 previous errors
37+
error: this expression will panic at runtime
38+
--> $DIR/const-err3.rs:24:14
39+
|
40+
LL | let _e = [5u8][1];
41+
| ^^^^^^^^ index out of bounds: the len is 1 but the index is 1
42+
43+
error: aborting due to 6 previous errors
3844

src/test/ui/issues/issue-54348.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
fn main() {
22
[1][0u64 as usize];
33
[1][1.5 as usize]; //~ ERROR index out of bounds
4+
//~| ERROR this expression will panic at runtime
45
[1][1u64 as usize]; //~ ERROR index out of bounds
6+
//~| ERROR this expression will panic at runtime
57
}

0 commit comments

Comments
 (0)