[const-prop] Handle MIR Rvalue::Aggregates

This commit is contained in:
Wesley Wiser 2019-09-14 14:11:31 -04:00
parent c8f7e18ceb
commit a2e3ed5c05
13 changed files with 75 additions and 7 deletions

View file

@ -436,13 +436,13 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
// if this isn't a supported operation, then return None // if this isn't a supported operation, then return None
match rvalue { match rvalue {
Rvalue::Aggregate(..) |
Rvalue::NullaryOp(NullOp::Box, _) | Rvalue::NullaryOp(NullOp::Box, _) |
Rvalue::Discriminant(..) => return None, Rvalue::Discriminant(..) => return None,
Rvalue::Use(_) | Rvalue::Use(_) |
Rvalue::Len(_) | Rvalue::Len(_) |
Rvalue::Repeat(..) | Rvalue::Repeat(..) |
Rvalue::Aggregate(..) |
Rvalue::Cast(..) | Rvalue::Cast(..) |
Rvalue::NullaryOp(..) | Rvalue::NullaryOp(..) |
Rvalue::CheckedBinaryOp(..) | Rvalue::CheckedBinaryOp(..) |
@ -535,6 +535,12 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
return None; return None;
} }
} }
} else if let Rvalue::Aggregate(_, operands) = rvalue {
// FIXME(wesleywiser): const eval will turn this into a `const Scalar(<ZST>)` that
// `SimplifyLocals` doesn't know it can remove.
if operands.len() == 0 {
return None;
}
} }
self.use_ecx(source_info, |this| { self.use_ecx(source_info, |this| {

View file

@ -14,6 +14,7 @@ fn main() {
//~^ ERROR const_err //~^ ERROR const_err
let _e = [5u8][1]; let _e = [5u8][1];
//~^ ERROR const_err //~^ ERROR const_err
//~| ERROR this expression will panic at runtime
black_box(b); black_box(b);
black_box(c); black_box(c);
black_box(d); black_box(d);

View file

@ -0,0 +1,25 @@
// compile-flags: -O
fn main() {
let x = (0, 1, 2).1 + 0;
}
// END RUST SOURCE
// START rustc.main.ConstProp.before.mir
// bb0: {
// ...
// _3 = (const 0i32, const 1i32, const 2i32);
// _2 = (_3.1: i32);
// _1 = Add(move _2, const 0i32);
// ...
// }
// END rustc.main.ConstProp.before.mir
// START rustc.main.ConstProp.after.mir
// bb0: {
// ...
// _3 = (const 0i32, const 1i32, const 2i32);
// _2 = const 1i32;
// _1 = Add(move _2, const 0i32);
// ...
// }
// END rustc.main.ConstProp.after.mir

View file

@ -2,6 +2,7 @@
// compile-flags: -C debug-assertions // compile-flags: -C debug-assertions
#![warn(exceeding_bitshifts)] #![warn(exceeding_bitshifts)]
#![warn(const_err)]
fn main() { fn main() {
let _n = 1i64 >> [64][0]; let _n = 1i64 >> [64][0];

View file

@ -2,6 +2,7 @@
// compile-flags: -C debug-assertions // compile-flags: -C debug-assertions
#![warn(exceeding_bitshifts)] #![warn(exceeding_bitshifts)]
#![warn(const_err)]
#![feature(const_indexing)] #![feature(const_indexing)]
fn main() { fn main() {

View file

@ -23,6 +23,7 @@ fn main() {
//~^ ERROR const_err //~^ ERROR const_err
let _e = [5u8][1]; let _e = [5u8][1];
//~^ ERROR index out of bounds //~^ ERROR index out of bounds
//~| ERROR this expression will panic at runtime
black_box(a); black_box(a);
black_box(b); black_box(b);
black_box(c); black_box(c);

View file

@ -34,5 +34,11 @@ error: index out of bounds: the len is 1 but the index is 1
LL | let _e = [5u8][1]; LL | let _e = [5u8][1];
| ^^^^^^^^ | ^^^^^^^^
error: aborting due to 5 previous errors error: this expression will panic at runtime
--> $DIR/const-err2.rs:24:14
|
LL | let _e = [5u8][1];
| ^^^^^^^^ index out of bounds: the len is 1 but the index is 1
error: aborting due to 6 previous errors

View file

@ -23,6 +23,7 @@ fn main() {
//~^ ERROR const_err //~^ ERROR const_err
let _e = [5u8][1]; let _e = [5u8][1];
//~^ ERROR const_err //~^ ERROR const_err
//~| ERROR this expression will panic at runtime
black_box(a); black_box(a);
black_box(b); black_box(b);
black_box(c); black_box(c);

View file

@ -34,5 +34,11 @@ error: index out of bounds: the len is 1 but the index is 1
LL | let _e = [5u8][1]; LL | let _e = [5u8][1];
| ^^^^^^^^ | ^^^^^^^^
error: aborting due to 5 previous errors error: this expression will panic at runtime
--> $DIR/const-err3.rs:24:14
|
LL | let _e = [5u8][1];
| ^^^^^^^^ index out of bounds: the len is 1 but the index is 1
error: aborting due to 6 previous errors

View file

@ -1,5 +1,7 @@
fn main() { fn main() {
[1][0u64 as usize]; [1][0u64 as usize];
[1][1.5 as usize]; //~ ERROR index out of bounds [1][1.5 as usize]; //~ ERROR index out of bounds
//~| ERROR this expression will panic at runtime
[1][1u64 as usize]; //~ ERROR index out of bounds [1][1u64 as usize]; //~ ERROR index out of bounds
//~| ERROR this expression will panic at runtime
} }

View file

@ -6,11 +6,23 @@ LL | [1][1.5 as usize];
| |
= note: `#[deny(const_err)]` on by default = note: `#[deny(const_err)]` on by default
error: this expression will panic at runtime
--> $DIR/issue-54348.rs:3:5
|
LL | [1][1.5 as usize];
| ^^^^^^^^^^^^^^^^^ index out of bounds: the len is 1 but the index is 1
error: index out of bounds: the len is 1 but the index is 1 error: index out of bounds: the len is 1 but the index is 1
--> $DIR/issue-54348.rs:4:5 --> $DIR/issue-54348.rs:5:5
| |
LL | [1][1u64 as usize]; LL | [1][1u64 as usize];
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors error: this expression will panic at runtime
--> $DIR/issue-54348.rs:5:5
|
LL | [1][1u64 as usize];
| ^^^^^^^^^^^^^^^^^^ index out of bounds: the len is 1 but the index is 1
error: aborting due to 4 previous errors

View file

@ -8,7 +8,7 @@ fn main() {
let n = 1u8 << (4+3); let n = 1u8 << (4+3);
let n = 1u8 << (4+4); //~ ERROR: attempt to shift left with overflow let n = 1u8 << (4+4); //~ ERROR: attempt to shift left with overflow
let n = 1i64 >> [63][0]; let n = 1i64 >> [63][0];
let n = 1i64 >> [64][0]; // should be linting, needs to wait for const propagation let n = 1i64 >> [64][0]; //~ ERROR: attempt to shift right with overflow
#[cfg(target_pointer_width = "32")] #[cfg(target_pointer_width = "32")]
const BITS: usize = 32; const BITS: usize = 32;

View file

@ -10,6 +10,12 @@ note: lint level defined here
LL | #![deny(exceeding_bitshifts, const_err)] LL | #![deny(exceeding_bitshifts, const_err)]
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
error: attempt to shift right with overflow
--> $DIR/lint-exceeding-bitshifts2.rs:11:15
|
LL | let n = 1i64 >> [64][0];
| ^^^^^^^^^^^^^^^
error: attempt to shift left with overflow error: attempt to shift left with overflow
--> $DIR/lint-exceeding-bitshifts2.rs:17:15 --> $DIR/lint-exceeding-bitshifts2.rs:17:15
| |
@ -22,5 +28,5 @@ error: attempt to shift left with overflow
LL | let n = 1_usize << BITS; LL | let n = 1_usize << BITS;
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors error: aborting due to 4 previous errors