[const-prop] Handle MIR Rvalue::Aggregates
This commit is contained in:
parent
c8f7e18ceb
commit
a2e3ed5c05
13 changed files with 75 additions and 7 deletions
|
@ -436,13 +436,13 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
|||
|
||||
// if this isn't a supported operation, then return None
|
||||
match rvalue {
|
||||
Rvalue::Aggregate(..) |
|
||||
Rvalue::NullaryOp(NullOp::Box, _) |
|
||||
Rvalue::Discriminant(..) => return None,
|
||||
|
||||
Rvalue::Use(_) |
|
||||
Rvalue::Len(_) |
|
||||
Rvalue::Repeat(..) |
|
||||
Rvalue::Aggregate(..) |
|
||||
Rvalue::Cast(..) |
|
||||
Rvalue::NullaryOp(..) |
|
||||
Rvalue::CheckedBinaryOp(..) |
|
||||
|
@ -535,6 +535,12 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
|||
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| {
|
||||
|
|
|
@ -14,6 +14,7 @@ fn main() {
|
|||
//~^ ERROR const_err
|
||||
let _e = [5u8][1];
|
||||
//~^ ERROR const_err
|
||||
//~| ERROR this expression will panic at runtime
|
||||
black_box(b);
|
||||
black_box(c);
|
||||
black_box(d);
|
||||
|
|
25
src/test/mir-opt/const_prop/aggregate.rs
Normal file
25
src/test/mir-opt/const_prop/aggregate.rs
Normal 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
|
|
@ -2,6 +2,7 @@
|
|||
// compile-flags: -C debug-assertions
|
||||
|
||||
#![warn(exceeding_bitshifts)]
|
||||
#![warn(const_err)]
|
||||
|
||||
fn main() {
|
||||
let _n = 1i64 >> [64][0];
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
// compile-flags: -C debug-assertions
|
||||
|
||||
#![warn(exceeding_bitshifts)]
|
||||
#![warn(const_err)]
|
||||
#![feature(const_indexing)]
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -23,6 +23,7 @@ fn main() {
|
|||
//~^ ERROR const_err
|
||||
let _e = [5u8][1];
|
||||
//~^ ERROR index out of bounds
|
||||
//~| ERROR this expression will panic at runtime
|
||||
black_box(a);
|
||||
black_box(b);
|
||||
black_box(c);
|
||||
|
|
|
@ -34,5 +34,11 @@ error: index out of bounds: the len is 1 but the index is 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
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@ fn main() {
|
|||
//~^ ERROR const_err
|
||||
let _e = [5u8][1];
|
||||
//~^ ERROR const_err
|
||||
//~| ERROR this expression will panic at runtime
|
||||
black_box(a);
|
||||
black_box(b);
|
||||
black_box(c);
|
||||
|
|
|
@ -34,5 +34,11 @@ error: index out of bounds: the len is 1 but the index is 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
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
fn main() {
|
||||
[1][0u64 as usize];
|
||||
[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
|
||||
//~| ERROR this expression will panic at runtime
|
||||
}
|
||||
|
|
|
@ -6,11 +6,23 @@ LL | [1][1.5 as usize];
|
|||
|
|
||||
= 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
|
||||
--> $DIR/issue-54348.rs:4:5
|
||||
--> $DIR/issue-54348.rs:5:5
|
||||
|
|
||||
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
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ fn main() {
|
|||
let n = 1u8 << (4+3);
|
||||
let n = 1u8 << (4+4); //~ ERROR: attempt to shift left with overflow
|
||||
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")]
|
||||
const BITS: usize = 32;
|
||||
|
|
|
@ -10,6 +10,12 @@ note: lint level defined here
|
|||
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
|
||||
--> $DIR/lint-exceeding-bitshifts2.rs:17:15
|
||||
|
|
||||
|
@ -22,5 +28,5 @@ error: attempt to shift left with overflow
|
|||
LL | let n = 1_usize << BITS;
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
error: aborting due to 4 previous errors
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue