about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMark Simulacrum <mark.simulacrum@gmail.com>2018-06-05 08:33:48 -0600
committerGitHub <noreply@github.com>2018-06-05 08:33:48 -0600
commit54cb13d9750bde19c1b3baabc2b9ae43d281ff44 (patch)
tree38eaffb76c4c82cb3c6e7fa903cc65aba97ddd1f /src
parentac32f8151b33fe1f00e1991c1d5ac20f7914fad3 (diff)
parent96004899bec1d63a8f54bea461f2d2a2965d4636 (diff)
downloadrust-54cb13d9750bde19c1b3baabc2b9ae43d281ff44.tar.gz
rust-54cb13d9750bde19c1b3baabc2b9ae43d281ff44.zip
Rollup merge of #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 #48920.

r? @oli-obk because you suggested it
Diffstat (limited to 'src')
-rw-r--r--src/librustc_mir/transform/const_prop.rs10
-rw-r--r--src/test/compile-fail/const-err-early.rs4
-rw-r--r--src/test/compile-fail/const-err2.rs1
-rw-r--r--src/test/compile-fail/const-err3.rs1
-rw-r--r--src/test/run-fail/mir_indexing_oob_1.rs1
-rw-r--r--src/test/run-fail/mir_indexing_oob_2.rs1
-rw-r--r--src/test/run-fail/mir_indexing_oob_3.rs1
-rw-r--r--src/test/ui/const-eval/index_out_of_bound.stderr9
-rw-r--r--src/test/ui/const-eval/index_out_of_bounds.rs (renamed from src/test/ui/const-eval/index_out_of_bound.rs)5
-rw-r--r--src/test/ui/const-eval/index_out_of_bounds.stderr17
10 files changed, 28 insertions, 22 deletions
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<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);
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]
 }
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
index e7ffbe81b9a..f3578bcef6e 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
+}
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`.