Skip to content

Commit f012947

Browse files
authored
Rollup merge of #136503 - estebank:const-panic, r=RalfJung
Tweak output of const panic diagnostic ### Shorten span of panic failures in const context Previously, we included a redundant prefix on the panic message and a postfix of the location of the panic. The prefix didn't carry any additional information beyond "something failed", and the location of the panic is redundant with the diagnostic's span, which gets printed out even if its code is not shown. ``` error[E0080]: evaluation of constant value failed --> $DIR/assert-type-intrinsics.rs:11:9 | LL | MaybeUninit::<!>::uninit().assume_init(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ panic: aborted execution: attempted to instantiate uninhabited type `!` ``` ``` error[E0080]: evaluation of `Fail::<i32>::C` failed --> $DIR/collect-in-dead-closure.rs:9:19 | LL | const C: () = panic!(); | ^^^^^^^^ explicit panic | = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) ``` ``` error[E0080]: evaluation of constant value failed --> $DIR/uninhabited.rs:87:9 | LL | assert!(false); | ^^^^^^^^^^^^^^ assertion failed: false | = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) ``` ### Remove duplicated span from const eval frame list When the primary span for a const error is the same as the first frame in the const error report, skip it. ``` error[E0080]: evaluation of constant value failed --> $DIR/issue-88434-removal-index-should-be-less.rs:3:24 | LL | const _CONST: &[u8] = &f(&[], |_| {}); | ^^^^^^^^^^^^^^ explicit panic | note: inside `f::<{closure@$DIR/issue-88434-removal-index-should-be-less.rs:3:31: 3:34}>` --> $DIR/issue-88434-removal-index-should-be-less.rs:10:5 | LL | panic!() | ^^^^^^^^ the failure occurred here = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) ``` instead of ``` error[E0080]: evaluation of constant value failed --> $DIR/issue-88434-removal-index-should-be-less.rs:10:5 | LL | panic!() | ^^^^^^^^ explicit panic | note: inside `f::<{closure@$DIR/issue-88434-removal-index-should-be-less.rs:3:31: 3:34}>` --> $DIR/issue-88434-removal-index-should-be-less.rs:10:5 | LL | panic!() | ^^^^^^^^ note: inside `_CONST` --> $DIR/issue-88434-removal-index-should-be-less.rs:3:24 | LL | const _CONST: &[u8] = &f(&[], |_| {}); | ^^^^^^^^^^^^^^ = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) note: erroneous constant encountered --> $DIR/issue-88434-removal-index-should-be-less.rs:3:23 | LL | const _CONST: &[u8] = &f(&[], |_| {}); | ^^^^^^^^^^^^^^^ ``` r? ``@oli-obk``
2 parents 002da76 + 7d4d09e commit f012947

File tree

127 files changed

+495
-715
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+495
-715
lines changed

compiler/rustc_const_eval/messages.ftl

+3-2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ const_eval_frame_note_inner = inside {$where_ ->
109109
*[other] {""}
110110
}
111111
112+
const_eval_frame_note_last = the failure occurred here
113+
112114
const_eval_in_bounds_test = out-of-bounds pointer use
113115
const_eval_incompatible_calling_conventions =
114116
calling a function with calling convention {$callee_conv} using calling convention {$caller_conv}
@@ -300,8 +302,7 @@ const_eval_overflow_arith =
300302
const_eval_overflow_shift =
301303
overflowing shift by {$shift_amount} in `{$intrinsic}`
302304
303-
const_eval_panic =
304-
the evaluated program panicked at '{$msg}', {$file}:{$line}:{$col}
305+
const_eval_panic = evaluation panicked: {$msg}
305306
306307
const_eval_panic_non_str = argument to `panic!()` in a const context must have type `&str`
307308

compiler/rustc_const_eval/src/const_eval/error.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ impl MachineStopType for ConstEvalErrKind {
4848
| ModifiedGlobal
4949
| WriteThroughImmutablePointer => {}
5050
AssertFailure(kind) => kind.add_args(adder),
51-
Panic { msg, line, col, file } => {
51+
Panic { msg, .. } => {
5252
adder("msg".into(), msg.into_diag_arg(&mut None));
53-
adder("file".into(), file.into_diag_arg(&mut None));
54-
adder("line".into(), line.into_diag_arg(&mut None));
55-
adder("col".into(), col.into_diag_arg(&mut None));
5653
}
5754
}
5855
}
@@ -72,7 +69,7 @@ pub fn get_span_and_frames<'tcx>(
7269
let mut stacktrace = Frame::generate_stacktrace_from_stack(stack);
7370
// Filter out `requires_caller_location` frames.
7471
stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(*tcx));
75-
let span = stacktrace.first().map(|f| f.span).unwrap_or(tcx.span);
72+
let span = stacktrace.last().map(|f| f.span).unwrap_or(tcx.span);
7673

