Skip to content

Commit

Permalink
[const-prop] Handle MIR Rvalue::Aggregates
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleywiser committed Oct 18, 2019
1 parent c8f7e18 commit a2e3ed5
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/librustc_mir/transform/const_prop.rs
Expand Up @@ -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(..) |
Expand Down Expand Up @@ -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| {
Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/consts/const-err3.rs
Expand Up @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions src/test/mir-opt/const_prop/aggregate.rs
@@ -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
1 change: 1 addition & 0 deletions src/test/run-fail/overflowing-rsh-5.rs
Expand Up @@ -2,6 +2,7 @@
// compile-flags: -C debug-assertions

#![warn(exceeding_bitshifts)]
#![warn(const_err)]

fn main() {
let _n = 1i64 >> [64][0];
Expand Down
1 change: 1 addition & 0 deletions src/test/run-fail/overflowing-rsh-6.rs
Expand Up @@ -2,6 +2,7 @@
// compile-flags: -C debug-assertions

#![warn(exceeding_bitshifts)]
#![warn(const_err)]
#![feature(const_indexing)]

fn main() {
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/consts/const-err2.rs
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/test/ui/consts/const-err2.stderr
Expand Up @@ -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

1 change: 1 addition & 0 deletions src/test/ui/consts/const-err3.rs
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/test/ui/consts/const-err3.stderr
Expand Up @@ -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

2 changes: 2 additions & 0 deletions src/test/ui/issues/issue-54348.rs
@@ -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
}
16 changes: 14 additions & 2 deletions src/test/ui/issues/issue-54348.stderr
Expand Up @@ -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

2 changes: 1 addition & 1 deletion src/test/ui/lint/lint-exceeding-bitshifts2.rs
Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/test/ui/lint/lint-exceeding-bitshifts2.stderr
Expand Up @@ -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
|
Expand All @@ -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

0 comments on commit a2e3ed5

Please sign in to comment.