Skip to content

Commit 0c1d5b4

Browse files
committed
Tweak output for note pointing at root cause of const error
``` error[E0080]: evaluation of constant value failed --> $DIR/issue-81899.rs:4:24 | LL | const _CONST: &[u8] = &f(&[], |_| {}); | ^^^^^^^^^^^^^^ panic: explicit panic | note: the evaluated program failed inside `f::<{closure@$DIR/issue-81899.rs:4:31: 4:34}>` --> $DIR/issue-81899.rs:11:5 | LL | panic!() | ^^^^^^^^ the evaluated program failed 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) ```
1 parent 0aa955c commit 0c1d5b4

Some content is hidden

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

45 files changed

+217
-93
lines changed

compiler/rustc_const_eval/messages.ftl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,17 @@ const_eval_frame_note = {$times ->
103103
*[other] [... {$times} additional calls {const_eval_frame_note_inner} ...]
104104
}
105105
106-
const_eval_frame_note_inner = inside {$where_ ->
106+
const_eval_frame_note_inner = {$is_last ->
107+
[true] {"the evaluated program failed "}
108+
*[false] {""}
109+
}inside {$where_ ->
107110
[closure] closure
108111
[instance] `{$instance}`
109112
*[other] {""}
110113
}
111114
115+
const_eval_frame_note_last = the evaluated program failed here
116+
112117
const_eval_in_bounds_test = out-of-bounds pointer use
113118
const_eval_incompatible_calling_conventions =
114119
calling a function with calling convention {$callee_conv} using calling convention {$caller_conv}

compiler/rustc_const_eval/src/const_eval/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ pub fn get_span_and_frames<'tcx>(
115115
if frames.len() > 0 {
116116
frames.remove(0);
117117
}
118+
if let Some(last) = frames.last_mut() {
119+
last.is_last = true;
120+
}
118121

119122
(span, frames)
120123
}

compiler/rustc_const_eval/src/errors.rs

Lines changed: 23 additions & 3 deletions
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,32 @@ 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 is_last: 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+
diag.arg("is_last", self.is_last);
302+
let mut span: MultiSpan = self.span.into();
303+
if self.is_last && !self.span.is_dummy() {
304+
span.push_span_label(self.span, fluent::const_eval_frame_note_last);
305+
}
306+
let msg = f(diag, fluent::const_eval_frame_note.into());
307+
diag.span_note(span, msg);
308+
}
289309
}
290310

291311
#[derive(Subdiagnostic)]

compiler/rustc_const_eval/src/interpret/stack.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ impl<'tcx, Prov: Provenance> LocalState<'tcx, Prov> {
209209
pub struct FrameInfo<'tcx> {
210210
pub instance: ty::Instance<'tcx>,
211211
pub span: Span,
212+
pub is_last: bool,
212213
}
213214

214215
// FIXME: only used by miri, should be removed once translatable.
@@ -231,13 +232,25 @@ impl<'tcx> FrameInfo<'tcx> {
231232
pub fn as_note(&self, tcx: TyCtxt<'tcx>) -> errors::FrameNote {
232233
let span = self.span;
233234
if tcx.def_key(self.instance.def_id()).disambiguated_data.data == DefPathData::Closure {
234-
errors::FrameNote { where_: "closure", span, instance: String::new(), times: 0 }
235+
errors::FrameNote {
236+
where_: "closure",
237+
span,
238+
instance: String::new(),
239+
times: 0,
240+
is_last: self.is_last,
241+
}
235242
} else {
236243
let instance = format!("{}", self.instance);
237244
// Note: this triggers a `must_produce_diag` state, which means that if we ever get
238245
// here we must emit a diagnostic. We should never display a `FrameInfo` unless we
239246
// actually want to emit a warning or error to the user.
240-
errors::FrameNote { where_: "instance", span, instance, times: 0 }
247+
errors::FrameNote {
248+
where_: "instance",
249+
span,
250+
instance,
251+
times: 0,
252+
is_last: self.is_last,
253+
}
241254
}
242255
}
243256
}
@@ -322,15 +335,15 @@ impl<'tcx, Prov: Provenance, Extra> Frame<'tcx, Prov, Extra> {
322335
let mir::SourceInfo { mut span, scope } = *frame.body.source_info(loc);
323336
let mut scope_data = &frame.body.source_scopes[scope];
324337
while let Some((instance, call_span)) = scope_data.inlined {
325-
frames.push(FrameInfo { span, instance });
338+
frames.push(FrameInfo { span, instance, is_last: false });
326339
span = call_span;
327340
scope_data = &frame.body.source_scopes[scope_data.parent_scope.unwrap()];
328341
}
329342
span
330343
}
331344
Right(span) => span,
332345
};
333-
frames.push(FrameInfo { span, instance: frame.instance });
346+
frames.push(FrameInfo { span, instance: frame.instance, is_last: false });
334347
}
335348
trace!("generate stacktrace: {:#?}", frames);
336349
frames

