]> git.lizzy.rs Git - rust.git/commitdiff
Referring to erroneous constants in promoteds must abort the build
authorOliver Schneider <github35764891676564198441@oli-obk.de>
Sun, 3 Jun 2018 00:04:20 +0000 (02:04 +0200)
committerOliver Schneider <github35764891676564198441@oli-obk.de>
Tue, 5 Jun 2018 18:49:46 +0000 (20:49 +0200)
14 files changed:
src/librustc_mir/monomorphize/collector.rs
src/test/ui/const-eval/conditional_array_execution.nll.stderr [new file with mode: 0644]
src/test/ui/const-eval/conditional_array_execution.rs
src/test/ui/const-eval/conditional_array_execution.stderr
src/test/ui/const-eval/issue-43197.nll.stderr [new file with mode: 0644]
src/test/ui/const-eval/issue-43197.rs
src/test/ui/const-eval/issue-43197.stderr
src/test/ui/const-eval/issue-44578.nll.stderr [new file with mode: 0644]
src/test/ui/const-eval/issue-44578.rs
src/test/ui/const-eval/issue-44578.stderr
src/test/ui/const-eval/issue-50814-2.rs [new file with mode: 0644]
src/test/ui/const-eval/issue-50814-2.stderr [new file with mode: 0644]
src/test/ui/const-eval/issue-50814.rs [new file with mode: 0644]
src/test/ui/const-eval/issue-50814.stderr [new file with mode: 0644]

index 54221239253cc7f2db48254d1343e9f9d267d940..4f3adaaae274cece31021b80e5a86a47513a6fbb 100644 (file)
@@ -1190,13 +1190,25 @@ fn collect_neighbours<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
     let param_env = ty::ParamEnv::reveal_all();
     for i in 0..mir.promoted.len() {
         use rustc_data_structures::indexed_vec::Idx;
+        let i = Promoted::new(i);
         let cid = GlobalId {
             instance,
-            promoted: Some(Promoted::new(i)),
+            promoted: Some(i),
         };
         match tcx.const_eval(param_env.and(cid)) {
             Ok(val) => collect_const(tcx, val, instance.substs, output),
-            Err(_) => {},
+            Err(err) => {
+                use rustc::middle::const_val::ErrKind;
+                use rustc::mir::interpret::EvalErrorKind;
+                if let ErrKind::Miri(ref miri, ..) = *err.kind {
+                    if let EvalErrorKind::ReferencedConstant = miri.kind {
+                        err.report_as_error(
+                            tcx.at(mir.promoted[i].span),
+                            "erroneous constant used",
+                        );
+                    }
+                }
+            },
         }
     }
 }
