Skip to content

Commit a722296

Browse files
committed
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.
2 parents ba64edb + 1788af3 commit a722296

28 files changed

+95
-58
lines changed

src/librustc/lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ declare_lint! {
2727

2828
declare_lint! {
2929
pub CONST_ERR,
30-
Warn,
30+
Deny,
3131
"constant evaluation detected erroneous expression"
3232
}
3333

src/test/compile-fail/array_const_index-0.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
const A: &'static [i32] = &[];
1212
const B: i32 = (&A)[1];
13-
//~^ ERROR constant evaluation error
14-
//~| index out of bounds: the len is 0 but the index is 1
15-
//~| WARN this constant cannot be used
13+
//~^ index out of bounds: the len is 0 but the index is 1
14+
//~| ERROR this constant cannot be used
1615

1716
fn main() {
1817
let _ = B;

src/test/compile-fail/array_const_index-1.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
const A: [i32; 0] = [];
1212
const B: i32 = A[1];
13-
//~^ ERROR constant evaluation error
14-
//~| index out of bounds: the len is 0 but the index is 1
15-
//~| WARN this constant cannot be used
13+
//~^ index out of bounds: the len is 0 but the index is 1
14+
//~| ERROR this constant cannot be used
1615

1716
fn main() {
1817
let _ = B;

src/test/compile-fail/const-slice-oob.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212

1313
const FOO: &'static[u32] = &[1, 2, 3];
1414
const BAR: u32 = FOO[5];
15-
//~^ ERROR constant evaluation error [E0080]
16-
//~| index out of bounds: the len is 3 but the index is 5
17-
//~| WARN this constant cannot be used
15+
//~^ index out of bounds: the len is 3 but the index is 5
16+
//~| ERROR this constant cannot be used
1817

1918
fn main() {
2019
let _ = BAR;

src/test/compile-fail/eval-enum.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ enum Test {
1212
DivZero = 1/0,
1313
//~^ attempt to divide by zero
1414
//~| ERROR constant evaluation error
15-
//~| WARN constant evaluation error
15+
//~| ERROR constant evaluation error
1616
RemZero = 1%0,
1717
//~^ attempt to calculate the remainder with a divisor of zero
1818
//~| ERROR constant evaluation error
19-
//~| WARN constant evaluation error
19+
//~| ERROR constant evaluation error
2020
}
2121

2222
fn main() {}

src/test/incremental/warnings-reemitted.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// compile-pass
1414

1515
#![allow(warnings)]
16+
#![warn(const_err)]
1617

1718
fn main() {
1819
255u8 + 1; //~ WARNING this expression will panic at run-time

src/test/run-fail/overflowing-add.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// error-pattern:thread 'main' panicked at 'attempt to add with overflow'
1212
// compile-flags: -C debug-assertions
1313

14+
#![allow(const_err)]
15+
1416
fn main() {
1517
let _x = 200u8 + 200u8 + 200u8;
1618
}

src/test/run-fail/overflowing-mul.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// error-pattern:thread 'main' panicked at 'attempt to multiply with overflow'
1212
// compile-flags: -C debug-assertions
1313

14+
#![allow(const_err)]
15+
1416
fn main() {
1517
let x = 200u8 * 4;
1618
}

src/test/run-fail/overflowing-neg.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// error-pattern:thread 'main' panicked at 'attempt to negate with overflow'
1212
// compile-flags: -C debug-assertions
1313

14+
#![allow(const_err)]
15+
1416
fn main() {
1517
let _x = -std::i8::MIN;
1618
}

src/test/run-fail/overflowing-sub.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// error-pattern:thread 'main' panicked at 'attempt to subtract with overflow'
1212
// compile-flags: -C debug-assertions
1313

14+
#![allow(const_err)]
15+
1416
fn main() {
1517
let _x = 42u8 - (42u8 + 1);
1618
}

src/test/ui/const-eval-overflow-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// Evaluation of constants in refutable patterns goes through
1212
// different compiler control-flow paths.
1313

14-
#![allow(unused_imports, warnings)]
14+
#![allow(unused_imports, warnings, const_err)]
1515

1616
use std::fmt;
1717
use std::{i8, i16, i32, i64, isize};

src/test/ui/const-eval-overflow-4.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::{u8, u16, u32, u64, usize};
2222
const A_I8_T
2323
: [u32; (i8::MAX as i8 + 1i8) as usize]
2424
//~^ ERROR E0080
25-
//~| WARN attempt to add with overflow
25+
//~| ERROR attempt to add with overflow
2626
= [0; (i8::MAX as usize) + 1];
2727

2828
fn main() {
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
warning: attempt to add with overflow
1+
error: attempt to add with overflow
22
--> $DIR/const-eval-overflow-4.rs:23:13
33
|
44
LL | : [u32; (i8::MAX as i8 + 1i8) as usize]
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: #[warn(const_err)] on by default
7+
= note: #[deny(const_err)] on by default
88

99
error[E0080]: constant evaluation error
1010
--> $DIR/const-eval-overflow-4.rs:23:13
1111
|
1212
LL | : [u32; (i8::MAX as i8 + 1i8) as usize]
1313
| ^^^^^^^^^^^^^^^^^^^^^ attempt to add with overflow
1414

15-
error: aborting due to previous error
15+
error: aborting due to 2 previous errors
1616

1717
For more information about this error, try `rustc --explain E0080`.

src/test/ui/const-eval/conditional_array_execution.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
// compile-pass
12+
#![warn(const_err)]
1213

1314
const X: u32 = 5;
1415
const Y: u32 = 6;

src/test/ui/const-eval/conditional_array_execution.stderr

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
warning: attempt to subtract with overflow
2-
--> $DIR/conditional_array_execution.rs:15:19
2+
--> $DIR/conditional_array_execution.rs:16:19
33
|
44
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
55
| ^^^^^
66
|
7-
= note: #[warn(const_err)] on by default
7+
note: lint level defined here
8+
--> $DIR/conditional_array_execution.rs:12:9
9+
|
10+
LL | #![warn(const_err)]
11+
| ^^^^^^^^^
812

913
warning: this constant cannot be used
10-
--> $DIR/conditional_array_execution.rs:15:1
14+
--> $DIR/conditional_array_execution.rs:16:1
1115
|
1216
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
1317
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
1418

1519
warning: constant evaluation error
16-
--> $DIR/conditional_array_execution.rs:20:20
20+
--> $DIR/conditional_array_execution.rs:21:20
1721
|
1822
LL | println!("{}", FOO);
1923
| ^^^ referenced constant has errors

src/test/ui/const-eval/issue-43197.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
// compile-pass
12+
#![warn(const_err)]
1213

1314
#![feature(const_fn)]
1415

src/test/ui/const-eval/issue-43197.stderr

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
warning: attempt to subtract with overflow
2-
--> $DIR/issue-43197.rs:20:20
2+
--> $DIR/issue-43197.rs:21:20
33
|
44
LL | const X: u32 = 0-1;
55
| ^^^
66
|
7-
= note: #[warn(const_err)] on by default
7+
note: lint level defined here
8+
--> $DIR/issue-43197.rs:12:9
9+
|
10+
LL | #![warn(const_err)]
11+
| ^^^^^^^^^
812

913
warning: this constant cannot be used
10-
--> $DIR/issue-43197.rs:20:5
14+
--> $DIR/issue-43197.rs:21:5
1115
|
1216
LL | const X: u32 = 0-1;
1317
| ^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
1418

1519
warning: attempt to subtract with overflow
16-
--> $DIR/issue-43197.rs:23:24
20+
--> $DIR/issue-43197.rs:24:24
1721
|
1822
LL | const Y: u32 = foo(0-1);
1923
| ^^^
2024

2125
warning: this constant cannot be used
22-
--> $DIR/issue-43197.rs:23:5
26+
--> $DIR/issue-43197.rs:24:5
2327
|
2428
LL | const Y: u32 = foo(0-1);
2529
| ^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
2630

2731
warning: constant evaluation error
28-
--> $DIR/issue-43197.rs:26:23
32+
--> $DIR/issue-43197.rs:27:23
2933
|
3034
LL | println!("{} {}", X, Y);
3135
| ^ referenced constant has errors
3236

3337
warning: constant evaluation error
34-
--> $DIR/issue-43197.rs:26:26
38+
--> $DIR/issue-43197.rs:27:26
3539
|
3640
LL | println!("{} {}", X, Y);
3741
| ^ referenced constant has errors

src/test/ui/const-eval/issue-44578.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
// compile-pass
12+
#![warn(const_err)]
1213

1314
trait Foo {
1415
const AMT: usize;

src/test/ui/const-eval/issue-44578.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
warning: constant evaluation error
2-
--> $DIR/issue-44578.rs:35:20
2+
--> $DIR/issue-44578.rs:36:20
33
|
44
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
66
|
7-
= note: #[warn(const_err)] on by default
7+
note: lint level defined here
8+
--> $DIR/issue-44578.rs:12:9
9+
|
10+
LL | #![warn(const_err)]
11+
| ^^^^^^^^^
812

913
warning: constant evaluation error
10-
--> $DIR/issue-44578.rs:35:20
14+
--> $DIR/issue-44578.rs:36:20
1115
|
1216
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
1317
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors

src/test/ui/const-eval/promoted_errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![warn(const_err)]
12+
1113
// compile-pass
1214
// compile-flags: -O
1315
fn main() {

src/test/ui/const-eval/promoted_errors.stderr

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,53 @@
11
warning: constant evaluation error
2-
--> $DIR/promoted_errors.rs:14:20
2+
--> $DIR/promoted_errors.rs:16:20
33
|
44
LL | println!("{}", 0u32 - 1);
55
| ^^^^^^^^ attempt to subtract with overflow
66
|
7-
= note: #[warn(const_err)] on by default
7+
note: lint level defined here
8+
--> $DIR/promoted_errors.rs:11:9
9+
|
10+
LL | #![warn(const_err)]
11+
| ^^^^^^^^^
812

913
warning: constant evaluation error
10-
--> $DIR/promoted_errors.rs:14:20
14+
--> $DIR/promoted_errors.rs:16:20
1115
|
1216
LL | println!("{}", 0u32 - 1);
1317
| ^^^^^^^^ attempt to subtract with overflow
1418

1519
warning: constant evaluation error
16-
--> $DIR/promoted_errors.rs:17:14
20+
--> $DIR/promoted_errors.rs:19:14
1721
|
1822
LL | let _x = 0u32 - 1;
1923
| ^^^^^^^^ attempt to subtract with overflow
2024

2125
warning: attempt to divide by zero
22-
--> $DIR/promoted_errors.rs:19:20
26+
--> $DIR/promoted_errors.rs:21:20
2327
|
2428
LL | println!("{}", 1/(1-1));
2529
| ^^^^^^^
2630

2731
warning: constant evaluation error
28-
--> $DIR/promoted_errors.rs:19:20
32+
--> $DIR/promoted_errors.rs:21:20
2933
|
3034
LL | println!("{}", 1/(1-1));
3135
| ^^^^^^^ attempt to divide by zero
3236

3337
warning: attempt to divide by zero
34-
--> $DIR/promoted_errors.rs:22:14
38+
--> $DIR/promoted_errors.rs:24:14
3539
|
3640
LL | let _x = 1/(1-1);
3741
| ^^^^^^^
3842

3943
warning: constant evaluation error
40-
--> $DIR/promoted_errors.rs:22:14
44+
--> $DIR/promoted_errors.rs:24:14
4145
|
4246
LL | let _x = 1/(1-1);
4347
| ^^^^^^^ attempt to divide by zero
4448

4549
warning: constant evaluation error
46-
--> $DIR/promoted_errors.rs:25:20
50+
--> $DIR/promoted_errors.rs:27:20
4751
|
4852
LL | println!("{}", 1/(false as u32));
4953
| ^^^^^^^^^^^^^^^^ attempt to divide by zero

src/test/ui/const-eval/pub_const_err.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
// compile-pass
12+
#![warn(const_err)]
1213

1314
#![crate_type = "lib"]
1415

src/test/ui/const-eval/pub_const_err.stderr

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
warning: attempt to subtract with overflow
2-
--> $DIR/pub_const_err.rs:15:20
2+
--> $DIR/pub_const_err.rs:16:20
33
|
44
LL | pub const Z: u32 = 0 - 1;
55
| ^^^^^
66
|
7-
= note: #[warn(const_err)] on by default
7+
note: lint level defined here
8+
--> $DIR/pub_const_err.rs:12:9
9+
|
10+
LL | #![warn(const_err)]
11+
| ^^^^^^^^^
812

913
warning: this constant cannot be used
10-
--> $DIR/pub_const_err.rs:15:1
14+
--> $DIR/pub_const_err.rs:16:1
1115
|
1216
LL | pub const Z: u32 = 0 - 1;
1317
| ^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
1418

1519
warning: attempt to subtract with overflow
16-
--> $DIR/pub_const_err.rs:19:22
20+
--> $DIR/pub_const_err.rs:20:22
1721
|
1822
LL | pub type Foo = [i32; 0 - 1];
1923
| ^^^^^
2024

2125
warning: this array length cannot be used
22-
--> $DIR/pub_const_err.rs:19:22
26+
--> $DIR/pub_const_err.rs:20:22
2327
|
2428
LL | pub type Foo = [i32; 0 - 1];
2529
| ^^^^^ attempt to subtract with overflow

src/test/ui/const-eval/pub_const_err_bin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
// compile-pass
12+
#![warn(const_err)]
1213

1314
pub const Z: u32 = 0 - 1;
1415
//~^ WARN attempt to subtract with overflow

0 commit comments

Comments
 (0)