tests/ui/borrowck/issue-81899.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0080]: evaluation of constant value failed
44
LL | const _CONST: &[u8] = &f(&[], |_| {});
55
| ^^^^^^^^^^^^^^ panic: explicit panic
66
|
7-
note: inside `f::<{closure@$DIR/issue-81899.rs:4:31: 4:34}>`
7+
note: the evaluated program failed inside `f::<{closure@$DIR/issue-81899.rs:4:31: 4:34}>`
88
--> $DIR/issue-81899.rs:11:5
99
|
1010
LL | panic!()
11-
| ^^^^^^^^
11+
| ^^^^^^^^ the evaluated program failed here
1212
= 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)
1313

1414
note: erroneous constant encountered

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0080]: evaluation of constant value failed
44
LL | const _CONST: &() = &f(&|_| {});
55
| ^^^^^^^^^^ panic: explicit panic
66
|
7-
note: inside `f::<{closure@$DIR/issue-88434-minimal-example.rs:3:25: 3:28}>`
7+
note: the evaluated program failed 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-
| ^^^^^^^^
11+
| ^^^^^^^^ the evaluated program failed here
1212
= 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)
1313

1414
note: erroneous constant encountered

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0080]: evaluation of constant value failed
44
LL | const _CONST: &[u8] = &f(&[], |_| {});
55
| ^^^^^^^^^^^^^^ panic: explicit panic
66
|
7-
note: inside `f::<{closure@$DIR/issue-88434-removal-index-should-be-less.rs:3:31: 3:34}>`
7+
note: the evaluated program failed 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-
| ^^^^^^^^
11+
| ^^^^^^^^ the evaluated program failed here
1212
= 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)
1313

1414
note: erroneous constant encountered

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0080]: evaluation of constant value failed
44
LL | impl ConstGenericTrait<{my_fn(1)}> for () {}
55
| ^^^^^^^^ panic: Some error occurred
66
|
7-
note: inside `my_fn`
7+
note: the evaluated program failed inside `my_fn`
88
--> $DIR/const-errs-dont-conflict-103369.rs:10:5
99
|
1010
LL | panic!("Some error occurred");
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program failed here
1212
= 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)
1313

1414
error[E0080]: evaluation of constant value failed
@@ -17,11 +17,11 @@ error[E0080]: evaluation of constant value failed
1717
LL | impl ConstGenericTrait<{my_fn(2)}> for () {}
1818
| ^^^^^^^^ panic: Some error occurred
1919
|
20-
note: inside `my_fn`
20+
note: the evaluated program failed inside `my_fn`
2121
--> $DIR/const-errs-dont-conflict-103369.rs:10:5
2222
|
2323
LL | panic!("Some error occurred");
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program failed here
2525
= 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)
2626

