Skip to content

Check array indices in constant propagation #51308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
@@ -240,16 +240,6 @@ impl<'b, 'a, 'tcx:'b> ConstPropagator<'b, 'a, 'tcx> {
) -> Option<Const<'tcx>> {
let span = source_info.span;
match *rvalue {
// No need to overwrite an already evaluated constant
Rvalue::Use(Operand::Constant(box Constant {
literal: Literal::Value {
value: &ty::Const {
val: ConstVal::Value(_),
..
},
},
..
})) => None,
// This branch exists for the sanity type check
Rvalue::Use(Operand::Constant(ref c)) => {
assert_eq!(c.ty, place_ty);
4 changes: 2 additions & 2 deletions src/test/compile-fail/const-err-early.rs
Original file line number Diff line number Diff line change
@@ -19,8 +19,8 @@ pub const C: u8 = 200u8 * 4; //~ ERROR const_err
//~^ ERROR this constant cannot be used
pub const D: u8 = 42u8 - (42u8 + 1); //~ ERROR const_err
//~^ ERROR this constant cannot be used
pub const E: u8 = [5u8][1];
//~^ ERROR const_err
pub const E: u8 = [5u8][1]; //~ ERROR const_err
//~| ERROR this constant cannot be used

fn main() {
let _a = A;
1 change: 1 addition & 0 deletions src/test/compile-fail/const-err2.rs
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@ fn main() {
let d = 42u8 - (42u8 + 1);
//~^ ERROR const_err
let _e = [5u8][1];
//~^ ERROR const_err
black_box(a);
black_box(b);
black_box(c);
1 change: 1 addition & 0 deletions src/test/compile-fail/const-err3.rs
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ fn main() {
let d = 42u8 - (42u8 + 1);
//~^ ERROR const_err
let _e = [5u8][1];
//~^ ERROR const_err
black_box(b);
black_box(c);
black_box(d);
1 change: 1 addition & 0 deletions src/test/run-fail/mir_indexing_oob_1.rs
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@

const C: [u32; 5] = [0; 5];

#[allow(const_err)]
fn test() -> u32 {
C[10]
}
1 change: 1 addition & 0 deletions src/test/run-fail/mir_indexing_oob_2.rs
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@

const C: &'static [u8; 5] = b"hello";

#[allow(const_err)]
fn test() -> u8 {
C[10]
}
1 change: 1 addition & 0 deletions src/test/run-fail/mir_indexing_oob_3.rs
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@

const C: &'static [u8; 5] = b"hello";

#[allow(const_err)]
fn mir() -> u8 {
C[10]
}
9 changes: 0 additions & 9 deletions src/test/ui/const-eval/index_out_of_bound.stderr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -11,4 +11,7 @@
static FOO: i32 = [][0];
//~^ ERROR E0080

fn main() {}
fn main() {
let array = [std::env::args().len()];
array[1]; //~ ERROR index out of bounds
}
17 changes: 17 additions & 0 deletions src/test/ui/const-eval/index_out_of_bounds.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0080]: constant evaluation error
--> $DIR/index_out_of_bounds.rs:11:19
|
LL | static FOO: i32 = [][0];
| ^^^^^ index out of bounds: the len is 0 but the index is 0

error: index out of bounds: the len is 1 but the index is 1
--> $DIR/index_out_of_bounds.rs:16:5
|
LL | array[1]; //~ ERROR index out of bounds
| ^^^^^^^^
|
= note: #[deny(const_err)] on by default

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0080`.