]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #50110 - oli-obk:warn_all_the_constants, r=estebank
authorbors <bors@rust-lang.org>
Wed, 25 Apr 2018 09:19:07 +0000 (09:19 +0000)
committerbors <bors@rust-lang.org>
Wed, 25 Apr 2018 09:19:07 +0000 (09:19 +0000)
Warn on all erroneous constants

fixes #49791
fixes #47054

@Zoxc this PR triggers the nondeterministic errors of https://github.com/rust-lang/rust/pull/49950#issuecomment-383074959 really often (at least on stage1).

18 files changed:
src/librustc_lint/builtin.rs
src/librustc_lint/lib.rs
src/test/compile-fail/array_const_index-0.rs
src/test/compile-fail/array_const_index-1.rs
src/test/compile-fail/const-err-early.rs
src/test/compile-fail/const-err-multi.rs
src/test/compile-fail/const-eval-overflow2.rs
src/test/compile-fail/const-eval-overflow2b.rs
src/test/compile-fail/const-eval-overflow2c.rs
src/test/compile-fail/const-slice-oob.rs
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.rs
src/test/ui/const-eval/issue-43197.stderr
src/test/ui/const-eval/pub_const_err.rs
src/test/ui/const-eval/pub_const_err.stderr
src/test/ui/const-eval/pub_const_err_bin.rs [new file with mode: 0644]
src/test/ui/const-eval/pub_const_err_bin.stderr [new file with mode: 0644]

index 6f2c51b0f1899afaadaf2dcb99e6e43859819878..af0620b00051f901e6f6ebf6e5ad8a30a4eb3cfb 100644 (file)
@@ -1441,3 +1441,71 @@ fn check_item(&mut self, cx: &LateContext, item: &hir::Item) {
         }
     }
 }
