Skip to content

Commit cd6c186

Browse files
committed
Warn on all erroneous constants
1 parent 52ed3d8 commit cd6c186

18 files changed

+233
-75
lines changed

src/librustc_lint/builtin.rs

+68
Original file line numberDiff line numberDiff line change
@@ -1441,3 +1441,71 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeAliasBounds {
14411441
}
14421442
}
14431443
}
1444+
1445+
/// Lint constants that are erroneous.
1446+
/// Without this lint, we might not get any diagnostic if the constant is
1447+
/// unused within this crate, even though downstream crates can't use it
1448+
/// without producing an error.
1449+
pub struct UnusedBrokenConst;
1450+
1451+
impl LintPass for UnusedBrokenConst {
1452+
fn get_lints(&self) -> LintArray {
1453+
lint_array!()
1454+
}
1455+
}
1456+
1457+
fn check_const(cx: &LateContext, body_id: hir::BodyId, what: &str) {
1458+
let def_id = cx.tcx.hir.body_owner_def_id(body_id);
1459+
let param_env = cx.tcx.param_env(def_id);
1460+
let cid = ::rustc::mir::interpret::GlobalId {
1461+
instance: ty::Instance::mono(cx.tcx, def_id),
1462+
promoted: None
1463+
};
1464+
if let Err(err) = cx.tcx.const_eval(param_env.and(cid)) {
1465+
let span = cx.tcx.def_span(def_id);
1466+
let mut diag = cx.struct_span_lint(
1467+
CONST_ERR,
1468+
span,
1469+
&format!("this {} cannot be used", what),
1470+
);
1471+
use rustc::middle::const_val::ConstEvalErrDescription;
1472+
match err.description() {
1473+
ConstEvalErrDescription::Simple(message) => {
1474+
diag.span_label(span, message);
1475+
}
1476+
ConstEvalErrDescription::Backtrace(miri, frames) => {
1477+
diag.span_label(span, format!("{}", miri));
1478+
for frame in frames {
1479+
diag.span_label(frame.span, format!("inside call to `{}`", frame.location));
1480+
}
1481+
}
1482+
}
1483+
diag.emit()
1484+
}
1485+
}
1486+
1487+
struct UnusedBrokenConstVisitor<'a, 'tcx: 'a>(&'a LateContext<'a, 'tcx>);
1488+
1489+
impl<'a, 'tcx, 'v> hir::intravisit::Visitor<'v> for UnusedBrokenConstVisitor<'a, 'tcx> {
1490+
fn visit_nested_body(&mut self, id: hir::BodyId) {
1491+
check_const(self.0, id, "array length");
1492+
}
1493+
fn nested_visit_map<'this>(&'this mut self) -> hir::intravisit::NestedVisitorMap<'this, 'v> {
1494+
hir::intravisit::NestedVisitorMap::None
1495+
}
1496+
}
1497+
1498+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedBrokenConst {
1499+
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
1500+
match it.node {
1501+
hir::ItemConst(_, body_id) => {
1502+
check_const(cx, body_id, "constant");
1503+
},
1504+
hir::ItemTy(ref ty, _) => hir::intravisit::walk_ty(
1505+
&mut UnusedBrokenConstVisitor(cx),
1506+
ty
1507+
),
1508+
_ => {},
1509+
}
1510+
}
1511+
}

src/librustc_lint/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
136136
UnionsWithDropFields,
137137
UnreachablePub,
138138
TypeAliasBounds,
139+
UnusedBrokenConst,
139140
);
140141

