]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #51308 - fanzier:const-prop-array-bounds-check, r=oli-obk
authorMark Simulacrum <mark.simulacrum@gmail.com>
Tue, 5 Jun 2018 14:33:48 +0000 (08:33 -0600)
committerGitHub <noreply@github.com>
Tue, 5 Jun 2018 14:33:48 +0000 (08:33 -0600)
Check array indices in constant propagation

Previously, uses of constant weren't correctly propagated.
This fixes #48920.

r? @oli-obk because you suggested it

src/librustc_mir/transform/const_prop.rs
src/test/compile-fail/const-err-early.rs
src/test/compile-fail/const-err2.rs
src/test/compile-fail/const-err3.rs
src/test/run-fail/mir_indexing_oob_1.rs
src/test/run-fail/mir_indexing_oob_2.rs
src/test/run-fail/mir_indexing_oob_3.rs
src/test/ui/const-eval/index_out_of_bound.rs [deleted file]
src/test/ui/const-eval/index_out_of_bound.stderr [deleted file]
src/test/ui/const-eval/index_out_of_bounds.rs [new file with mode: 0644]
src/test/ui/const-eval/index_out_of_bounds.stderr [new file with mode: 0644]

index 40a6610c4173caab16c2f96816f9e4e68748640a..d39042ceba99fc6162c5d4aa6d246084d6ad3f26 100644 (file)
@@ -240,16 +240,6 @@ fn const_prop(
     ) -> 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);
index 6caec159d019ce39ce7f9bd1d65f64a561aa9b53..f8b20f6ee7933f4efac8699f741efe23c6b56491 100644 (file)
@@ -19,8 +19,8 @@
 //~^ 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;
index 46b73371e56cf554ee652cb136a67b9d28132877..9a5cb5a4a83fac6fdd10a4ba4930688ff7531609 100644 (file)
@@ -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);
index 9656af6002442c6369dd86120e9cb3008cb96b6f..f5e43b57e7775082022d99b3291b3765c03da23b 100644 (file)
@@ -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);
index 41ff466f810ea262d8cd9237e50b0f7172c4e9ac..cf342ad94f990271e34c8f0eedfd5fac33727c2f 100644 (file)
@@ -12,6 +12,7 @@
 
 const C: [u32; 5] = [0; 5];
 
+#[allow(const_err)]
 fn test() -> u32 {
     C[10]
 }
index c5c823428bc94edfcf26095d60614c990252e7f1..3eb94682b20471d05a05eead80d74f9ef0009573 100644 (file)
@@ -12,6 +12,7 @@
 
 const C: &'static [u8; 5] = b"hello";
 
+#[allow(const_err)]
 fn test() -> u8 {
     C[10]
 }
index 9bc4b0025e55ad46f604fddd34a0438430a48d85..06bb6d4d2871378cc173b75cd81f49f8d26235be 100644 (file)
@@ -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.rs b/src/test/ui/const-eval/index_out_of_bound.rs
deleted file mode 100644 (file)
index e7ffbe8..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-static FOO: i32 = [][0];
-//~^ ERROR E0080
-
-fn main() {}
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 (file)
index d16231c..0000000
+++ /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_bounds.rs b/src/test/ui/const-eval/index_out_of_bounds.rs
new file mode 100644 (file)
index 0000000..f3578bc
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+static FOO: i32 = [][0];
+//~^ ERROR E0080
+
+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 (file)
index 0000000..96e592d
--- /dev/null
@@ -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`.