Skip to content

Commit 54cb13d

Browse files
Rollup merge of rust-lang#51308 - fanzier:const-prop-array-bounds-check, r=oli-obk
Check array indices in constant propagation Previously, uses of constant weren't correctly propagated. This fixes rust-lang#48920. r? @oli-obk because you suggested it
2 parents ac32f81 + 9600489 commit 54cb13d

10 files changed

+28
-22
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/compile-fail/const-err-early.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ pub const C: u8 = 200u8 * 4; //~ ERROR const_err
1919
//~^ ERROR this constant cannot be used
2020
pub const D: u8 = 42u8 - (42u8 + 1); //~ ERROR const_err
2121
//~^ ERROR this constant cannot be used
22-
pub const E: u8 = [5u8][1];
23-
//~^ ERROR const_err
22+
pub const E: u8 = [5u8][1]; //~ ERROR const_err
23+
//~| ERROR this constant cannot be used
2424

2525
fn main() {
2626
let _a = A;

src/test/compile-fail/const-err2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ fn main() {
3131
let d = 42u8 - (42u8 + 1);
3232
//~^ ERROR const_err
3333
let _e = [5u8][1];
34+
//~^ ERROR const_err
3435
black_box(a);
3536
black_box(b);
3637
black_box(c);

src/test/compile-fail/const-err3.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fn main() {
2323
let d = 42u8 - (42u8 + 1);
2424
//~^ ERROR const_err
2525
let _e = [5u8][1];
26+
//~^ ERROR const_err
2627
black_box(b);
2728
black_box(c);
2829
black_box(d);

src/test/run-fail/mir_indexing_oob_1.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

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

15+
#[allow(const_err)]
1516
fn test() -> u32 {
1617
C[10]
1718
}

src/test/run-fail/mir_indexing_oob_2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

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

15+
#[allow(const_err)]
1516
fn test() -> u8 {
1617
C[10]
1718
}

src/test/run-fail/mir_indexing_oob_3.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

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

15+
#[allow(const_err)]
1516
fn mir() -> u8 {
1617
C[10]
1718
}

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)