2727
error: aborting due to 2 previous errors

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0080]: evaluation of constant value failed
44
LL | x.set_false();
55
| ^^^^^^^^^^^^^ writing to ALLOC0 which is read-only
66
|
7-
note: inside `T::<&true>::set_false`
7+
note: the evaluated program failed inside `T::<&true>::set_false`
88
--> $DIR/issue-100313.rs:9:13
99
|
1010
LL | *(B as *const bool as *mut bool) = false;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program failed here
1212

1313
error: aborting due to 1 previous error
1414

tests/ui/const-ptr/forbidden_slices.stderr

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
107107
|
108108
note: inside `from_ptr_range::<'_, ()>`
109109
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
110-
note: inside `std::ptr::const_ptr::<impl *const ()>::sub_ptr`
110+
note: the evaluated program failed inside `std::ptr::const_ptr::<impl *const ()>::sub_ptr`
111111
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
112+
|
113+
= note: the evaluated program failed here
112114
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
113115

114116
error[E0080]: could not evaluate static initializer
@@ -117,8 +119,10 @@ error[E0080]: could not evaluate static initializer
117119
LL | from_ptr_range(ptr..ptr.add(2)) // errors inside libcore
118120
| ^^^^^^^^^^ out-of-bounds pointer arithmetic: expected a pointer to 8 bytes of memory, but got ALLOC10 which is only 4 bytes from the end of the allocation
119121
|
120-
note: inside `std::ptr::const_ptr::<impl *const u32>::add`
122+
note: the evaluated program failed inside `std::ptr::const_ptr::<impl *const u32>::add`
121123
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
124+
|
125+
= note: the evaluated program failed here
122126

123127
error[E0080]: it is undefined behavior to use this value
124128
--> $DIR/forbidden_slices.rs:57:1
@@ -172,8 +176,10 @@ error[E0080]: could not evaluate static initializer
172176
LL | from_ptr_range(ptr..ptr.add(1))
173177
| ^^^^^^^^^^ out-of-bounds pointer arithmetic: expected a pointer to 8 bytes of memory, but got ALLOC11+0x1 which is only 7 bytes from the end of the allocation
174178
|
175-
note: inside `std::ptr::const_ptr::<impl *const u64>::add`
179+
note: the evaluated program failed inside `std::ptr::const_ptr::<impl *const u64>::add`
176180
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
181+
|
182+
= note: the evaluated program failed here
177183

178184
error[E0080]: could not evaluate static initializer
179185
--> $DIR/forbidden_slices.rs:85:34
@@ -183,8 +189,10 @@ LL | pub static R9: &[u32] = unsafe { from_ptr_range(&D0..(&D0 as *const u32).ad
183189
|
184190
note: inside `from_ptr_range::<'_, u32>`
185191
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
186-
note: inside `std::ptr::const_ptr::<impl *const u32>::sub_ptr`
192+
note: the evaluated program failed inside `std::ptr::const_ptr::<impl *const u32>::sub_ptr`
187193
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
194+
|
195+
= note: the evaluated program failed here
188196

189197
error[E0080]: could not evaluate static initializer
190198
--> $DIR/forbidden_slices.rs:87:35
@@ -194,8 +202,10 @@ LL | pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) };
194202
|
195203
note: inside `from_ptr_range::<'_, u32>`
196204
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
197-
note: inside `std::ptr::const_ptr::<impl *const u32>::sub_ptr`
205+
note: the evaluated program failed inside `std::ptr::const_ptr::<impl *const u32>::sub_ptr`
198206
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
207+
|
208+
= note: the evaluated program failed here
199209

200210
error: aborting due to 18 previous errors
201211

tests/ui/const-ptr/out_of_bounds_read.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ error[E0080]: evaluation of constant value failed
44
LL | const _READ: u32 = unsafe { ptr::read(PAST_END_PTR) };
55
| ^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: expected a pointer to 4 bytes of memory, but got ALLOC0+0x4 which is at or beyond the end of the allocation of size 4 bytes
66
|
7-
note: inside `std::ptr::read::<u32>`
7+
note: the evaluated program failed inside `std::ptr::read::<u32>`
88
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
9+
|
10+
= note: the evaluated program failed here
911

