Skip to content

Commit c062f3d

Browse files
authored
Rollup merge of #86340 - Smittyvb:ctfe-hard-error-message, r=RalfJung
Use better error message for hard errors in CTFE I noticed this while working on #86255: currently the same message is used for hard errors and soft errors in CTFE. This changes the error messages to make 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. This doesn't affect the behaviour of these error messages, only the content. This changes the error logic to check if the error should be hard or soft where it is generated, instead of where it is emitted, to allow this distinction in error messages.
2 parents 9521da7 + 044b362 commit c062f3d

16 files changed

+80
-107
lines changed

compiler/rustc_middle/src/mir/interpret/error.rs

+10
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,14 @@ impl InterpError<'_> {
518518
_ => false,
519519
}
520520
}
521+
522+
/// Should this error be reported as a hard error, preventing compilation, or a soft error,
523+
/// causing a deny-by-default lint?
524+
pub fn is_hard_err(&self) -> bool {
525+
use InterpError::*;
526+
match *self {
527+
MachineStop(ref err) => err.is_hard_err(),
528+
_ => false,
529+
}
530+
}
521531
}

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

+15-12
Original file line numberDiff line numberDiff line change
@@ -312,22 +312,17 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
312312
let err = ConstEvalErr::new(&ecx, error, None);
313313
// Some CTFE errors raise just a lint, not a hard error; see
314314
// <https://github.com/rust-lang/rust/issues/71800>.
315-
let emit_as_lint = if let Some(def) = def.as_local() {
315+
let is_hard_err = if let Some(def) = def.as_local() {
316316
// (Associated) consts only emit a lint, since they might be unused.
317-
matches!(tcx.def_kind(def.did.to_def_id()), DefKind::Const | DefKind::AssocConst)
317+
!matches!(tcx.def_kind(def.did.to_def_id()), DefKind::Const | DefKind::AssocConst)
318+
// check if the inner InterpError is hard
319+
|| err.error.is_hard_err()
318320
} else {
319321
// use of broken constant from other crate: always an error
320-
false
322+
true
321323
};
322-
if emit_as_lint {
323-
let hir_id = tcx.hir().local_def_id_to_hir_id(def.as_local().unwrap().did);
324-
Err(err.report_as_lint(
325-
tcx.at(tcx.def_span(def.did)),
326-
"any use of this value will cause an error",
327-
hir_id,
328-
Some(err.span),
329-
))
330-
} else {
324+
325+
if is_hard_err {
331326
let msg = if is_static {
332327
Cow::from("could not evaluate static initializer")
333328
} else {
@@ -345,6 +340,14 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
345340
};
346341

347342
Err(err.report_as_error(ecx.tcx.at(ecx.cur_span()), &msg))
343+
} else {
344+
let hir_id = tcx.hir().local_def_id_to_hir_id(def.as_local().unwrap().did);
345+
Err(err.report_as_lint(
346+
tcx.at(tcx.def_span(def.did)),
347+
"any use of this value will cause an error",
348+
hir_id,
349+
Some(err.span),
350+
))
348351
}
349352
}
350353
Ok(mplace) => {

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

0 commit comments

Comments
 (0)