Skip to content

Commit a2e3ed5

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

13 files changed

+75
-7
lines changed

src/librustc_mir/transform/const_prop.rs

+7-1
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::Aggregate(..) |
440439
Rvalue::NullaryOp(NullOp::Box, _) |
441440
Rvalue::Discriminant(..) => return None,
442441

443442
Rvalue::Use(_) |
444443
Rvalue::Len(_) |
445444
Rvalue::Repeat(..) |
445+
Rvalue::Aggregate(..) |
446446
Rvalue::Cast(..) |
447447
Rvalue::NullaryOp(..) |
448448
Rvalue::CheckedBinaryOp(..) |
@@ -535,6 +535,12 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
535535
return None;
536536
}
537537
}
538+
} else if let Rvalue::Aggregate(_, operands) = rvalue {
539+
// FIXME(wesleywiser): const eval will turn this into a `const Scalar(<ZST>)` that
540+
// `SimplifyLocals` doesn't know it can remove.
541+
if operands.len() == 0 {
542+
return None;
543+
}
538544
}
539545

540546
self.use_ecx(source_info, |this| {

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

+1
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);
+25
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

+1
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

+1
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

+1
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

+7-1
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

+1
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

+7-1
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

+2
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
}

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@ LL | [1][1.5 as usize];
66
|
77
= note: `#[deny(const_err)]` on by default
88

9+
error: this expression will panic at runtime
10+
--> $DIR/issue-54348.rs:3:5
11+
|
12+
LL | [1][1.5 as usize];
13+
| ^^^^^^^^^^^^^^^^^ index out of bounds: the len is 1 but the index is 1
14+
915
error: index out of bounds: the len is 1 but the index is 1
10-
--> $DIR/issue-54348.rs:4:5
16+
--> $DIR/issue-54348.rs:5:5
1117
|
1218
LL | [1][1u64 as usize];
1319
| ^^^^^^^^^^^^^^^^^^
1420

15-
error: aborting due to 2 previous errors
21+
error: this expression will panic at runtime
22+
--> $DIR/issue-54348.rs:5:5
23+
|
24+
LL | [1][1u64 as usize];
25+
| ^^^^^^^^^^^^^^^^^^ index out of bounds: the len is 1 but the index is 1
26+
27+
error: aborting due to 4 previous errors
1628

src/test/ui/lint/lint-exceeding-bitshifts2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88
let n = 1u8 << (4+3);
99
let n = 1u8 << (4+4); //~ ERROR: attempt to shift left with overflow
1010
let n = 1i64 >> [63][0];
11-
let n = 1i64 >> [64][0]; // should be linting, needs to wait for const propagation
11+
let n = 1i64 >> [64][0]; //~ ERROR: attempt to shift right with overflow
1212

1313
#[cfg(target_pointer_width = "32")]
1414
const BITS: usize = 32;

src/test/ui/lint/lint-exceeding-bitshifts2.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ note: lint level defined here
1010
LL | #![deny(exceeding_bitshifts, const_err)]
1111
| ^^^^^^^^^^^^^^^^^^^
1212

13+
error: attempt to shift right with overflow
14+
--> $DIR/lint-exceeding-bitshifts2.rs:11:15
15+
|
16+
LL | let n = 1i64 >> [64][0];
17+
| ^^^^^^^^^^^^^^^
18+
1319
error: attempt to shift left with overflow
1420
--> $DIR/lint-exceeding-bitshifts2.rs:17:15
1521
|
@@ -22,5 +28,5 @@ error: attempt to shift left with overflow
2228
LL | let n = 1_usize << BITS;
2329
| ^^^^^^^^^^^^^^^
2430

25-
error: aborting due to 3 previous errors
31+
error: aborting due to 4 previous errors
2632

0 commit comments

Comments
 (0)