141142
add_builtin_with_new!(sess,

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

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const A: &'static [i32] = &[];
1212
const B: i32 = (&A)[1];
1313
//~^ ERROR constant evaluation error
1414
//~| index out of bounds: the len is 0 but the index is 1
15+
//~| WARN this constant cannot be used
1516

1617
fn main() {
1718
let _ = B;

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

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const A: [i32; 0] = [];
1212
const B: i32 = A[1];
1313
//~^ ERROR constant evaluation error
1414
//~| index out of bounds: the len is 0 but the index is 1
15+
//~| WARN this constant cannot be used
1516

1617
fn main() {
1718
let _ = B;

src/test/compile-fail/const-err-early.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010

1111
#![deny(const_err)]
1212

13-
pub const A: i8 = -std::i8::MIN; //~ ERROR E0080
14-
//~^ ERROR attempt to negate with overflow
13+
pub const A: i8 = -std::i8::MIN; //~ ERROR const_err
14+
//~^ ERROR this constant cannot be used
1515
//~| ERROR constant evaluation error
16-
pub const B: u8 = 200u8 + 200u8; //~ ERROR E0080
17-
//~^ ERROR attempt to add with overflow
18-
pub const C: u8 = 200u8 * 4; //~ ERROR E0080
19-
//~^ ERROR attempt to multiply with overflow
20-
pub const D: u8 = 42u8 - (42u8 + 1); //~ ERROR E0080
21-
//~^ ERROR attempt to subtract with overflow
16+
pub const B: u8 = 200u8 + 200u8; //~ ERROR const_err
17+
//~^ ERROR this constant cannot be used
18+
pub const C: u8 = 200u8 * 4; //~ ERROR const_err
19+
//~^ ERROR this constant cannot be used
20+
pub const D: u8 = 42u8 - (42u8 + 1); //~ ERROR const_err
21+
//~^ ERROR this constant cannot be used
2222
pub const E: u8 = [5u8][1];
23-
//~^ ERROR E0080
23+
//~^ ERROR const_err
2424

2525
fn main() {
2626
let _a = A;

src/test/compile-fail/const-err-multi.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ pub const A: i8 = -std::i8::MIN;
1414
//~^ ERROR E0080
1515
//~| ERROR attempt to negate with overflow
1616
//~| ERROR constant evaluation error
17+
//~| ERROR this constant cannot be used
1718
pub const B: i8 = A;
18-
//~^ ERROR E0080
19+
//~^ ERROR const_err
1920
pub const C: u8 = A as u8;
20-
//~^ ERROR E0080
21+
//~^ ERROR const_err
2122
pub const D: i8 = 50 - A;
22-
//~^ ERROR E0080
23+
//~^ ERROR const_err
2324

2425
fn main() {
2526
let _ = (A, B, C, D);

src/test/compile-fail/const-eval-overflow2.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -22,57 +22,57 @@ use std::{i8, i16, i32, i64, isize};
2222
use std::{u8, u16, u32, u64, usize};
2323

2424
const VALS_I8: (i8,) =
25+
//~^ ERROR this constant cannot be used
2526
(
2627
i8::MIN - 1,
27-
//~^ ERROR constant evaluation error
28-
//~| ERROR attempt to subtract with overflow
28+
//~^ ERROR attempt to subtract with overflow
2929
);
3030

3131
const VALS_I16: (i16,) =
32+
//~^ ERROR this constant cannot be used
3233
(
3334
i16::MIN - 1,
34-
//~^ ERROR constant evaluation error
35-
//~| ERROR attempt to subtract with overflow
35+
//~^ ERROR attempt to subtract with overflow
3636
);
3737

3838
const VALS_I32: (i32,) =
39+
//~^ ERROR this constant cannot be used
3940
(
4041
i32::MIN - 1,
41-
//~^ ERROR constant evaluation error
42-
//~| ERROR attempt to subtract with overflow
42+
//~^ ERROR attempt to subtract with overflow
4343
);
4444

4545
const VALS_I64: (i64,) =
46+
//~^ ERROR this constant cannot be used
4647
(
4748
i64::MIN - 1,
48-
//~^ ERROR constant evaluation error
49-
//~| ERROR attempt to subtract with overflow
49+
//~^ ERROR attempt to subtract with overflow
5050
);
5151

5252
const VALS_U8: (u8,) =
53+
//~^ ERROR this constant cannot be used
5354
(
5455
u8::MIN - 1,
55-
//~^ ERROR constant evaluation error
56-
//~| ERROR attempt to subtract with overflow
56+
//~^ ERROR attempt to subtract with overflow
5757
);
5858

5959
const VALS_U16: (u16,) = (
60+
//~^ ERROR this constant cannot be used
6061
u16::MIN - 1,
61-
//~^ ERROR constant evaluation error
62-
//~| ERROR attempt to subtract with overflow
62+
//~^ ERROR attempt to subtract with overflow
6363
);
6464

6565
const VALS_U32: (u32,) = (
66+
//~^ ERROR this constant cannot be used
6667
u32::MIN - 1,
67-
//~^ ERROR constant evaluation error
68-
//~| ERROR attempt to subtract with overflow
68+
//~^ ERROR attempt to subtract with overflow
6969
);
7070

7171
const VALS_U64: (u64,) =
72+
//~^ ERROR this constant cannot be used
7273
(
7374
u64::MIN - 1,
74-
//~^ ERROR constant evaluation error
75-
//~| ERROR attempt to subtract with overflow
75+
//~^ ERROR attempt to subtract with overflow
7676
);
7777

7878
fn main() {

src/test/compile-fail/const-eval-overflow2b.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -22,57 +22,57 @@ use std::{i8, i16, i32, i64, isize};
2222
use std::{u8, u16, u32, u64, usize};
2323

2424
const VALS_I8: (i8,) =
25+
//~^ ERROR this constant cannot be used
2526
(
2627
i8::MAX + 1,
27-
//~^ ERROR constant evaluation error
28-
//~| ERROR attempt to add with overflow
28+
//~^ ERROR attempt to add with overflow
2929
);
3030

3131
const VALS_I16: (i16,) =
32+
//~^ ERROR this constant cannot be used
3233
(
3334
i16::MAX + 1,
34-
//~^ ERROR constant evaluation error
35-
//~| ERROR attempt to add with overflow
35+
//~^ ERROR attempt to add with overflow
3636
);
3737

3838
const VALS_I32: (i32,) =
39+
//~^ ERROR this constant cannot be used
3940
(
4041
i32::MAX + 1,
41-
//~^ ERROR constant evaluation error
42-
//~| ERROR attempt to add with overflow
42+
//~^ ERROR attempt to add with overflow
4343
);
4444

4545
const VALS_I64: (i64,) =
46+
//~^ ERROR this constant cannot be used
4647
(
4748
i64::MAX + 1,
48-
//~^ ERROR constant evaluation error
49-
//~| ERROR attempt to add with overflow
49+
//~^ ERROR attempt to add with overflow
5050
);
5151

5252
const VALS_U8: (u8,) =
53+
//~^ ERROR this constant cannot be used
5354
(
5455
u8::MAX + 1,
55-
//~^ ERROR constant evaluation error
56-
//~| ERROR attempt to add with overflow
56+
//~^ ERROR attempt to add with overflow
5757
);
5858

5959
const VALS_U16: (u16,) = (
60+
//~^ ERROR this constant cannot be used
6061
u16::MAX + 1,
61-
//~^ ERROR constant evaluation error
62-
//~| ERROR attempt to add with overflow
62+
//~^ ERROR attempt to add with overflow
6363
);
6464

6565
const VALS_U32: (u32,) = (
66+
//~^ ERROR this constant cannot be used
6667
u32::MAX + 1,
67-
//~^ ERROR constant evaluation error
68-
//~| ERROR attempt to add with overflow
68+
//~^ ERROR attempt to add with overflow
6969
);
7070

7171
const VALS_U64: (u64,) =
72+
//~^ ERROR this constant cannot be used
7273
(
7374
u64::MAX + 1,
74-
//~^ ERROR constant evaluation error
75-
//~| ERROR attempt to add with overflow
75+
//~^ ERROR attempt to add with overflow
7676
);
7777

7878
fn main() {

src/test/compile-fail/const-eval-overflow2c.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -22,57 +22,57 @@ use std::{i8, i16, i32, i64, isize};
2222
use std::{u8, u16, u32, u64, usize};
2323

2424
const VALS_I8: (i8,) =
25+
//~^ ERROR this constant cannot be used
2526
(
2627
i8::MIN * 2,
27-
//~^ ERROR constant evaluation error
28-
//~| ERROR attempt to multiply with overflow
28+
//~^ ERROR attempt to multiply with overflow
2929
);
3030

3131
const VALS_I16: (i16,) =
32+
//~^ ERROR this constant cannot be used
3233
(
3334
i16::MIN * 2,
34-
//~^ ERROR constant evaluation error
35-
//~| ERROR attempt to multiply with overflow
35+
//~^ ERROR attempt to multiply with overflow
3636
);
3737

3838
const VALS_I32: (i32,) =
39+
//~^ ERROR this constant cannot be used
3940
(
4041
i32::MIN * 2,
41-
//~^ ERROR constant evaluation error
42-
//~| ERROR attempt to multiply with overflow
42+
//~^ ERROR attempt to multiply with overflow
4343
);
4444

4545
const VALS_I64: (i64,) =
46+
//~^ ERROR this constant cannot be used
4647
(
4748
i64::MIN * 2,
48-
//~^ ERROR constant evaluation error
49-
//~| ERROR attempt to multiply with overflow
49+
//~^ ERROR attempt to multiply with overflow
5050
);
5151

5252
const VALS_U8: (u8,) =
53+
//~^ ERROR this constant cannot be used
5354
(
5455
u8::MAX * 2,
55-
//~^ ERROR constant evaluation error
56-
//~| ERROR attempt to multiply with overflow
56+
//~^ ERROR attempt to multiply with overflow
5757
);
5858

5959
const VALS_U16: (u16,) = (
60+
//~^ ERROR this constant cannot be used
6061
u16::MAX * 2,
61-
//~^ ERROR constant evaluation error
62-
//~| ERROR attempt to multiply with overflow
62+
//~^ ERROR attempt to multiply with overflow
6363
);
6464

6565
const VALS_U32: (u32,) = (
66+
//~^ ERROR this constant cannot be used
6667
u32::MAX * 2,
67-
//~^ ERROR constant evaluation error
68-
//~| ERROR attempt to multiply with overflow
68+
//~^ ERROR attempt to multiply with overflow
6969
);
7070

7171
const VALS_U64: (u64,) =
72+
//~^ ERROR this constant cannot be used
7273
(
7374
u64::MAX * 2,
74-
//~^ ERROR constant evaluation error
75-
//~| ERROR attempt to multiply with overflow
75+
//~^ ERROR attempt to multiply with overflow
7676
);
7777

7878
fn main() {

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

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const FOO: &'static[u32] = &[1, 2, 3];
1414
const BAR: u32 = FOO[5];
1515
//~^ ERROR constant evaluation error [E0080]
1616
//~| index out of bounds: the len is 3 but the index is 5
17+
//~| WARN this constant cannot be used
1718

1819
fn main() {
1920
let _ = BAR;

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

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const X: u32 = 5;
1414
const Y: u32 = 6;
1515
const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
1616
//~^ WARN attempt to subtract with overflow
17+
//~| WARN this constant cannot be used
1718

1819
fn main() {
1920
println!("{}", FOO);

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
66
|
77
= note: #[warn(const_err)] on by default
88

9+
warning: this constant cannot be used
10+
--> $DIR/conditional_array_execution.rs:15:1
11+
|
12+
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
14+
915
warning: constant evaluation error
10-
--> $DIR/conditional_array_execution.rs:19:20
16+
--> $DIR/conditional_array_execution.rs:20:20
1117
|
1218
LL | println!("{}", FOO);
1319
| ^^^ referenced constant has errors

0 commit comments

Comments
 (0)