1012
error[E0080]: evaluation of constant value failed
1113
--> $DIR/out_of_bounds_read.rs:11:39
@@ -15,8 +17,10 @@ LL | const _CONST_READ: u32 = unsafe { PAST_END_PTR.read() };
1517
|
1618
note: inside `std::ptr::const_ptr::<impl *const u32>::read`
1719
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
18-
note: inside `std::ptr::read::<u32>`
20+
note: the evaluated program failed inside `std::ptr::read::<u32>`
1921
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
22+
|
23+
= note: the evaluated program failed here
2024

2125
error[E0080]: evaluation of constant value failed
2226
--> $DIR/out_of_bounds_read.rs:12:37
@@ -26,8 +30,10 @@ LL | const _MUT_READ: u32 = unsafe { (PAST_END_PTR as *mut u32).read() };
2630
|
2731
note: inside `std::ptr::mut_ptr::<impl *mut u32>::read`
2832
--> $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
29-
note: inside `std::ptr::read::<u32>`
33+
note: the evaluated program failed inside `std::ptr::read::<u32>`
3034
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
35+
|
36+
= note: the evaluated program failed here
3137

3238
error: aborting due to 3 previous errors
3339

tests/ui/consts/const-eval/const_fn_ptr_fail2.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ error[E0080]: evaluation of constant value failed
44
LL | const Y: usize = bar(X, 2); // FIXME: should fail to typeck someday
55
| ^^^^^^^^^ calling non-const function `double`
66
|
7-
note: inside `bar`
7+
note: the evaluated program failed inside `bar`
88
--> $DIR/const_fn_ptr_fail2.rs:9:5
99
|
1010
LL | x(y)
11-
| ^^^^
11+
| ^^^^ the evaluated program failed here
1212

1313
error[E0080]: evaluation of constant value failed
1414
--> $DIR/const_fn_ptr_fail2.rs:14:18
1515
|
1616
LL | const Z: usize = bar(double, 2); // FIXME: should fail to typeck someday
1717
| ^^^^^^^^^^^^^^ calling non-const function `double`
1818
|
19-
note: inside `bar`
19+
note: the evaluated program failed inside `bar`
2020
--> $DIR/const_fn_ptr_fail2.rs:9:5
2121
|
2222
LL | x(y)
23-
| ^^^^
23+
| ^^^^ the evaluated program failed here
2424

2525
warning: skipping const checks
2626
|

tests/ui/consts/const-eval/const_panic_track_caller.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const fn b() -> u32 {
1313

1414
const fn c() -> u32 {
1515
b() //~ NOTE inside `c`
16+
//~^ NOTE the evaluated
1617
}
1718

1819
const X: u32 = c();

tests/ui/consts/const-eval/const_panic_track_caller.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/const_panic_track_caller.rs:18:16
2+
--> $DIR/const_panic_track_caller.rs:19:16
33
|
44
LL | const X: u32 = c();
55
| ^^^ panic: hey
66
|
7-
note: inside `c`
7+
note: the evaluated program failed inside `c`
88
--> $DIR/const_panic_track_caller.rs:15:5
99
|
1010
LL | b()
11-
| ^^^
11+
| ^^^ the evaluated program failed here
1212

1313
error: aborting due to 1 previous error
1414

tests/ui/consts/const-eval/heap/alloc_intrinsic_errors.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0080]: evaluation of constant value failed
44
LL | const FOO: i32 = foo();
55
| ^^^^^ invalid align passed to `const_allocate`: 3 is not a power of 2
66
|
7-
note: inside `foo`
7+
note: the evaluated program failed inside `foo`
88
--> $DIR/alloc_intrinsic_errors.rs:8:17
99
|
1010
LL | let _ = intrinsics::const_allocate(4, 3) as *mut i32;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program failed here
1212

1313
error: aborting due to 1 previous error
1414

0 commit comments

Comments
 (0)