From 589f9a87a87c2f444e83a1e4d25de0ac8c0ca7a4 Mon Sep 17 00:00:00 2001 From: Fabian Zaiser Date: Sun, 3 Jun 2018 20:57:45 +0200 Subject: [PATCH] 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`.