From 589f9a87a87c2f444e83a1e4d25de0ac8c0ca7a4 Mon Sep 17 00:00:00 2001 From: Fabian Zaiser Date: Sun, 3 Jun 2018 20:57:45 +0200 Subject: [PATCH 1/3] Propagate uses of constants correctly so that array index checks work --- src/librustc_mir/transform/const_prop.rs | 10 ---------- .../ui/const-eval/index_out_of_bound.stderr | 9 --------- ...x_out_of_bound.rs => index_out_of_bounds.rs} | 5 ++++- .../ui/const-eval/index_out_of_bounds.stderr | 17 +++++++++++++++++ 4 files changed, 21 insertions(+), 20 deletions(-) delete mode 100644 src/test/ui/const-eval/index_out_of_bound.stderr rename src/test/ui/const-eval/{index_out_of_bound.rs => index_out_of_bounds.rs} (83%) create mode 100644 src/test/ui/const-eval/index_out_of_bounds.stderr diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index 40a6610c417..d39042ceba9 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -240,16 +240,6 @@ impl<'b, 'a, 'tcx:'b> ConstPropagator<'b, 'a, 'tcx> { ) -> Option> { 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); diff --git a/src/test/ui/const-eval/index_out_of_bound.stderr b/src/test/ui/const-eval/index_out_of_bound.stderr deleted file mode 100644 index d16231c72b9..00000000000 --- a/src/test/ui/const-eval/index_out_of_bound.stderr +++ /dev/null @@ -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`. diff --git a/src/test/ui/const-eval/index_out_of_bound.rs b/src/test/ui/const-eval/index_out_of_bounds.rs similarity index 83% rename from src/test/ui/const-eval/index_out_of_bound.rs rename to src/test/ui/const-eval/index_out_of_bounds.rs index e7ffbe81b9a..9624b2924ba 100644 --- a/src/test/ui/const-eval/index_out_of_bound.rs +++ b/src/test/ui/const-eval/index_out_of_bounds.rs @@ -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 +} \ No newline at end of file diff --git a/src/test/ui/const-eval/index_out_of_bounds.stderr b/src/test/ui/const-eval/index_out_of_bounds.stderr new file mode 100644 index 00000000000..96e592dc209 --- /dev/null +++ b/src/test/ui/const-eval/index_out_of_bounds.stderr @@ -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`. From 29c43fea376624d62a8466e9304312f70c19d558 Mon Sep 17 00:00:00 2001 From: Fabian Zaiser Date: Sun, 3 Jun 2018 21:05:22 +0200 Subject: [PATCH 2/3] Fix tidy --- src/test/ui/const-eval/index_out_of_bounds.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/const-eval/index_out_of_bounds.rs b/src/test/ui/const-eval/index_out_of_bounds.rs index 9624b2924ba..f3578bcef6e 100644 --- a/src/test/ui/const-eval/index_out_of_bounds.rs +++ b/src/test/ui/const-eval/index_out_of_bounds.rs @@ -14,4 +14,4 @@ static FOO: i32 = [][0]; fn main() { let array = [std::env::args().len()]; array[1]; //~ ERROR index out of bounds -} \ No newline at end of file +} From 96004899bec1d63a8f54bea461f2d2a2965d4636 Mon Sep 17 00:00:00 2001 From: Fabian Zaiser Date: Sun, 3 Jun 2018 23:39:02 +0200 Subject: [PATCH 3/3] Fix tests --- src/test/compile-fail/const-err-early.rs | 4 ++-- src/test/compile-fail/const-err2.rs | 1 + src/test/compile-fail/const-err3.rs | 1 + src/test/run-fail/mir_indexing_oob_1.rs | 1 + src/test/run-fail/mir_indexing_oob_2.rs | 1 + src/test/run-fail/mir_indexing_oob_3.rs | 1 + 6 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/test/compile-fail/const-err-early.rs b/src/test/compile-fail/const-err-early.rs index 6caec159d01..f8b20f6ee79 100644 --- a/src/test/compile-fail/const-err-early.rs +++ b/src/test/compile-fail/const-err-early.rs @@ -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; diff --git a/src/test/compile-fail/const-err2.rs b/src/test/compile-fail/const-err2.rs index 46b73371e56..9a5cb5a4a83 100644 --- a/src/test/compile-fail/const-err2.rs +++ b/src/test/compile-fail/const-err2.rs @@ -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); diff --git a/src/test/compile-fail/const-err3.rs b/src/test/compile-fail/const-err3.rs index 9656af60024..f5e43b57e77 100644 --- a/src/test/compile-fail/const-err3.rs +++ b/src/test/compile-fail/const-err3.rs @@ -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); diff --git a/src/test/run-fail/mir_indexing_oob_1.rs b/src/test/run-fail/mir_indexing_oob_1.rs index 41ff466f810..cf342ad94f9 100644 --- a/src/test/run-fail/mir_indexing_oob_1.rs +++ b/src/test/run-fail/mir_indexing_oob_1.rs @@ -12,6 +12,7 @@ const C: [u32; 5] = [0; 5]; +#[allow(const_err)] fn test() -> u32 { C[10] } diff --git a/src/test/run-fail/mir_indexing_oob_2.rs b/src/test/run-fail/mir_indexing_oob_2.rs index c5c823428bc..3eb94682b20 100644 --- a/src/test/run-fail/mir_indexing_oob_2.rs +++ b/src/test/run-fail/mir_indexing_oob_2.rs @@ -12,6 +12,7 @@ const C: &'static [u8; 5] = b"hello"; +#[allow(const_err)] fn test() -> u8 { C[10] } diff --git a/src/test/run-fail/mir_indexing_oob_3.rs b/src/test/run-fail/mir_indexing_oob_3.rs index 9bc4b0025e5..06bb6d4d287 100644 --- a/src/test/run-fail/mir_indexing_oob_3.rs +++ b/src/test/run-fail/mir_indexing_oob_3.rs @@ -12,6 +12,7 @@ const C: &'static [u8; 5] = b"hello"; +#[allow(const_err)] fn mir() -> u8 { C[10] }