diff --git a/src/test/ui/const-eval/conditional_array_execution.nll.stderr b/src/test/ui/const-eval/conditional_array_execution.nll.stderr
new file mode 100644 (file)
index 0000000..a35252c
--- /dev/null
@@ -0,0 +1,45 @@
+warning: attempt to subtract with overflow
+  --> $DIR/conditional_array_execution.rs:15:19
+   |
+LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
+   |                   ^^^^^
+   |
+note: lint level defined here
+  --> $DIR/conditional_array_execution.rs:11:9
+   |
+LL | #![warn(const_err)]
+   |         ^^^^^^^^^
+
+warning: this constant cannot be used
+  --> $DIR/conditional_array_execution.rs:15:1
+   |
+LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
+   | ^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                   |
+   |                   attempt to subtract with overflow
+
+warning: this expression will panic at runtime
+  --> $DIR/conditional_array_execution.rs:20:20
+   |
+LL |     println!("{}", FOO);
+   |                    ^^^ referenced constant has errors
+
+error[E0080]: erroneous constant used
+  --> $DIR/conditional_array_execution.rs:20:5
+   |
+LL |     println!("{}", FOO);
+   |     ^^^^^^^^^^^^^^^---^^
+   |                    |
+   |                    referenced constant has errors
+   |
+   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
+
+error[E0080]: erroneous constant used
+  --> $DIR/conditional_array_execution.rs:20:20
+   |
+LL |     println!("{}", FOO);
+   |                    ^^^ referenced constant has errors
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0080`.
index daeeae513d9112570f20ae0d33992e4a9d3967c9..055224d27315d0312c17d3ddb10384731031d760 100644 (file)
@@ -8,7 +8,6 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// compile-pass
 #![warn(const_err)]
 
 const X: u32 = 5;
@@ -20,4 +19,5 @@
 fn main() {
     println!("{}", FOO);
     //~^ WARN this expression will panic at runtime
+    //~| ERROR erroneous constant used
 }
index 00b39d08eaf9413573a2a4c51fddb0c6a73864ab..f396c8f0444e7ffc48b28a7d3774aa09244abecd 100644 (file)
@@ -1,17 +1,17 @@
 warning: attempt to subtract with overflow
-  --> $DIR/conditional_array_execution.rs:16:19
+  --> $DIR/conditional_array_execution.rs:15:19
    |
 LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
    |                   ^^^^^
    |
 note: lint level defined here
-  --> $DIR/conditional_array_execution.rs:12:9
+  --> $DIR/conditional_array_execution.rs:11:9
    |
 LL | #![warn(const_err)]
    |         ^^^^^^^^^
 
 warning: this constant cannot be used
-  --> $DIR/conditional_array_execution.rs:16:1
+  --> $DIR/conditional_array_execution.rs:15:1
    |
 LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
    | ^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -19,8 +19,17 @@ LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
    |                   attempt to subtract with overflow
 
 warning: this expression will panic at runtime
-  --> $DIR/conditional_array_execution.rs:21:20
+  --> $DIR/conditional_array_execution.rs:20:20
    |
 LL |     println!("{}", FOO);
    |                    ^^^ referenced constant has errors
 
+error[E0080]: erroneous constant used
+  --> $DIR/conditional_array_execution.rs:20:20
+   |
+LL |     println!("{}", FOO);
+   |                    ^^^ referenced constant has errors
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0080`.
diff --git a/src/test/ui/const-eval/issue-43197.nll.stderr b/src/test/ui/const-eval/issue-43197.nll.stderr
new file mode 100644 (file)
index 0000000..4169676
--- /dev/null
@@ -0,0 +1,71 @@
+warning: attempt to subtract with overflow
+  --> $DIR/issue-43197.rs:20:20
+   |
+LL |     const X: u32 = 0-1;
+   |                    ^^^
+   |
+note: lint level defined here
+  --> $DIR/issue-43197.rs:11:9
+   |
+LL | #![warn(const_err)]
+   |         ^^^^^^^^^
+
+warning: this constant cannot be used
+  --> $DIR/issue-43197.rs:20:5
+   |
+LL |     const X: u32 = 0-1;
+   |     ^^^^^^^^^^^^^^^---^
+   |                    |
+   |                    attempt to subtract with overflow
+
+warning: attempt to subtract with overflow
+  --> $DIR/issue-43197.rs:23:24
+   |
+LL |     const Y: u32 = foo(0-1);
+   |                        ^^^
+
+warning: this constant cannot be used
+  --> $DIR/issue-43197.rs:23:5
+   |
+LL |     const Y: u32 = foo(0-1);
+   |     ^^^^^^^^^^^^^^^^^^^---^^
+   |                        |
+   |                        attempt to subtract with overflow
+
+warning: this expression will panic at runtime
+  --> $DIR/issue-43197.rs:26:23
+   |
+LL |     println!("{} {}", X, Y);
+   |                       ^ referenced constant has errors
+
+warning: this expression will panic at runtime
+  --> $DIR/issue-43197.rs:26:26
+   |
+LL |     println!("{} {}", X, Y);
+   |                          ^ referenced constant has errors
+
+error[E0080]: erroneous constant used
+  --> $DIR/issue-43197.rs:26:5
+   |
+LL |     println!("{} {}", X, Y);
+   |     ^^^^^^^^^^^^^^^^^^-^^^^^
+   |                       |
+   |                       referenced constant has errors
+   |
+   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
+
+error[E0080]: erroneous constant used
+  --> $DIR/issue-43197.rs:26:26
+   |
+LL |     println!("{} {}", X, Y);
+   |                          ^ referenced constant has errors
+
+error[E0080]: erroneous constant used
+  --> $DIR/issue-43197.rs:26:23
+   |
+LL |     println!("{} {}", X, Y);
+   |                       ^ referenced constant has errors
+
+error: aborting due to 3 previous errors
+
+For more information about this error, try `rustc --explain E0080`.
index df352adea635808ed6420ff577d75f426072baa1..f8d820db74796da3078d3bb556a0de6aaef03550 100644 (file)
@@ -8,7 +8,6 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// compile-pass
 #![warn(const_err)]
 
 #![feature(const_fn)]