7774
let mut frames = Vec::new();
7875

@@ -115,6 +112,20 @@ pub fn get_span_and_frames<'tcx>(
115112
}
116113
}
117114

115+
// In `rustc`, we present const-eval errors from the outer-most place first to the inner-most.
116+
// So we reverse the frames here. The first frame will be the same as the span from the current
117+
// `TyCtxtAt<'_>`, so we remove it as it would be redundant.
118+
frames.reverse();
119+
if frames.len() > 0 {
120+
frames.remove(0);
121+
}
122+
if let Some(last) = frames.last_mut()
123+
// If the span is not going to be printed, we don't want the span label for `is_last`.
124+
&& tcx.sess.source_map().span_to_snippet(last.span.source_callsite()).is_ok()
125+
{
126+
last.has_label = true;
127+
}
128+
118129
(span, frames)
119130
}
120131

compiler/rustc_const_eval/src/errors.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_abi::WrappingRange;
66
use rustc_errors::codes::*;
77
use rustc_errors::{
88
Diag, DiagArgValue, DiagCtxtHandle, DiagMessage, Diagnostic, EmissionGuarantee, Level,
9+
MultiSpan, SubdiagMessageOp, Subdiagnostic,
910
};
1011
use rustc_hir::ConstContext;
1112
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
@@ -17,6 +18,7 @@ use rustc_middle::mir::interpret::{
1718
use rustc_middle::ty::{self, Mutability, Ty};
1819
use rustc_span::{Span, Symbol};
1920

21+
use crate::fluent_generated as fluent;
2022
use crate::interpret::InternKind;
2123

2224
#[derive(Diagnostic)]
@@ -278,14 +280,31 @@ pub(crate) struct NonConstImplNote {
278280
pub span: Span,
279281
}
280282

281-
#[derive(Subdiagnostic, Clone)]
282-
#[note(const_eval_frame_note)]
283+
#[derive(Clone)]
283284
pub struct FrameNote {
284-
#[primary_span]
285285
pub span: Span,
286286
pub times: i32,
287287
pub where_: &'static str,
288288
pub instance: String,
289+
pub has_label: bool,
290+
}
291+
292+
impl Subdiagnostic for FrameNote {
293+
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
294+
self,
295+
diag: &mut Diag<'_, G>,
296+
f: &F,
297+
) {
298+
diag.arg("times", self.times);
299+
diag.arg("where_", self.where_);
300+
diag.arg("instance", self.instance);
301+
let mut span: MultiSpan = self.span.into();
302+
if self.has_label && !self.span.is_dummy() {
303+
span.push_span_label(self.span, fluent::const_eval_frame_note_last);
304+
}
305+
let msg = f(diag, fluent::const_eval_frame_note.into());
306+
diag.span_note(span, msg);
307+
}
289308
}
290309

291310
#[derive(Subdiagnostic)]

compiler/rustc_const_eval/src/interpret/stack.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,19 @@ impl<'tcx> FrameInfo<'tcx> {
231231
pub fn as_note(&self, tcx: TyCtxt<'tcx>) -> errors::FrameNote {
232232
let span = self.span;
233233
if tcx.def_key(self.instance.def_id()).disambiguated_data.data == DefPathData::Closure {
234-
errors::FrameNote { where_: "closure", span, instance: String::new(), times: 0 }
234+
errors::FrameNote {
235+
where_: "closure",
236+
span,
237+
instance: String::new(),
238+
times: 0,
239+
has_label: false,
240+
}
235241
} else {
236242
let instance = format!("{}", self.instance);
237243
// Note: this triggers a `must_produce_diag` state, which means that if we ever get
238244
// here we must emit a diagnostic. We should never display a `FrameInfo` unless we
239245
// actually want to emit a warning or error to the user.
240-
errors::FrameNote { where_: "instance", span, instance, times: 0 }
246+
errors::FrameNote { where_: "instance", span, instance, times: 0, has_label: false }
241247
}
242248
}
243249
}

src/tools/miri/tests/fail/erroneous_const.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: evaluation of `PrintName::<i32>::VOID` failed
22
--> tests/fail/erroneous_const.rs:LL:CC
33
|
44
LL | const VOID: ! = panic!();
5-
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', tests/fail/erroneous_const.rs:LL:CC
5+
| ^^^^^^^^ evaluation panicked: explicit panic
66
|
77
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

