Propagate uses of constants correctly so that array index checks work

This commit is contained in:
Fabian Zaiser 2018-06-03 20:57:45 +02:00
parent f9157f5b86
commit 589f9a87a8
4 changed files with 21 additions and 20 deletions

View File

@ -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);

View File

@ -1,9 +0,0 @@
error[E0080]: constant evaluation error
--> $DIR/index_out_of_bound.rs:11:19
|
LL | static FOO: i32 = [][0];
| ^^^^^ index out of bounds: the len is 0 but the index is 0
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.

View File

@ -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
}

View File

@ -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`.