Skip to content

Commit 589f9a8

Browse files
committed
Propagate uses of constants correctly so that array index checks work
1 parent f9157f5 commit 589f9a8

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

src/librustc_mir/transform/const_prop.rs

-10
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,6 @@ impl<'b, 'a, 'tcx:'b> ConstPropagator<'b, 'a, 'tcx> {
240240
) -> Option<Const<'tcx>> {
241241
let span = source_info.span;
242242
match *rvalue {
243-
// No need to overwrite an already evaluated constant
244-
Rvalue::Use(Operand::Constant(box Constant {
245-
literal: Literal::Value {
246-
value: &ty::Const {
247-
val: ConstVal::Value(_),
248-
..
249-
},
250-
},
251-
..
252-
})) => None,
253243
// This branch exists for the sanity type check
254244
Rvalue::Use(Operand::Constant(ref c)) => {
255245
assert_eq!(c.ty, place_ty);

src/test/ui/const-eval/index_out_of_bound.stderr

-9
This file was deleted.

src/test/ui/const-eval/index_out_of_bound.rs renamed to src/test/ui/const-eval/index_out_of_bounds.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@
1111
static FOO: i32 = [][0];
1212
//~^ ERROR E0080
1313

14-
fn main() {}
14+
fn main() {
15+
let array = [std::env::args().len()];
16+
array[1]; //~ ERROR index out of bounds
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: constant evaluation error
2+
--> $DIR/index_out_of_bounds.rs:11:19
3+
|
4+
LL | static FOO: i32 = [][0];
5+
| ^^^^^ index out of bounds: the len is 0 but the index is 0
6+
7+
error: index out of bounds: the len is 1 but the index is 1
8+
--> $DIR/index_out_of_bounds.rs:16:5
9+
|
10+
LL | array[1]; //~ ERROR index out of bounds
11+
| ^^^^^^^^
12+
|
13+
= note: #[deny(const_err)] on by default
14+
15+
error: aborting due to 2 previous errors
16+
17+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)