@@ -27,4 +26,6 @@ fn main() {
     println!("{} {}", X, Y);
     //~^ WARN this expression will panic at runtime
     //~| WARN this expression will panic at runtime
+    //~| ERROR erroneous constant used
+    //~| ERROR erroneous constant used
 }
index 412d97883f143e8ba1048aa329a472120b2bb45a..207523f688f977d11966b80611fdb0485f310233 100644 (file)
@@ -1,17 +1,17 @@
 warning: attempt to subtract with overflow
-  --> $DIR/issue-43197.rs:21:20
+  --> $DIR/issue-43197.rs:20:20
    |
 LL |     const X: u32 = 0-1;
    |                    ^^^
    |
 note: lint level defined here
-  --> $DIR/issue-43197.rs:12:9
+  --> $DIR/issue-43197.rs:11:9
    |
 LL | #![warn(const_err)]
    |         ^^^^^^^^^
 
 warning: this constant cannot be used
-  --> $DIR/issue-43197.rs:21:5
+  --> $DIR/issue-43197.rs:20:5
    |
 LL |     const X: u32 = 0-1;
    |     ^^^^^^^^^^^^^^^---^
@@ -19,13 +19,13 @@ LL |     const X: u32 = 0-1;
    |                    attempt to subtract with overflow
 
 warning: attempt to subtract with overflow
-  --> $DIR/issue-43197.rs:24:24
+  --> $DIR/issue-43197.rs:23:24
    |
 LL |     const Y: u32 = foo(0-1);
    |                        ^^^
 
 warning: this constant cannot be used
-  --> $DIR/issue-43197.rs:24:5
+  --> $DIR/issue-43197.rs:23:5
    |
 LL |     const Y: u32 = foo(0-1);
    |     ^^^^^^^^^^^^^^^^^^^---^^
@@ -33,14 +33,29 @@ LL |     const Y: u32 = foo(0-1);
    |                        attempt to subtract with overflow
 
 warning: this expression will panic at runtime
-  --> $DIR/issue-43197.rs:27:23
+  --> $DIR/issue-43197.rs:26:23
    |
 LL |     println!("{} {}", X, Y);
    |                       ^ referenced constant has errors
 
 warning: this expression will panic at runtime
-  --> $DIR/issue-43197.rs:27:26
+  --> $DIR/issue-43197.rs:26:26
    |
 LL |     println!("{} {}", X, Y);
    |                          ^ referenced constant has errors
 