tests/ui/borrowck/issue-81899.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// Regression test for #81899.
22
// The `panic!()` below is important to trigger the fixed ICE.
33

4-
const _CONST: &[u8] = &f(&[], |_| {});
4+
const _CONST: &[u8] = &f(&[], |_| {}); //~ ERROR evaluation of constant value failed
55
//~^ constant
66

77
const fn f<F>(_: &[u8], _: F) -> &[u8]
88
where
99
F: FnMut(&u8),
1010
{
11-
panic!() //~ ERROR evaluation of constant value failed
12-
//~^ panic
11+
panic!() //~ inside `f
1312
}
1413

1514
fn main() {}

tests/ui/borrowck/issue-81899.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/issue-81899.rs:11:5
2+
--> $DIR/issue-81899.rs:4:24
33
|
4-
LL | panic!()
5-
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/issue-81899.rs:11:5
4+
LL | const _CONST: &[u8] = &f(&[], |_| {});
5+
| ^^^^^^^^^^^^^^ evaluation panicked: explicit panic
66
|
77
note: inside `f::<{closure@$DIR/issue-81899.rs:4:31: 4:34}>`
88
--> $DIR/issue-81899.rs:11:5
99
|
1010
LL | panic!()
11-
| ^^^^^^^^
12-
note: inside `_CONST`
13-
--> $DIR/issue-81899.rs:4:24
14-
|
15-
LL | const _CONST: &[u8] = &f(&[], |_| {});
16-
| ^^^^^^^^^^^^^^
11+
| ^^^^^^^^ the failure occurred here
1712
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
1813

1914
note: erroneous constant encountered
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Regression test related to issue 88434
22

3-
const _CONST: &() = &f(&|_| {});
3+
const _CONST: &() = &f(&|_| {}); //~ ERROR evaluation of constant value failed
44
//~^ constant
55

66
const fn f<F>(_: &F)
77
where
88
F: FnMut(&u8),
99
{
10-
panic!() //~ ERROR evaluation of constant value failed
11-
//~^ panic
10+
panic!() //~ inside `f
1211
}
1312

1413
fn main() { }

tests/ui/borrowck/issue-88434-minimal-example.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/issue-88434-minimal-example.rs:10:5
2+
--> $DIR/issue-88434-minimal-example.rs:3:22
33
|
4-
LL | panic!()
5-
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/issue-88434-minimal-example.rs:10:5
4+
LL | const _CONST: &() = &f(&|_| {});
5+
| ^^^^^^^^^^ evaluation panicked: explicit panic
66
|
77
note: inside `f::<{closure@$DIR/issue-88434-minimal-example.rs:3:25: 3:28}>`
88
--> $DIR/issue-88434-minimal-example.rs:10:5
99
|
1010
LL | panic!()
11-
| ^^^^^^^^
12-
note: inside `_CONST`
13-
--> $DIR/issue-88434-minimal-example.rs:3:22
14-
|
15-
LL | const _CONST: &() = &f(&|_| {});
16-
| ^^^^^^^^^^
11+
| ^^^^^^^^ the failure occurred here
1712
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
1813

1914
note: erroneous constant encountered
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Regression test for issue 88434
22

3-
const _CONST: &[u8] = &f(&[], |_| {});
3+
const _CONST: &[u8] = &f(&[], |_| {}); //~ ERROR evaluation of constant value failed
44
//~^ constant
55

66
const fn f<F>(_: &[u8], _: F) -> &[u8]
77
where
88
F: FnMut(&u8),
99
{
10-
panic!() //~ ERROR evaluation of constant value failed
11-
//~^ panic
10+
panic!() //~ inside `f
1211
}
1312

1413
fn main() { }

tests/ui/borrowck/issue-88434-removal-index-should-be-less.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/issue-88434-removal-index-should-be-less.rs:10:5
2+
--> $DIR/issue-88434-removal-index-should-be-less.rs:3:24
33
|
4-
LL | panic!()
5-
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/issue-88434-removal-index-should-be-less.rs:10:5
4+
LL | const _CONST: &[u8] = &f(&[], |_| {});
5+
| ^^^^^^^^^^^^^^ evaluation panicked: explicit panic
66
|
77
note: inside `f::<{closure@$DIR/issue-88434-removal-index-should-be-less.rs:3:31: 3:34}>`
88
--> $DIR/issue-88434-removal-index-should-be-less.rs:10:5
99
|
1010
LL | panic!()
11-
| ^^^^^^^^
12-
note: inside `_CONST`
13-
--> $DIR/issue-88434-removal-index-should-be-less.rs:3:24
14-
|
15-
LL | const _CONST: &[u8] = &f(&[], |_| {});
16-
| ^^^^^^^^^^^^^^
11+
| ^^^^^^^^ the failure occurred here
1712
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
1813

