Skip to content

Commit 4d47330

Browse files
committed
Turn the use of erroneous constants into errors again
1 parent 4eea1a4 commit 4d47330

13 files changed

+45
-42
lines changed

src/librustc/mir/interpret/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
8080
::mir::interpret::EvalErrorKind::CheckMatchError |
8181
::mir::interpret::EvalErrorKind::Layout(_) => return None,
8282
::mir::interpret::EvalErrorKind::ReferencedConstant(ref inner) => {
83-
inner.struct_generic(tcx, "referenced constant", lint_root)?.emit();
83+
inner.struct_generic(tcx, "referenced constant has errors", lint_root)?.emit();
8484
},
8585
_ => {},
8686
}

src/librustc_mir/transform/const_prop.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,23 @@ impl<'b, 'a, 'tcx:'b> ConstPropagator<'b, 'a, 'tcx> {
170170
) -> Option<Const<'tcx>> {
171171
match c.literal {
172172
Literal::Value { value } => {
173-
let v = self.use_ecx(source_info, |this| {
174-
this.ecx.const_to_value(value.val)
175-
})?;
176-
Some((v, value.ty, c.span))
173+
self.ecx.tcx.span = source_info.span;
174+
match self.ecx.const_to_value(value.val) {
175+
Ok(val) => Some((val, value.ty, c.span)),
176+
Err(error) => {
177+
let (stacktrace, span) = self.ecx.generate_stacktrace(None);
178+
let err = ConstEvalErr {
179+
span,
180+
error,
181+
stacktrace,
182+
};
183+
err.report_as_error(
184+
self.tcx.at(source_info.span),
185+
"could not evaluate constant",
186+
);
187+
None
188+
},
189+
}
177190
},
178191
// evaluate the promoted and replace the constant with the evaluated result
179192
Literal::Promoted { index } => {

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

+2
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ const FOO: u8 = [5u8][1];
2727

2828
fn main() {
2929
black_box((FOO, FOO));
30+
//~^ ERROR referenced constant has errors
31+
//~| ERROR could not evaluate constant
3032
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ note: lint level defined here
1212
LL | #![warn(const_err)]
1313
| ^^^^^^^^^
1414

15-
warning: referenced constant
15+
warning: referenced constant has errors
1616
--> $DIR/conditional_array_execution.rs:19:20
1717
|
1818
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
@@ -27,7 +27,7 @@ warning: this expression will panic at runtime
2727
LL | println!("{}", FOO);
2828
| ^^^ referenced constant has errors
2929

30-
error[E0080]: referenced constant
30+
error[E0080]: referenced constant has errors
3131
--> $DIR/conditional_array_execution.rs:19:5
3232
|
3333
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
@@ -48,7 +48,7 @@ LL | println!("{}", FOO);
4848
|
4949
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
5050

51-
error[E0080]: referenced constant
51+
error[E0080]: referenced constant has errors
5252
--> $DIR/conditional_array_execution.rs:19:20
5353
|
5454
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ note: lint level defined here
1212
LL | #![warn(const_err)]
1313
| ^^^^^^^^^
1414

15-
warning: referenced constant
15+
warning: referenced constant has errors
1616
--> $DIR/conditional_array_execution.rs:19:20
1717
|
1818
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
@@ -27,7 +27,7 @@ warning: this expression will panic at runtime
2727
LL | println!("{}", FOO);
2828
| ^^^ referenced constant has errors
2929

30-
error[E0080]: referenced constant
30+
error[E0080]: referenced constant has errors
3131
--> $DIR/conditional_array_execution.rs:19:20
3232
|
3333
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LL | const Y: u32 = foo(0-1);
2020
| |
2121
| attempt to subtract with overflow
2222

23-
warning: referenced constant
23+
warning: referenced constant has errors
2424
--> $DIR/issue-43197.rs:24:23
2525
|
2626
LL | const X: u32 = 0-1;
@@ -35,7 +35,7 @@ warning: this expression will panic at runtime
3535
LL | println!("{} {}", X, Y);
3636
| ^ referenced constant has errors
3737

38-
warning: referenced constant
38+
warning: referenced constant has errors
3939
--> $DIR/issue-43197.rs:24:26
4040
|
4141
LL | const Y: u32 = foo(0-1);
@@ -50,7 +50,7 @@ warning: this expression will panic at runtime
5050
LL | println!("{} {}", X, Y);
5151
| ^ referenced constant has errors
5252

53-
error[E0080]: referenced constant
53+
error[E0080]: referenced constant has errors
5454
--> $DIR/issue-43197.rs:24:5
5555
|
5656
LL | const X: u32 = 0-1;
@@ -71,7 +71,7 @@ LL | println!("{} {}", X, Y);
7171
|
7272
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
7373

74-
error[E0080]: referenced constant
74+
error[E0080]: referenced constant has errors
7575
--> $DIR/issue-43197.rs:24:26
7676
|
7777
LL | const Y: u32 = foo(0-1);
@@ -86,7 +86,7 @@ error[E0080]: erroneous constant used
8686
LL | println!("{} {}", X, Y);
8787
| ^ referenced constant has errors
8888

89-
error[E0080]: referenced constant
89+
error[E0080]: referenced constant has errors
9090
--> $DIR/issue-43197.rs:24:23
9191
|
9292
LL | const X: u32 = 0-1;

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LL | const Y: u32 = foo(0-1);
2020
| |
2121
| attempt to subtract with overflow
2222

23-
warning: referenced constant
23+
warning: referenced constant has errors
2424
--> $DIR/issue-43197.rs:24:23
2525
|
2626
LL | const X: u32 = 0-1;
@@ -35,7 +35,7 @@ warning: this expression will panic at runtime
3535
LL | println!("{} {}", X, Y);
3636
| ^ referenced constant has errors
3737

38-
warning: referenced constant
38+
warning: referenced constant has errors
3939
--> $DIR/issue-43197.rs:24:26
4040
|
4141
LL | const Y: u32 = foo(0-1);
@@ -50,7 +50,7 @@ warning: this expression will panic at runtime
5050
LL | println!("{} {}", X, Y);
5151
| ^ referenced constant has errors
5252

53-
error[E0080]: referenced constant
53+
error[E0080]: referenced constant has errors
5454
--> $DIR/issue-43197.rs:24:26
5555
|
5656
LL | const Y: u32 = foo(0-1);
@@ -65,7 +65,7 @@ error[E0080]: erroneous constant used
6565
LL | println!("{} {}", X, Y);
6666
| ^ referenced constant has errors
6767

68-
error[E0080]: referenced constant
68+
error[E0080]: referenced constant has errors
6969
--> $DIR/issue-43197.rs:24:23
7070
|
7171
LL | const X: u32 = 0-1;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0080]: referenced constant
1+
error[E0080]: referenced constant has errors
22
--> $DIR/issue-44578.rs:35:5
33
|
44
LL | const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize];
@@ -19,7 +19,7 @@ LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
1919
|
2020
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
2121

22-
error[E0080]: referenced constant
22+
error[E0080]: referenced constant has errors
2323
--> $DIR/issue-44578.rs:35:20
2424
|
2525
LL | const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize];

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0080]: referenced constant
1+
error[E0080]: referenced constant has errors
22
--> $DIR/issue-44578.rs:35:20
33
|
44
LL | const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize];