+error[E0080]: erroneous constant used
+  --> $DIR/issue-43197.rs:26:26
+   |
+LL |     println!("{} {}", X, Y);
+   |                          ^ referenced constant has errors
+
+error[E0080]: erroneous constant used
+  --> $DIR/issue-43197.rs:26:23
+   |
+LL |     println!("{} {}", X, Y);
+   |                       ^ referenced constant has errors
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0080`.
diff --git a/src/test/ui/const-eval/issue-44578.nll.stderr b/src/test/ui/const-eval/issue-44578.nll.stderr
new file mode 100644 (file)
index 0000000..49587fa
--- /dev/null
@@ -0,0 +1,19 @@
+error[E0080]: erroneous constant used
+  --> $DIR/issue-44578.rs:35:5
+   |
+LL |     println!("{}", <Bar<u16, u8> as Foo>::AMT);
+   |     ^^^^^^^^^^^^^^^--------------------------^^
+   |                    |
+   |                    referenced constant has errors
+   |
+   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
+
+error[E0080]: erroneous constant used
+  --> $DIR/issue-44578.rs:35:20
+   |
+LL |     println!("{}", <Bar<u16, u8> as Foo>::AMT);
+   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0080`.
index 4133a8864f619b39088ed3ad696be37be27c1787..331de4ab661558e7f389b9c26328085e20c1b191 100644 (file)
@@ -8,8 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// compile-pass
-#![warn(const_err)]
+#![allow(const_err)]
 
 trait Foo {
     const AMT: usize;
@@ -33,6 +32,6 @@ impl Foo for u16 {
 }
 
 fn main() {
-    println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
-    //~^ WARN const_err
+    println!("{}", <Bar<u16, u8> as Foo>::AMT);
+    //~^ ERROR erroneous constant used
 }
index 3632a9baea17277f72175757bb814e92493aebf2..42d60c604b8f2ca4ab02b1016b4e8d6901fd007a 100644 (file)
@@ -1,18 +1,9 @@
-warning: this expression will panic at runtime
-  --> $DIR/issue-44578.rs:36:20
+error[E0080]: erroneous constant used
+  --> $DIR/issue-44578.rs:35:20
    |
-LL |     println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
+LL |     println!("{}", <Bar<u16, u8> as Foo>::AMT);
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
-   |
-note: lint level defined here
-  --> $DIR/issue-44578.rs:12:9
-   |
-LL | #![warn(const_err)]
-   |         ^^^^^^^^^
 
-warning: this expression will panic at runtime
-  --> $DIR/issue-44578.rs:36:20
-   |
-LL |     println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
-   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
+error: aborting due to previous error
 
+For more information about this error, try `rustc --explain E0080`.
diff --git a/src/test/ui/const-eval/issue-50814-2.rs b/src/test/ui/const-eval/issue-50814-2.rs
new file mode 100644 (file)
index 0000000..a905792
--- /dev/null
@@ -0,0 +1,42 @@
+// 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.
+
+trait C {
+    const BOO: usize;
+}
+
+trait Foo<T> {
+    const BAR: usize;
+}
+
+struct A<T>(T);
+
+impl<T: C> Foo<T> for A<T> {
+    const BAR: usize = [5, 6, 7][T::BOO];
+}
+
+fn foo<T: C>() -> &'static usize {
+    &<A<T> as Foo<T>>::BAR //~ ERROR erroneous constant used
+}
+
+impl C for () {
+    const BOO: usize = 42;
+}
+
+impl C for u32 {
+    const BOO: usize = 1;
+}
+
+fn main() {
+    println!("{:x}", foo::<()>() as *const usize as usize);
+    println!("{:x}", foo::<u32>() as *const usize as usize);
+    println!("{:x}", foo::<()>());
+    println!("{:x}", foo::<u32>());
+}
\ No newline at end of file
diff --git a/src/test/ui/const-eval/issue-50814-2.stderr b/src/test/ui/const-eval/issue-50814-2.stderr
new file mode 100644 (file)
index 0000000..3c61313
--- /dev/null
@@ -0,0 +1,11 @@
+error[E0080]: erroneous constant used
+  --> $DIR/issue-50814-2.rs:26:5
+   |
+LL |     &<A<T> as Foo<T>>::BAR //~ ERROR erroneous constant used
+   |     ^---------------------
+   |      |
+   |      referenced constant has errors
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0080`.
diff --git a/src/test/ui/const-eval/issue-50814.rs b/src/test/ui/const-eval/issue-50814.rs
new file mode 100644 (file)
index 0000000..2af51c4
--- /dev/null
@@ -0,0 +1,32 @@
+// 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.
+
+trait Unsigned {
+    const MAX: u8;
+}
+
+struct U8(u8);
+impl Unsigned for U8 {
+    const MAX: u8 = 0xff;
+}
+
+struct Sum<A,B>(A,B);
+
+impl<A: Unsigned, B: Unsigned> Unsigned for Sum<A,B> {
+    const MAX: u8 = A::MAX + B::MAX;
+}
+
+fn foo<T>(_: T) -> &'static u8 {
+    &Sum::<U8,U8>::MAX //~ ERROR erroneous constant used
+}
+
+fn main() {
+    foo(0);
+}
\ No newline at end of file
diff --git a/src/test/ui/const-eval/issue-50814.stderr b/src/test/ui/const-eval/issue-50814.stderr
new file mode 100644 (file)
index 0000000..b3e13b4
--- /dev/null
@@ -0,0 +1,11 @@
+error[E0080]: erroneous constant used
+  --> $DIR/issue-50814.rs:27:5
+   |
+LL |     &Sum::<U8,U8>::MAX //~ ERROR erroneous constant used
+   |     ^-----------------
+   |      |
+   |      referenced constant has errors
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0080`.