1914
note: erroneous constant encountered

tests/ui/coherence/const-errs-dont-conflict-103369.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
pub trait ConstGenericTrait<const N: u32> {}
44

5-
impl ConstGenericTrait<{my_fn(1)}> for () {}
5+
impl ConstGenericTrait<{my_fn(1)}> for () {} //~ ERROR E0080
66

7-
impl ConstGenericTrait<{my_fn(2)}> for () {}
7+
impl ConstGenericTrait<{my_fn(2)}> for () {} //~ ERROR E0080
88

99
const fn my_fn(v: u32) -> u32 {
10-
panic!("Some error occurred"); //~ ERROR E0080
11-
//~| ERROR E0080
10+
panic!("Some error occurred");
1211
}
1312

1413
fn main() {}

tests/ui/coherence/const-errs-dont-conflict-103369.stderr

+8-18
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/const-errs-dont-conflict-103369.rs:10:5
2+
--> $DIR/const-errs-dont-conflict-103369.rs:5:25
33
|
4-
LL | panic!("Some error occurred");
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'Some error occurred', $DIR/const-errs-dont-conflict-103369.rs:10:5
4+
LL | impl ConstGenericTrait<{my_fn(1)}> for () {}
5+
| ^^^^^^^^ evaluation panicked: Some error occurred
66
|
77
note: inside `my_fn`
88
--> $DIR/const-errs-dont-conflict-103369.rs:10:5
99
|
1010
LL | panic!("Some error occurred");
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: inside `<() as ConstGenericTrait<{my_fn(1)}>>::{constant#0}`
13-
--> $DIR/const-errs-dont-conflict-103369.rs:5:25
14-
|
15-
LL | impl ConstGenericTrait<{my_fn(1)}> for () {}
16-
| ^^^^^^^^
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
1712
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
1813

1914
error[E0080]: evaluation of constant value failed
20-
--> $DIR/const-errs-dont-conflict-103369.rs:10:5
15+
--> $DIR/const-errs-dont-conflict-103369.rs:7:25
2116
|
22-
LL | panic!("Some error occurred");
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'Some error occurred', $DIR/const-errs-dont-conflict-103369.rs:10:5
17+
LL | impl ConstGenericTrait<{my_fn(2)}> for () {}
18+
| ^^^^^^^^ evaluation panicked: Some error occurred
2419
|
2520
note: inside `my_fn`
2621
--> $DIR/const-errs-dont-conflict-103369.rs:10:5
2722
|
2823
LL | panic!("Some error occurred");
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30-
note: inside `<() as ConstGenericTrait<{my_fn(2)}>>::{constant#0}`
31-
--> $DIR/const-errs-dont-conflict-103369.rs:7:25
32-
|
33-
LL | impl ConstGenericTrait<{my_fn(2)}> for () {}
34-
| ^^^^^^^^
24+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
3525
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
3626

3727
error: aborting due to 2 previous errors

tests/ui/const-generics/issues/issue-100313.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ struct T<const B: &'static bool>;
66
impl<const B: &'static bool> T<B> {
77
const fn set_false(&self) {
88
unsafe {
9-
*(B as *const bool as *mut bool) = false;
10-
//~^ ERROR evaluation of constant value failed [E0080]
9+
*(B as *const bool as *mut bool) = false; //~ inside `T
1110
}
1211
}
1312
}
1413

1514
const _: () = {
1615
let x = T::<{ &true }>;
17-
x.set_false();
16+
x.set_false(); //~ ERROR evaluation of constant value failed [E0080]
1817
};
1918

2019
fn main() {}

tests/ui/const-generics/issues/issue-100313.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/issue-100313.rs:9:13
2+
--> $DIR/issue-100313.rs:16:5
33
|
4-
LL | *(B as *const bool as *mut bool) = false;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ writing to ALLOC0 which is read-only
4+
LL | x.set_false();
5+
| ^^^^^^^^^^^^^ writing to ALLOC0 which is read-only
66
|
77
note: inside `T::<&true>::set_false`
88
--> $DIR/issue-100313.rs:9:13
99
|
1010
LL | *(B as *const bool as *mut bool) = false;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: inside `_`
13-
--> $DIR/issue-100313.rs:17:5
14-
|
15-
LL | x.set_false();
16-
| ^^^^^^^^^^^^^
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
1712

1813
error: aborting due to 1 previous error
1914

0 commit comments

Comments
 (0)