Skip to content

Commit 4fe4ff9

Browse files
committed
Use better error message for hard errors in CTFE
Currently the same message is used for hard errors and soft errors. This makes hard errors use a message that indicates the reality of the situation correctly, since usage of the constant is never allowed when there was a hard error evaluating it.
1 parent 60f1a2f commit 4fe4ff9

15 files changed

+58
-97
lines changed

compiler/rustc_mir/src/const_eval/error.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl<'tcx> ConstEvalErr<'tcx> {
157157
tcx: TyCtxtAt<'tcx>,
158158
message: &str,
159159
emit: impl FnOnce(DiagnosticBuilder<'_>),
160-
mut lint_root: Option<hir::HirId>,
160+
lint_root: Option<hir::HirId>,
161161
) -> ErrorHandled {
162162
let finish = |mut err: DiagnosticBuilder<'_>, span_msg: Option<String>| {
163163
trace!("reporting const eval failure at {:?}", self.span);
@@ -194,12 +194,6 @@ impl<'tcx> ConstEvalErr<'tcx> {
194194
_ => {}
195195
};
196196

197-
// If we have a 'hard error', then set `lint_root` to `None` so that we don't
198-
// emit a lint.
199-
if matches!(&self.error, InterpError::MachineStop(err) if err.is_hard_err()) {
200-
lint_root = None;
201-
}
202-
203197
let err_msg = self.error.to_string();
204198

205199
// Regular case - emit a lint.

compiler/rustc_mir/src/const_eval/eval_queries.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use super::{CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr, Memory
22
use crate::interpret::eval_nullary_intrinsic;
33
use crate::interpret::{
44
intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId,
5-
Immediate, InternKind, InterpCx, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, Scalar,
6-
ScalarMaybeUninit, StackPopCleanup,
5+
Immediate, InternKind, InterpCx, InterpError, InterpResult, MPlaceTy, MemoryKind, OpTy,
6+
RefTracking, Scalar, ScalarMaybeUninit, StackPopCleanup,
77
};
88
use crate::util::pretty::display_allocation;
99

@@ -315,6 +315,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
315315
let emit_as_lint = if let Some(def) = def.as_local() {
316316
// (Associated) consts only emit a lint, since they might be unused.
317317
matches!(tcx.def_kind(def.did.to_def_id()), DefKind::Const | DefKind::AssocConst)
318+
&& !matches!(&err.error, InterpError::MachineStop(err) if err.is_hard_err())
318319
} else {
319320
// use of broken constant from other crate: always an error
320321
false

src/test/ui/consts/const-eval/const_panic.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@
55
const MSG: &str = "hello";
66

77
const Z: () = std::panic!("cheese");
8-
//~^ ERROR any use of this value will cause an error
8+
//~^ ERROR evaluation of constant value failed
99

1010
const Z2: () = std::panic!();
11-
//~^ ERROR any use of this value will cause an error
11+
//~^ ERROR evaluation of constant value failed
1212

1313
const Y: () = std::unreachable!();
14-
//~^ ERROR any use of this value will cause an error
14+
//~^ ERROR evaluation of constant value failed
1515

1616
const X: () = std::unimplemented!();
17-
//~^ ERROR any use of this value will cause an error
17+
//~^ ERROR evaluation of constant value failed
1818
//
1919
const W: () = std::panic!(MSG);
20-
//~^ ERROR any use of this value will cause an error
20+
//~^ ERROR evaluation of constant value failed
2121

2222
const Z_CORE: () = core::panic!("cheese");
23-
//~^ ERROR any use of this value will cause an error
23+
//~^ ERROR evaluation of constant value failed
2424

2525
const Z2_CORE: () = core::panic!();
26-
//~^ ERROR any use of this value will cause an error
26+
//~^ ERROR evaluation of constant value failed
2727

2828
const Y_CORE: () = core::unreachable!();
29-
//~^ ERROR any use of this value will cause an error
29+
//~^ ERROR evaluation of constant value failed
3030

3131
const X_CORE: () = core::unimplemented!();
32-
//~^ ERROR any use of this value will cause an error
32+
//~^ ERROR evaluation of constant value failed
3333

3434
const W_CORE: () = core::panic!(MSG);
35-
//~^ ERROR any use of this value will cause an error
35+
//~^ ERROR evaluation of constant value failed

src/test/ui/consts/const-eval/const_panic.stderr

+20-40
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,80 @@
1-
error[E0080]: any use of this value will cause an error
1+
error[E0080]: evaluation of constant value failed
22
--> $DIR/const_panic.rs:7:15
33
|
44
LL | const Z: () = std::panic!("cheese");
5-
| --------------^^^^^^^^^^^^^^^^^^^^^-
6-
| |
7-
| the evaluated program panicked at 'cheese', $DIR/const_panic.rs:7:15
5+
| ^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'cheese', $DIR/const_panic.rs:7:15
86
|
97
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
108

11-
error[E0080]: any use of this value will cause an error
9+
error[E0080]: evaluation of constant value failed
1210
--> $DIR/const_panic.rs:10:16
1311
|
1412
LL | const Z2: () = std::panic!();
15-
| ---------------^^^^^^^^^^^^^-
16-
| |
17-
| the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:10:16
13+
| ^^^^^^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:10:16
1814
|
1915
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
2016

21-
error[E0080]: any use of this value will cause an error
17+
error[E0080]: evaluation of constant value failed
2218
--> $DIR/const_panic.rs:13:15
2319
|
2420
LL | const Y: () = std::unreachable!();
25-
| --------------^^^^^^^^^^^^^^^^^^^-
26-
| |
27-
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:13:15
21+
| ^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:13:15
2822
|
2923
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
3024

31-
error[E0080]: any use of this value will cause an error
25+
error[E0080]: evaluation of constant value failed
3226
--> $DIR/const_panic.rs:16:15
3327
|
3428
LL | const X: () = std::unimplemented!();
35-
| --------------^^^^^^^^^^^^^^^^^^^^^-
36-
| |
37-
| the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:16:15
29+
| ^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:16:15
3830
|
3931
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
4032

41-
error[E0080]: any use of this value will cause an error
33+
error[E0080]: evaluation of constant value failed
4234
--> $DIR/const_panic.rs:19:15
4335
|
4436
LL | const W: () = std::panic!(MSG);
45-
| --------------^^^^^^^^^^^^^^^^-
46-
| |
47-
| the evaluated program panicked at 'hello', $DIR/const_panic.rs:19:15
37+
| ^^^^^^^^^^^^^^^^ the evaluated program panicked at 'hello', $DIR/const_panic.rs:19:15
4838
|
4939
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
5040

51-
error[E0080]: any use of this value will cause an error
41+
error[E0080]: evaluation of constant value failed
5242
--> $DIR/const_panic.rs:22:20
5343
|
5444
LL | const Z_CORE: () = core::panic!("cheese");
55-
| -------------------^^^^^^^^^^^^^^^^^^^^^^-
56-
| |
57-
| the evaluated program panicked at 'cheese', $DIR/const_panic.rs:22:20
45+
| ^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'cheese', $DIR/const_panic.rs:22:20
5846
|
5947
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
6048

61-
error[E0080]: any use of this value will cause an error
49+
error[E0080]: evaluation of constant value failed
6250
--> $DIR/const_panic.rs:25:21
6351
|
6452
LL | const Z2_CORE: () = core::panic!();
65-
| --------------------^^^^^^^^^^^^^^-
66-
| |
67-
| the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:25:21
53+
| ^^^^^^^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:25:21
6854
|
6955
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
7056

71-
error[E0080]: any use of this value will cause an error
57+
error[E0080]: evaluation of constant value failed
7258
--> $DIR/const_panic.rs:28:20
7359
|
7460
LL | const Y_CORE: () = core::unreachable!();
75-
| -------------------^^^^^^^^^^^^^^^^^^^^-
76-
| |
77-
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:28:20
61+
| ^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:28:20
7862
|
7963
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
8064

81-
error[E0080]: any use of this value will cause an error
65+
error[E0080]: evaluation of constant value failed
8266
--> $DIR/const_panic.rs:31:20
8367
|
8468
LL | const X_CORE: () = core::unimplemented!();
85-
| -------------------^^^^^^^^^^^^^^^^^^^^^^-
86-
| |
87-
| the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:31:20
69+
| ^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:31:20
8870
|
8971
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
9072

91-
error[E0080]: any use of this value will cause an error
73+
error[E0080]: evaluation of constant value failed
9274
--> $DIR/const_panic.rs:34:20
9375
|
9476
LL | const W_CORE: () = core::panic!(MSG);
95-
| -------------------^^^^^^^^^^^^^^^^^-
96-
| |
97-
| the evaluated program panicked at 'hello', $DIR/const_panic.rs:34:20
77+
| ^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'hello', $DIR/const_panic.rs:34:20
9878
|
9979
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
10080

src/test/ui/consts/const-eval/const_panic_libcore_bin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
use core::panic::PanicInfo;
88

99
const Z: () = panic!("cheese");
10-
//~^ ERROR any use of this value will cause an error
10+
//~^ ERROR evaluation of constant value failed
1111

1212
const Y: () = unreachable!();
13-
//~^ ERROR any use of this value will cause an error
13+
//~^ ERROR evaluation of constant value failed
1414

1515
const X: () = unimplemented!();
16-
//~^ ERROR any use of this value will cause an error
16+
//~^ ERROR evaluation of constant value failed
1717

1818
#[lang = "eh_personality"]
1919
fn eh() {}

src/test/ui/consts/const-eval/const_panic_libcore_bin.stderr

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
1-
error[E0080]: any use of this value will cause an error
1+
error[E0080]: evaluation of constant value failed
22
--> $DIR/const_panic_libcore_bin.rs:9:15
33
|
44
LL | const Z: () = panic!("cheese");
5-
| --------------^^^^^^^^^^^^^^^^-
6-
| |
7-
| the evaluated program panicked at 'cheese', $DIR/const_panic_libcore_bin.rs:9:15
5+
| ^^^^^^^^^^^^^^^^ the evaluated program panicked at 'cheese', $DIR/const_panic_libcore_bin.rs:9:15
86
|
97
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
108

11-
error[E0080]: any use of this value will cause an error
9+
error[E0080]: evaluation of constant value failed
1210
--> $DIR/const_panic_libcore_bin.rs:12:15
1311
|
1412
LL | const Y: () = unreachable!();
15-
| --------------^^^^^^^^^^^^^^-
16-
| |
17-
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic_libcore_bin.rs:12:15
13+
| ^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic_libcore_bin.rs:12:15
1814
|
1915
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
2016

21-
error[E0080]: any use of this value will cause an error
17+
error[E0080]: evaluation of constant value failed
2218
--> $DIR/const_panic_libcore_bin.rs:15:15
2319
|
2420
LL | const X: () = unimplemented!();
25-
| --------------^^^^^^^^^^^^^^^^-
26-
| |
27-
| the evaluated program panicked at 'not implemented', $DIR/const_panic_libcore_bin.rs:15:15
21+
| ^^^^^^^^^^^^^^^^ the evaluated program panicked at 'not implemented', $DIR/const_panic_libcore_bin.rs:15:15
2822
|
2923
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
3024

src/test/ui/consts/const-eval/panic-assoc-never-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct PrintName;
99

1010
impl PrintName {
1111
const VOID: ! = panic!();
12-
//~^ ERROR any use of this value will cause an error
12+
//~^ ERROR evaluation of constant value failed
1313
}
1414

1515
fn main() {

src/test/ui/consts/const-eval/panic-assoc-never-type.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error[E0080]: any use of this value will cause an error
1+
error[E0080]: evaluation of constant value failed
22
--> $DIR/panic-assoc-never-type.rs:11:21
33
|
44
LL | const VOID: ! = panic!();
5-
| ----------------^^^^^^^^-
6-
| |
7-
| the evaluated program panicked at 'explicit panic', $DIR/panic-assoc-never-type.rs:11:21
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/panic-assoc-never-type.rs:11:21
86
|
97
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
108

src/test/ui/consts/const-eval/panic-never-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![feature(never_type)]
55

66
const VOID: ! = panic!();
7-
//~^ ERROR any use of this value will cause an error
7+
//~^ ERROR evaluation of constant value failed
88

99
fn main() {
1010
let _ = VOID;

src/test/ui/consts/const-eval/panic-never-type.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error[E0080]: any use of this value will cause an error
1+
error[E0080]: evaluation of constant value failed
22
--> $DIR/panic-never-type.rs:6:17
33
|
44
LL | const VOID: ! = panic!();
5-
| ----------------^^^^^^^^-
6-
| |
7-
| the evaluated program panicked at 'explicit panic', $DIR/panic-never-type.rs:6:17
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/panic-never-type.rs:6:17
86
|
97
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
108

src/test/ui/consts/const-eval/unwind-abort.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#[unwind(aborts)]
44
const fn foo() {
5-
panic!() //~ ERROR any use of this value will cause an error
5+
panic!() //~ ERROR evaluation of constant value failed
66
}
77

88
const _: () = foo();

src/test/ui/consts/const-eval/unwind-abort.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
error[E0080]: any use of this value will cause an error
1+
error[E0080]: evaluation of constant value failed
22
--> $DIR/unwind-abort.rs:5:5
33
|
44
LL | panic!()
55
| ^^^^^^^^
66
| |
77
| the evaluated program panicked at 'explicit panic', $DIR/unwind-abort.rs:5:5
88
| inside `foo` at $SRC_DIR/std/src/panic.rs:LL:COL
9-
| inside `_` at $DIR/unwind-abort.rs:8:15
109
...
1110
LL | const _: () = foo();
12-
| --------------------
11+
| ----- inside `_` at $DIR/unwind-abort.rs:8:15
1312
|
1413
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
1514

src/test/ui/consts/const-unwrap.stderr

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
error[E0080]: any use of this value will cause an error
1+
error[E0080]: evaluation of constant value failed
22
--> $SRC_DIR/core/src/option.rs:LL:COL
33
|
44
LL | None => panic!("called `Option::unwrap()` on a `None` value"),
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
| |
77
| the evaluated program panicked at 'called `Option::unwrap()` on a `None` value', $DIR/const-unwrap.rs:9:38
88
| inside `Option::<i32>::unwrap` at $SRC_DIR/core/src/panic.rs:LL:COL
9-
| inside `BAR` at $DIR/const-unwrap.rs:9:18
109
|
11-
::: $DIR/const-unwrap.rs:9:1
10+
::: $DIR/const-unwrap.rs:9:18
1211
|
1312
LL | const BAR: i32 = Option::<i32>::None.unwrap();
14-
| ----------------------------------------------
13+
| ---------------------------- inside `BAR` at $DIR/const-unwrap.rs:9:18
1514
|
1615
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
1716

src/test/ui/consts/control-flow/assert.const_panic.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error[E0080]: any use of this value will cause an error
1+
error[E0080]: evaluation of constant value failed
22
--> $DIR/assert.rs:10:15
33
|
44
LL | const _: () = assert!(false);
5-
| --------------^^^^^^^^^^^^^^-
6-
| |
7-
| the evaluated program panicked at 'assertion failed: false', $DIR/assert.rs:10:15
5+
| ^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: false', $DIR/assert.rs:10:15
86
|
97
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
108

src/test/ui/consts/control-flow/assert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ const _: () = assert!(true);
99

1010
const _: () = assert!(false);
1111
//[stock]~^ ERROR panicking in constants is unstable
12-
//[const_panic]~^^ ERROR any use of this value will cause an error
12+
//[const_panic]~^^ ERROR evaluation of constant value failed
1313

1414
fn main() {}

0 commit comments

Comments
 (0)