src/test/ui/const-eval/issue-50814-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0080]: referenced constant
1+
error[E0080]: referenced constant has errors
22
--> $DIR/issue-50814-2.rs:26:5
33
|
44
LL | const BAR: usize = [5, 6, 7][T::BOO];

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0080]: referenced constant
1+
error[E0080]: referenced constant has errors
22
--> $DIR/issue-50814.rs:27:5
33
|
44
LL | const MAX: u8 = A::MAX + B::MAX;

src/test/ui/const-len-underflow-separate-spans.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ fn main() {
2020
let a: [i8; LEN] = unimplemented!();
2121
//~^ ERROR E0080
2222
//~| ERROR E0080
23-
//~| ERROR const_err
24-
//~| ERROR const_err
25-
//~| ERROR const_err
23+
//~| ERROR E0080
24+
//~| ERROR E0080
2625
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
1-
error: referenced constant
1+
error[E0080]: referenced constant has errors
22
--> $DIR/const-len-underflow-separate-spans.rs:20:17
33
|
44
LL | const LEN: usize = ONE - TWO;
55
| --------- attempt to subtract with overflow
66
...
77
LL | let a: [i8; LEN] = unimplemented!();
88
| ^^^
9-
|
10-
= note: #[deny(const_err)] on by default
119

12-
error: this expression will panic at runtime
10+
error[E0080]: could not evaluate constant
1311
--> $DIR/const-len-underflow-separate-spans.rs:20:17
1412
|
1513
LL | let a: [i8; LEN] = unimplemented!();
1614
| ^^^ referenced constant has errors
1715

18-
error: referenced constant
19-
--> $DIR/const-len-underflow-separate-spans.rs:20:17
20-
|
21-
LL | const LEN: usize = ONE - TWO;
22-
| --------- attempt to subtract with overflow
23-
...
24-
LL | let a: [i8; LEN] = unimplemented!();
25-
| ^^^
26-
27-
error[E0080]: referenced constant
16+
error[E0080]: referenced constant has errors
2817
--> $DIR/const-len-underflow-separate-spans.rs:20:12
2918
|
3019
LL | const LEN: usize = ONE - TWO;
@@ -41,6 +30,6 @@ LL | let a: [i8; LEN] = unimplemented!();
4130
| |
4231
| referenced constant has errors
4332

44-
error: aborting due to 5 previous errors
33+
error: aborting due to 4 previous errors
4534

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

0 commit comments

Comments
 (0)