+
+/// Lint constants that are erroneous.
+/// Without this lint, we might not get any diagnostic if the constant is
+/// unused within this crate, even though downstream crates can't use it
+/// without producing an error.
+pub struct UnusedBrokenConst;
+
+impl LintPass for UnusedBrokenConst {
+    fn get_lints(&self) -> LintArray {
+        lint_array!()
+    }
+}
+
+fn check_const(cx: &LateContext, body_id: hir::BodyId, what: &str) {
+    let def_id = cx.tcx.hir.body_owner_def_id(body_id);
+    let param_env = cx.tcx.param_env(def_id);
+    let cid = ::rustc::mir::interpret::GlobalId {
+        instance: ty::Instance::mono(cx.tcx, def_id),
+        promoted: None
+    };
+    if let Err(err) = cx.tcx.const_eval(param_env.and(cid)) {
+        let span = cx.tcx.def_span(def_id);
+        let mut diag = cx.struct_span_lint(
+            CONST_ERR,
+            span,
+            &format!("this {} cannot be used", what),
+        );
+        use rustc::middle::const_val::ConstEvalErrDescription;
+        match err.description() {
+            ConstEvalErrDescription::Simple(message) => {
+                diag.span_label(span, message);
+            }
+            ConstEvalErrDescription::Backtrace(miri, frames) => {
+                diag.span_label(span, format!("{}", miri));
+                for frame in frames {
+                    diag.span_label(frame.span, format!("inside call to `{}`", frame.location));
+                }
+            }
+        }
+        diag.emit()
+    }
+}
+
+struct UnusedBrokenConstVisitor<'a, 'tcx: 'a>(&'a LateContext<'a, 'tcx>);
+
+impl<'a, 'tcx, 'v> hir::intravisit::Visitor<'v> for UnusedBrokenConstVisitor<'a, 'tcx> {
+    fn visit_nested_body(&mut self, id: hir::BodyId) {
+        check_const(self.0, id, "array length");
+    }
+    fn nested_visit_map<'this>(&'this mut self) -> hir::intravisit::NestedVisitorMap<'this, 'v> {
+        hir::intravisit::NestedVisitorMap::None
+    }
+}
+
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedBrokenConst {
+    fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
+        match it.node {
+            hir::ItemConst(_, body_id) => {
+                check_const(cx, body_id, "constant");
+            },
+            hir::ItemTy(ref ty, _) => hir::intravisit::walk_ty(
+                &mut UnusedBrokenConstVisitor(cx),
+                ty
+            ),
+            _ => {},
+        }
+    }
+}
index 038d47dd5eae583fc744d3ae3edd203f36aa9b06..889c7d3e34ef96c45d93f3bda047037a3df98dcc 100644 (file)
@@ -136,6 +136,7 @@ macro_rules! add_lint_group {
                  UnionsWithDropFields,
                  UnreachablePub,
                  TypeAliasBounds,
+                 UnusedBrokenConst,
                  );
 
     add_builtin_with_new!(sess,
index 501c66e75cded115892fddfe02c0be9588b0c9c0..9b9deb4ca8d9d9cbef5304fd34a13d4447a388f0 100644 (file)
@@ -12,6 +12,7 @@
 const B: i32 = (&A)[1];
 //~^ ERROR constant evaluation error
 //~| index out of bounds: the len is 0 but the index is 1
+//~| WARN this constant cannot be used
 
 fn main() {
     let _ = B;
index d3b43e83bfe5218ac914584e6424f6fd4ee4a9d4..46feb20cf11ddbda624db1055b74db74476b9965 100644 (file)
@@ -12,6 +12,7 @@
 const B: i32 = A[1];
 //~^ ERROR constant evaluation error
 //~| index out of bounds: the len is 0 but the index is 1
+//~| WARN this constant cannot be used
 
 fn main() {
     let _ = B;
index 3de0f1ff61e1cb4c148ba5ec1a3a7ba99bb7df7d..6caec159d019ce39ce7f9bd1d65f64a561aa9b53 100644 (file)
 
 #![deny(const_err)]
 
-pub const A: i8 = -std::i8::MIN; //~ ERROR E0080
-//~^ ERROR attempt to negate with overflow
+pub const A: i8 = -std::i8::MIN; //~ ERROR const_err
+//~^ ERROR this constant cannot be used
 //~| ERROR constant evaluation error
-pub const B: u8 = 200u8 + 200u8; //~ ERROR E0080
-//~^ ERROR attempt to add with overflow
-pub const C: u8 = 200u8 * 4; //~ ERROR E0080
-//~^ ERROR attempt to multiply with overflow
-pub const D: u8 = 42u8 - (42u8 + 1); //~ ERROR E0080
-//~^ ERROR attempt to subtract with overflow
+pub const B: u8 = 200u8 + 200u8; //~ ERROR const_err
+//~^ ERROR this constant cannot be used
+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 E0080
+//~^ ERROR const_err
 
 fn main() {
     let _a = A;
index d2355f57f17294c5ed97c40a951d97b4de2764d9..6f0281b8bd0ca7382bf595d54137dae3a5b852a7 100644 (file)
 //~^ ERROR E0080
 //~| ERROR attempt to negate with overflow
 //~| ERROR constant evaluation error
+//~| ERROR this constant cannot be used
 pub const B: i8 = A;
-//~^ ERROR E0080
+//~^ ERROR const_err
 pub const C: u8 = A as u8;
-//~^ ERROR E0080
+//~^ ERROR const_err
 pub const D: i8 = 50 - A;
-//~^ ERROR E0080
+//~^ ERROR const_err
 
 fn main() {
     let _ = (A, B, C, D);
index a0d8f9672c05e1519ffb37e72705dae40315ca4e..faa8c3039b78cdfe0aae394a6428ea16b9adc8bd 100644 (file)
 use std::{u8, u16, u32, u64, usize};
 
 const VALS_I8: (i8,) =
+     //~^ ERROR this constant cannot be used
     (
      i8::MIN - 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to subtract with overflow
+     //~^ ERROR attempt to subtract with overflow
      );
 
 const VALS_I16: (i16,) =
+     //~^ ERROR this constant cannot be used
     (
      i16::MIN - 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to subtract with overflow
+     //~^ ERROR attempt to subtract with overflow
      );
 
 const VALS_I32: (i32,) =
+     //~^ ERROR this constant cannot be used
     (
      i32::MIN - 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to subtract with overflow
+     //~^ ERROR attempt to subtract with overflow
      );
 
 const VALS_I64: (i64,) =
+     //~^ ERROR this constant cannot be used
     (
      i64::MIN - 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to subtract with overflow
+     //~^ ERROR attempt to subtract with overflow
      );
 
 const VALS_U8: (u8,) =
+     //~^ ERROR this constant cannot be used
     (
      u8::MIN - 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to subtract with overflow
+     //~^ ERROR attempt to subtract with overflow
      );
 
 const VALS_U16: (u16,) = (
+     //~^ ERROR this constant cannot be used
      u16::MIN - 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to subtract with overflow
+     //~^ ERROR attempt to subtract with overflow
      );
 
 const VALS_U32: (u32,) = (
+     //~^ ERROR this constant cannot be used
      u32::MIN - 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to subtract with overflow
+     //~^ ERROR attempt to subtract with overflow
      );
 
 const VALS_U64: (u64,) =
+     //~^ ERROR this constant cannot be used
     (
      u64::MIN - 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to subtract with overflow
+     //~^ ERROR attempt to subtract with overflow
      );
 
 fn main() {
index 08128f90e532f368a16185a155bcbe0b5ac13104..d827e680c5bbbec49cad32f782b1b96cda6764d8 100644 (file)
 use std::{u8, u16, u32, u64, usize};
 
 const VALS_I8: (i8,) =
+     //~^ ERROR this constant cannot be used
     (
      i8::MAX + 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to add with overflow
+     //~^ ERROR attempt to add with overflow
      );
 
 const VALS_I16: (i16,) =
+     //~^ ERROR this constant cannot be used
     (
      i16::MAX + 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to add with overflow
+     //~^ ERROR attempt to add with overflow
      );
 
 const VALS_I32: (i32,) =
+     //~^ ERROR this constant cannot be used
     (
      i32::MAX + 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to add with overflow
+     //~^ ERROR attempt to add with overflow
      );
 
 const VALS_I64: (i64,) =
+     //~^ ERROR this constant cannot be used
     (
      i64::MAX + 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to add with overflow
+     //~^ ERROR attempt to add with overflow
      );
 
 const VALS_U8: (u8,) =
+     //~^ ERROR this constant cannot be used
     (
      u8::MAX + 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to add with overflow
+     //~^ ERROR attempt to add with overflow
      );
 
 const VALS_U16: (u16,) = (
+     //~^ ERROR this constant cannot be used
      u16::MAX + 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to add with overflow
+     //~^ ERROR attempt to add with overflow
      );
 
 const VALS_U32: (u32,) = (
+     //~^ ERROR this constant cannot be used
      u32::MAX + 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to add with overflow
+     //~^ ERROR attempt to add with overflow
      );
 
 const VALS_U64: (u64,) =
+     //~^ ERROR this constant cannot be used
     (
      u64::MAX + 1,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to add with overflow
+     //~^ ERROR attempt to add with overflow
      );
 
 fn main() {
index 31a1638cade170e7fecbf058fd4266937d22093c..2fd46b038ef28d7e62b9f98fcec95e360ba5c119 100644 (file)
 use std::{u8, u16, u32, u64, usize};
 
 const VALS_I8: (i8,) =
+     //~^ ERROR this constant cannot be used
     (
      i8::MIN * 2,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to multiply with overflow
+     //~^ ERROR attempt to multiply with overflow
      );
 
 const VALS_I16: (i16,) =
+     //~^ ERROR this constant cannot be used
     (
      i16::MIN * 2,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to multiply with overflow
+     //~^ ERROR attempt to multiply with overflow
      );
 
 const VALS_I32: (i32,) =
+     //~^ ERROR this constant cannot be used
     (
      i32::MIN * 2,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to multiply with overflow
+     //~^ ERROR attempt to multiply with overflow
      );
 
 const VALS_I64: (i64,) =
+     //~^ ERROR this constant cannot be used
     (
      i64::MIN * 2,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to multiply with overflow
+     //~^ ERROR attempt to multiply with overflow
      );
 
 const VALS_U8: (u8,) =
+     //~^ ERROR this constant cannot be used
     (
      u8::MAX * 2,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to multiply with overflow
+     //~^ ERROR attempt to multiply with overflow
      );
 
 const VALS_U16: (u16,) = (
+     //~^ ERROR this constant cannot be used
      u16::MAX * 2,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to multiply with overflow
+     //~^ ERROR attempt to multiply with overflow
      );
 
 const VALS_U32: (u32,) = (
+     //~^ ERROR this constant cannot be used
      u32::MAX * 2,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to multiply with overflow
+     //~^ ERROR attempt to multiply with overflow
      );
 
 const VALS_U64: (u64,) =
+     //~^ ERROR this constant cannot be used
     (
      u64::MAX * 2,
-     //~^ ERROR constant evaluation error
-     //~| ERROR attempt to multiply with overflow
+     //~^ ERROR attempt to multiply with overflow
      );
 
 fn main() {
index 179ea9e853f3ad6bd4a4abc7973ee4fb40e7203d..7da5a2f17eaf90960d6339474ec99ddd2a509b47 100644 (file)
@@ -14,6 +14,7 @@
 const BAR: u32 = FOO[5];
 //~^ ERROR constant evaluation error [E0080]
 //~| index out of bounds: the len is 3 but the index is 5
+//~| WARN this constant cannot be used
 
 fn main() {
     let _ = BAR;
index e059c354d65a73151a3d06d8d9b57077cc1e59dd..dbddee862e022a87d3521e58ffec761291acf78b 100644 (file)
@@ -14,6 +14,7 @@
 const Y: u32 = 6;
 const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
 //~^ WARN attempt to subtract with overflow
+//~| WARN this constant cannot be used
 
 fn main() {
     println!("{}", FOO);
index fd29990fec295abd5c0d807b663020bec0c72a26..713b1b36c08b1b0c55f807d0eee174608f2e45a4 100644 (file)
@@ -6,8 +6,14 @@ LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
    |
    = note: #[warn(const_err)] on by default
 
+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: constant evaluation error
-  --> $DIR/conditional_array_execution.rs:19:20
+  --> $DIR/conditional_array_execution.rs:20:20
    |
 LL |     println!("{}", FOO);
    |                    ^^^ referenced constant has errors
index b27791ef5337b7c7039d1005e2464e5b452521d5..097fba4d3c45094b98152e68372b8878b789539e 100644 (file)
@@ -19,8 +19,10 @@ const fn foo(x: u32) -> u32 {
 fn main() {
     const X: u32 = 0-1;
     //~^ WARN attempt to subtract with overflow
+    //~| WARN this constant cannot be used
     const Y: u32 = foo(0-1);
     //~^ WARN attempt to subtract with overflow
+    //~| WARN this constant cannot be used
     println!("{} {}", X, Y);
     //~^ WARN constant evaluation error
     //~| WARN constant evaluation error
index 5da47a85eb803f3b1b79e8179d72b93856dfa020..a22e8016296c4f95a3b2fe53907d52bd39a71db4 100644 (file)
@@ -6,20 +6,32 @@ LL |     const X: u32 = 0-1;
    |
    = note: #[warn(const_err)] on by default
 
-warning: constant evaluation error
-  --> $DIR/issue-43197.rs:24:23
+warning: this constant cannot be used
+  --> $DIR/issue-43197.rs:20:5
    |
-LL |     println!("{} {}", X, Y);
-   |                       ^ referenced constant has errors
+LL |     const X: u32 = 0-1;
+   |     ^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
 
 warning: attempt to subtract with overflow
-  --> $DIR/issue-43197.rs:22:24
+  --> $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: constant evaluation error
+  --> $DIR/issue-43197.rs:26:23
+   |
+LL |     println!("{} {}", X, Y);
+   |                       ^ referenced constant has errors
+
 warning: constant evaluation error
-  --> $DIR/issue-43197.rs:24:26
+  --> $DIR/issue-43197.rs:26:26
    |
 LL |     println!("{} {}", X, Y);
    |                          ^ referenced constant has errors
index bdb9f5b19a88541088a6cbcce2be7827e721fc24..c6bf07649af38a17dd33964436037a59b06d0493 100644 (file)
@@ -8,9 +8,14 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-#![deny(const_err)]
+// compile-pass
 
 #![crate_type = "lib"]
 
 pub const Z: u32 = 0 - 1;
-//~^ ERROR attempt to subtract with overflow
+//~^ WARN attempt to subtract with overflow
+//~| WARN this constant cannot be used
+
+pub type Foo = [i32; 0 - 1];
+//~^ WARN attempt to subtract with overflow
+//~| WARN this array length cannot be used
index b77ec38ca1679869a692892c8d8ed94bf3d795f9..2981ac20cd981d639a426fd082a0486e2e4ff147 100644 (file)
@@ -1,14 +1,26 @@
-error: attempt to subtract with overflow
+warning: attempt to subtract with overflow
   --> $DIR/pub_const_err.rs:15:20
    |
 LL | pub const Z: u32 = 0 - 1;
    |                    ^^^^^
    |
-note: lint level defined here
-  --> $DIR/pub_const_err.rs:11:9
+   = note: #[warn(const_err)] on by default
+
+warning: this constant cannot be used
+  --> $DIR/pub_const_err.rs:15:1
    |
-LL | #![deny(const_err)]
-   |         ^^^^^^^^^
+LL | pub const Z: u32 = 0 - 1;
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
 
-error: aborting due to previous error
+warning: attempt to subtract with overflow
+  --> $DIR/pub_const_err.rs:19:22
+   |
+LL | pub type Foo = [i32; 0 - 1];
+   |                      ^^^^^
+
+warning: this array length cannot be used
+  --> $DIR/pub_const_err.rs:19:22
+   |
+LL | pub type Foo = [i32; 0 - 1];
+   |                      ^^^^^ attempt to subtract with overflow
 
diff --git a/src/test/ui/const-eval/pub_const_err_bin.rs b/src/test/ui/const-eval/pub_const_err_bin.rs
new file mode 100644 (file)
index 0000000..d87cb7e
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright 2017 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.
+
+// compile-pass
+
+pub const Z: u32 = 0 - 1;
+//~^ WARN attempt to subtract with overflow
+//~| WARN this constant cannot be used
+
+pub type Foo = [i32; 0 - 1];
+//~^ WARN attempt to subtract with overflow
+//~| WARN this array length cannot be used
+
+fn main() {}
diff --git a/src/test/ui/const-eval/pub_const_err_bin.stderr b/src/test/ui/const-eval/pub_const_err_bin.stderr
new file mode 100644 (file)
index 0000000..3e8966d
--- /dev/null
@@ -0,0 +1,26 @@
+warning: attempt to subtract with overflow
+  --> $DIR/pub_const_err_bin.rs:13:20
+   |
+LL | pub const Z: u32 = 0 - 1;
+   |                    ^^^^^
+   |
+   = note: #[warn(const_err)] on by default
+
+warning: this constant cannot be used
+  --> $DIR/pub_const_err_bin.rs:13:1
+   |
+LL | pub const Z: u32 = 0 - 1;
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
+
+warning: attempt to subtract with overflow
+  --> $DIR/pub_const_err_bin.rs:17:22
+   |
+LL | pub type Foo = [i32; 0 - 1];
+   |                      ^^^^^
+
+warning: this array length cannot be used
+  --> $DIR/pub_const_err_bin.rs:17:22
+   |
+LL | pub type Foo = [i32; 0 - 1];
+   |                      ^^^^^ attempt to subtract with overflow
+