Auto merge of #50653 - oli-obk:bad_const, r=cramertj

Make the `const_err` lint `deny`-by-default

At best these things are runtime panics (debug mode) or overflows (release mode). More likely they are public constants that are unused in the crate declaring them.

This is not a breaking change, as dependencies won't break and root crates can `#![warn(const_err)]`, though I don't know why anyone would do that.
This commit is contained in:
bors 2018-05-18 17:17:35 +00:00
commit a722296b6e
28 changed files with 95 additions and 58 deletions

View File

@ -27,7 +27,7 @@ declare_lint! {
declare_lint! { declare_lint! {
pub CONST_ERR, pub CONST_ERR,
Warn, Deny,
"constant evaluation detected erroneous expression" "constant evaluation detected erroneous expression"
} }

View File

@ -10,9 +10,8 @@
const A: &'static [i32] = &[]; const A: &'static [i32] = &[];
const B: i32 = (&A)[1]; const B: i32 = (&A)[1];
//~^ ERROR constant evaluation error //~^ index out of bounds: the len is 0 but the index is 1
//~| index out of bounds: the len is 0 but the index is 1 //~| ERROR this constant cannot be used
//~| WARN this constant cannot be used
fn main() { fn main() {
let _ = B; let _ = B;

View File

@ -10,9 +10,8 @@
const A: [i32; 0] = []; const A: [i32; 0] = [];
const B: i32 = A[1]; const B: i32 = A[1];
//~^ ERROR constant evaluation error //~^ index out of bounds: the len is 0 but the index is 1
//~| index out of bounds: the len is 0 but the index is 1 //~| ERROR this constant cannot be used
//~| WARN this constant cannot be used
fn main() { fn main() {
let _ = B; let _ = B;

View File

@ -12,9 +12,8 @@
const FOO: &'static[u32] = &[1, 2, 3]; const FOO: &'static[u32] = &[1, 2, 3];
const BAR: u32 = FOO[5]; const BAR: u32 = FOO[5];
//~^ ERROR constant evaluation error [E0080] //~^ index out of bounds: the len is 3 but the index is 5
//~| index out of bounds: the len is 3 but the index is 5 //~| ERROR this constant cannot be used
//~| WARN this constant cannot be used
fn main() { fn main() {
let _ = BAR; let _ = BAR;

View File

@ -12,11 +12,11 @@ enum Test {
DivZero = 1/0, DivZero = 1/0,
//~^ attempt to divide by zero //~^ attempt to divide by zero
//~| ERROR constant evaluation error //~| ERROR constant evaluation error
//~| WARN constant evaluation error //~| ERROR constant evaluation error
RemZero = 1%0, RemZero = 1%0,
//~^ attempt to calculate the remainder with a divisor of zero //~^ attempt to calculate the remainder with a divisor of zero
//~| ERROR constant evaluation error //~| ERROR constant evaluation error
//~| WARN constant evaluation error //~| ERROR constant evaluation error
} }
fn main() {} fn main() {}

View File

@ -13,6 +13,7 @@
// compile-pass // compile-pass
#![allow(warnings)] #![allow(warnings)]
#![warn(const_err)]
fn main() { fn main() {
255u8 + 1; //~ WARNING this expression will panic at run-time 255u8 + 1; //~ WARNING this expression will panic at run-time

View File

@ -11,6 +11,8 @@
// error-pattern:thread 'main' panicked at 'attempt to add with overflow' // error-pattern:thread 'main' panicked at 'attempt to add with overflow'
// compile-flags: -C debug-assertions // compile-flags: -C debug-assertions
#![allow(const_err)]
fn main() { fn main() {
let _x = 200u8 + 200u8 + 200u8; let _x = 200u8 + 200u8 + 200u8;
} }

View File

@ -11,6 +11,8 @@
// error-pattern:thread 'main' panicked at 'attempt to multiply with overflow' // error-pattern:thread 'main' panicked at 'attempt to multiply with overflow'
// compile-flags: -C debug-assertions // compile-flags: -C debug-assertions
#![allow(const_err)]
fn main() { fn main() {
let x = 200u8 * 4; let x = 200u8 * 4;
} }

View File

@ -11,6 +11,8 @@
// error-pattern:thread 'main' panicked at 'attempt to negate with overflow' // error-pattern:thread 'main' panicked at 'attempt to negate with overflow'
// compile-flags: -C debug-assertions // compile-flags: -C debug-assertions
#![allow(const_err)]
fn main() { fn main() {
let _x = -std::i8::MIN; let _x = -std::i8::MIN;
} }

View File

@ -11,6 +11,8 @@
// error-pattern:thread 'main' panicked at 'attempt to subtract with overflow' // error-pattern:thread 'main' panicked at 'attempt to subtract with overflow'
// compile-flags: -C debug-assertions // compile-flags: -C debug-assertions
#![allow(const_err)]
fn main() { fn main() {
let _x = 42u8 - (42u8 + 1); let _x = 42u8 - (42u8 + 1);
} }

View File

@ -11,7 +11,7 @@
// Evaluation of constants in refutable patterns goes through // Evaluation of constants in refutable patterns goes through
// different compiler control-flow paths. // different compiler control-flow paths.
#![allow(unused_imports, warnings)] #![allow(unused_imports, warnings, const_err)]
use std::fmt; use std::fmt;
use std::{i8, i16, i32, i64, isize}; use std::{i8, i16, i32, i64, isize};

View File

@ -22,7 +22,7 @@ use std::{u8, u16, u32, u64, usize};
const A_I8_T const A_I8_T
: [u32; (i8::MAX as i8 + 1i8) as usize] : [u32; (i8::MAX as i8 + 1i8) as usize]
//~^ ERROR E0080 //~^ ERROR E0080
//~| WARN attempt to add with overflow //~| ERROR attempt to add with overflow
= [0; (i8::MAX as usize) + 1]; = [0; (i8::MAX as usize) + 1];
fn main() { fn main() {

View File

@ -1,10 +1,10 @@
warning: attempt to add with overflow error: attempt to add with overflow
--> $DIR/const-eval-overflow-4.rs:23:13 --> $DIR/const-eval-overflow-4.rs:23:13
| |
LL | : [u32; (i8::MAX as i8 + 1i8) as usize] LL | : [u32; (i8::MAX as i8 + 1i8) as usize]
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
| |
= note: #[warn(const_err)] on by default = note: #[deny(const_err)] on by default
error[E0080]: constant evaluation error error[E0080]: constant evaluation error
--> $DIR/const-eval-overflow-4.rs:23:13 --> $DIR/const-eval-overflow-4.rs:23:13
@ -12,6 +12,6 @@ error[E0080]: constant evaluation error
LL | : [u32; (i8::MAX as i8 + 1i8) as usize] LL | : [u32; (i8::MAX as i8 + 1i8) as usize]
| ^^^^^^^^^^^^^^^^^^^^^ attempt to add with overflow | ^^^^^^^^^^^^^^^^^^^^^ attempt to add with overflow
error: aborting due to previous error error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0080`. For more information about this error, try `rustc --explain E0080`.

View File

@ -9,6 +9,7 @@
// except according to those terms. // except according to those terms.
// compile-pass // compile-pass
#![warn(const_err)]
const X: u32 = 5; const X: u32 = 5;
const Y: u32 = 6; const Y: u32 = 6;

View File

@ -1,19 +1,23 @@
warning: attempt to subtract with overflow warning: attempt to subtract with overflow
--> $DIR/conditional_array_execution.rs:15:19 --> $DIR/conditional_array_execution.rs:16:19
| |
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize]; LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
| ^^^^^ | ^^^^^
| |
= note: #[warn(const_err)] on by default note: lint level defined here
--> $DIR/conditional_array_execution.rs:12:9
|
LL | #![warn(const_err)]
| ^^^^^^^^^
warning: this constant cannot be used warning: this constant cannot be used
--> $DIR/conditional_array_execution.rs:15:1 --> $DIR/conditional_array_execution.rs:16:1
| |
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize]; LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
warning: constant evaluation error warning: constant evaluation error
--> $DIR/conditional_array_execution.rs:20:20 --> $DIR/conditional_array_execution.rs:21:20
| |
LL | println!("{}", FOO); LL | println!("{}", FOO);
| ^^^ referenced constant has errors | ^^^ referenced constant has errors

View File

@ -9,6 +9,7 @@
// except according to those terms. // except according to those terms.
// compile-pass // compile-pass
#![warn(const_err)]
#![feature(const_fn)] #![feature(const_fn)]

View File

@ -1,37 +1,41 @@
warning: attempt to subtract with overflow warning: attempt to subtract with overflow
--> $DIR/issue-43197.rs:20:20 --> $DIR/issue-43197.rs:21:20
| |
LL | const X: u32 = 0-1; LL | const X: u32 = 0-1;
| ^^^ | ^^^
| |
= note: #[warn(const_err)] on by default note: lint level defined here
--> $DIR/issue-43197.rs:12:9
|
LL | #![warn(const_err)]
| ^^^^^^^^^
warning: this constant cannot be used warning: this constant cannot be used
--> $DIR/issue-43197.rs:20:5 --> $DIR/issue-43197.rs:21:5
| |
LL | const X: u32 = 0-1; LL | const X: u32 = 0-1;
| ^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow | ^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
warning: attempt to subtract with overflow warning: attempt to subtract with overflow
--> $DIR/issue-43197.rs:23:24 --> $DIR/issue-43197.rs:24:24
| |
LL | const Y: u32 = foo(0-1); LL | const Y: u32 = foo(0-1);
| ^^^ | ^^^
warning: this constant cannot be used warning: this constant cannot be used
--> $DIR/issue-43197.rs:23:5 --> $DIR/issue-43197.rs:24:5
| |
LL | const Y: u32 = foo(0-1); LL | const Y: u32 = foo(0-1);
| ^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow | ^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
warning: constant evaluation error warning: constant evaluation error
--> $DIR/issue-43197.rs:26:23 --> $DIR/issue-43197.rs:27:23
| |
LL | println!("{} {}", X, Y); LL | println!("{} {}", X, Y);
| ^ referenced constant has errors | ^ referenced constant has errors
warning: constant evaluation error warning: constant evaluation error
--> $DIR/issue-43197.rs:26:26 --> $DIR/issue-43197.rs:27:26
| |
LL | println!("{} {}", X, Y); LL | println!("{} {}", X, Y);
| ^ referenced constant has errors | ^ referenced constant has errors

View File

@ -9,6 +9,7 @@
// except according to those terms. // except according to those terms.
// compile-pass // compile-pass
#![warn(const_err)]
trait Foo { trait Foo {
const AMT: usize; const AMT: usize;

View File

@ -1,13 +1,17 @@
warning: constant evaluation error warning: constant evaluation error
--> $DIR/issue-44578.rs:35:20 --> $DIR/issue-44578.rs:36:20
| |
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err LL | println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors | ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
| |
= note: #[warn(const_err)] on by default note: lint level defined here
--> $DIR/issue-44578.rs:12:9
|
LL | #![warn(const_err)]
| ^^^^^^^^^
warning: constant evaluation error warning: constant evaluation error
--> $DIR/issue-44578.rs:35:20 --> $DIR/issue-44578.rs:36:20
| |
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err LL | println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors | ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors

View File

@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
#![warn(const_err)]
// compile-pass // compile-pass
// compile-flags: -O // compile-flags: -O
fn main() { fn main() {

View File

@ -1,49 +1,53 @@
warning: constant evaluation error warning: constant evaluation error
--> $DIR/promoted_errors.rs:14:20 --> $DIR/promoted_errors.rs:16:20
| |
LL | println!("{}", 0u32 - 1); LL | println!("{}", 0u32 - 1);
| ^^^^^^^^ attempt to subtract with overflow | ^^^^^^^^ attempt to subtract with overflow
| |
= note: #[warn(const_err)] on by default note: lint level defined here
--> $DIR/promoted_errors.rs:11:9
|
LL | #![warn(const_err)]
| ^^^^^^^^^
warning: constant evaluation error warning: constant evaluation error
--> $DIR/promoted_errors.rs:14:20 --> $DIR/promoted_errors.rs:16:20
| |
LL | println!("{}", 0u32 - 1); LL | println!("{}", 0u32 - 1);
| ^^^^^^^^ attempt to subtract with overflow | ^^^^^^^^ attempt to subtract with overflow
warning: constant evaluation error warning: constant evaluation error
--> $DIR/promoted_errors.rs:17:14 --> $DIR/promoted_errors.rs:19:14
| |
LL | let _x = 0u32 - 1; LL | let _x = 0u32 - 1;
| ^^^^^^^^ attempt to subtract with overflow | ^^^^^^^^ attempt to subtract with overflow
warning: attempt to divide by zero warning: attempt to divide by zero
--> $DIR/promoted_errors.rs:19:20 --> $DIR/promoted_errors.rs:21:20
| |
LL | println!("{}", 1/(1-1)); LL | println!("{}", 1/(1-1));
| ^^^^^^^ | ^^^^^^^
warning: constant evaluation error warning: constant evaluation error
--> $DIR/promoted_errors.rs:19:20 --> $DIR/promoted_errors.rs:21:20
| |
LL | println!("{}", 1/(1-1)); LL | println!("{}", 1/(1-1));
| ^^^^^^^ attempt to divide by zero | ^^^^^^^ attempt to divide by zero
warning: attempt to divide by zero warning: attempt to divide by zero
--> $DIR/promoted_errors.rs:22:14 --> $DIR/promoted_errors.rs:24:14
| |
LL | let _x = 1/(1-1); LL | let _x = 1/(1-1);
| ^^^^^^^ | ^^^^^^^
warning: constant evaluation error warning: constant evaluation error
--> $DIR/promoted_errors.rs:22:14 --> $DIR/promoted_errors.rs:24:14
| |
LL | let _x = 1/(1-1); LL | let _x = 1/(1-1);
| ^^^^^^^ attempt to divide by zero | ^^^^^^^ attempt to divide by zero
warning: constant evaluation error warning: constant evaluation error
--> $DIR/promoted_errors.rs:25:20 --> $DIR/promoted_errors.rs:27:20
| |
LL | println!("{}", 1/(false as u32)); LL | println!("{}", 1/(false as u32));
| ^^^^^^^^^^^^^^^^ attempt to divide by zero | ^^^^^^^^^^^^^^^^ attempt to divide by zero

View File

@ -9,6 +9,7 @@
// except according to those terms. // except according to those terms.
// compile-pass // compile-pass
#![warn(const_err)]
#![crate_type = "lib"] #![crate_type = "lib"]

View File

@ -1,25 +1,29 @@
warning: attempt to subtract with overflow warning: attempt to subtract with overflow
--> $DIR/pub_const_err.rs:15:20 --> $DIR/pub_const_err.rs:16:20
| |
LL | pub const Z: u32 = 0 - 1; LL | pub const Z: u32 = 0 - 1;
| ^^^^^ | ^^^^^
| |
= note: #[warn(const_err)] on by default note: lint level defined here
--> $DIR/pub_const_err.rs:12:9
|
LL | #![warn(const_err)]
| ^^^^^^^^^
warning: this constant cannot be used warning: this constant cannot be used
--> $DIR/pub_const_err.rs:15:1 --> $DIR/pub_const_err.rs:16:1
| |
LL | pub const Z: u32 = 0 - 1; LL | pub const Z: u32 = 0 - 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow | ^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
warning: attempt to subtract with overflow warning: attempt to subtract with overflow
--> $DIR/pub_const_err.rs:19:22 --> $DIR/pub_const_err.rs:20:22
| |
LL | pub type Foo = [i32; 0 - 1]; LL | pub type Foo = [i32; 0 - 1];
| ^^^^^ | ^^^^^
warning: this array length cannot be used warning: this array length cannot be used
--> $DIR/pub_const_err.rs:19:22 --> $DIR/pub_const_err.rs:20:22
| |
LL | pub type Foo = [i32; 0 - 1]; LL | pub type Foo = [i32; 0 - 1];
| ^^^^^ attempt to subtract with overflow | ^^^^^ attempt to subtract with overflow

View File

@ -9,6 +9,7 @@
// except according to those terms. // except according to those terms.
// compile-pass // compile-pass
#![warn(const_err)]
pub const Z: u32 = 0 - 1; pub const Z: u32 = 0 - 1;
//~^ WARN attempt to subtract with overflow //~^ WARN attempt to subtract with overflow

View File

@ -1,25 +1,29 @@
warning: attempt to subtract with overflow warning: attempt to subtract with overflow
--> $DIR/pub_const_err_bin.rs:13:20 --> $DIR/pub_const_err_bin.rs:14:20
| |
LL | pub const Z: u32 = 0 - 1; LL | pub const Z: u32 = 0 - 1;
| ^^^^^ | ^^^^^
| |
= note: #[warn(const_err)] on by default note: lint level defined here
--> $DIR/pub_const_err_bin.rs:12:9
|
LL | #![warn(const_err)]
| ^^^^^^^^^
warning: this constant cannot be used warning: this constant cannot be used
--> $DIR/pub_const_err_bin.rs:13:1 --> $DIR/pub_const_err_bin.rs:14:1
| |
LL | pub const Z: u32 = 0 - 1; LL | pub const Z: u32 = 0 - 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow | ^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
warning: attempt to subtract with overflow warning: attempt to subtract with overflow
--> $DIR/pub_const_err_bin.rs:17:22 --> $DIR/pub_const_err_bin.rs:18:22
| |
LL | pub type Foo = [i32; 0 - 1]; LL | pub type Foo = [i32; 0 - 1];
| ^^^^^ | ^^^^^
warning: this array length cannot be used warning: this array length cannot be used
--> $DIR/pub_const_err_bin.rs:17:22 --> $DIR/pub_const_err_bin.rs:18:22
| |
LL | pub type Foo = [i32; 0 - 1]; LL | pub type Foo = [i32; 0 - 1];
| ^^^^^ attempt to subtract with overflow | ^^^^^ attempt to subtract with overflow

View File

@ -16,7 +16,7 @@ const ONE: usize = 1;
const TWO: usize = 2; const TWO: usize = 2;
const LEN: usize = ONE - TWO; const LEN: usize = ONE - TWO;
//~^ ERROR E0080 //~^ ERROR E0080
//~| WARN attempt to subtract with overflow //~| ERROR attempt to subtract with overflow
fn main() { fn main() {
let a: [i8; LEN] = unimplemented!(); let a: [i8; LEN] = unimplemented!();

View File

@ -1,10 +1,10 @@
warning: attempt to subtract with overflow error: attempt to subtract with overflow
--> $DIR/const-len-underflow-separate-spans.rs:17:20 --> $DIR/const-len-underflow-separate-spans.rs:17:20
| |
LL | const LEN: usize = ONE - TWO; LL | const LEN: usize = ONE - TWO;
| ^^^^^^^^^ | ^^^^^^^^^
| |
= note: #[warn(const_err)] on by default = note: #[deny(const_err)] on by default
error[E0080]: constant evaluation error error[E0080]: constant evaluation error
--> $DIR/const-len-underflow-separate-spans.rs:17:20 --> $DIR/const-len-underflow-separate-spans.rs:17:20
@ -18,6 +18,6 @@ error[E0080]: constant evaluation error
LL | let a: [i8; LEN] = unimplemented!(); LL | let a: [i8; LEN] = unimplemented!();
| ^^^ referenced constant has errors | ^^^ referenced constant has errors
error: aborting due to 2 previous errors error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0080`. For more information about this error, try `rustc --explain E0080`.

View File

@ -12,15 +12,15 @@ error[E0080]: constant evaluation error
LL | X = (1 << 500), //~ ERROR E0080 LL | X = (1 << 500), //~ ERROR E0080
| ^^^^^^^^^^ attempt to shift left with overflow | ^^^^^^^^^^ attempt to shift left with overflow
warning: attempt to divide by zero error: attempt to divide by zero
--> $DIR/E0080.rs:14:9 --> $DIR/E0080.rs:14:9
| |
LL | Y = (1 / 0) //~ ERROR E0080 LL | Y = (1 / 0) //~ ERROR E0080
| ^^^^^^^ | ^^^^^^^
| |
= note: #[warn(const_err)] on by default = note: #[deny(const_err)] on by default
warning: constant evaluation error error: constant evaluation error
--> $DIR/E0080.rs:14:9 --> $DIR/E0080.rs:14:9
| |
LL | Y = (1 / 0) //~ ERROR E0080 LL | Y = (1 / 0) //~ ERROR E0080
@ -32,6 +32,6 @@ error[E0080]: constant evaluation error
LL | Y = (1 / 0) //~ ERROR E0080 LL | Y = (1 / 0) //~ ERROR E0080
| ^^^^^^^ attempt to divide by zero | ^^^^^^^ attempt to divide by zero
error: aborting due to 3 previous errors error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0080`. For more information about this error, try `rustc --explain E0080`.