Skip to content

Commit 021d21c

Browse files
authored
Rollup merge of rust-lang#98549 - RalfJung:interpret-stacktraces, r=oli-obk
interpret: do not prune requires_caller_location stack frames quite so early rust-lang#87000 made the interpreter skip `caller_location` frames for its stacktraces and `cur_span`. However, those functions are used for much more than just panic reporting, and e.g. when Miri reports UB somewhere, it probably wants to point inside `caller_location` frames. (And if it did not, it would want to have its own logic to decide that, not be forced into it by the core interpreter engine.) This fixes some rare ICEs in Miri that say "we should never pop more than one frame at once". So let's remove all `caller_location` logic from the core interpreter, and instead move it to CTFE error reporting. This does not change user-visible behavior. That's the first commit. We might additionally want to change CTFE error reporting to treat panics differently from other errors: only prune `caller_location` frames for panics. The second commit does that. But honestly I am not sure if this is an improvement. r? ``@oli-obk``
2 parents b8bb6f9 + 852a111 commit 021d21c

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-18
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ impl<'tcx> ConstEvalErr<'tcx> {
8282
'tcx: 'mir,
8383
{
8484
error.print_backtrace();
85-
let stacktrace = ecx.generate_stacktrace();
86-
ConstEvalErr {
87-
error: error.into_kind(),
88-
stacktrace,
89-
span: span.unwrap_or_else(|| ecx.cur_span()),
90-
}
85+
let mut stacktrace = ecx.generate_stacktrace();
86+
// Filter out `requires_caller_location` frames.
87+
stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(*ecx.tcx));
88+
// If `span` is missing, use topmost remaining frame, or else the "root" span from `ecx.tcx`.
89+
let span = span.or_else(|| stacktrace.first().map(|f| f.span)).unwrap_or(ecx.tcx.span);
90+
ConstEvalErr { error: error.into_kind(), stacktrace, span }
9191
}
9292

9393
pub fn struct_error(

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
337337
}
338338
};
339339

340-
Err(err.report_as_error(ecx.tcx.at(ecx.cur_span()), &msg))
340+
Err(err.report_as_error(ecx.tcx.at(err.span), &msg))
341341
} else {
342342
let hir_id = tcx.hir().local_def_id_to_hir_id(def.as_local().unwrap().did);
343343
Err(err.report_as_lint(

compiler/rustc_const_eval/src/interpret/eval_context.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
428428

429429
#[inline(always)]
430430
pub fn cur_span(&self) -> Span {
431-
self.stack()
432-
.iter()
433-
.rev()
434-
.find(|frame| !frame.instance.def.requires_caller_location(*self.tcx))
435-
.map_or(self.tcx.span, |f| f.current_span())
431+
// This deliberately does *not* honor `requires_caller_location` since it is used for much
432+
// more than just panics.
433+
self.stack().last().map_or(self.tcx.span, |f| f.current_span())
436434
}
437435

438436
#[inline(always)]
@@ -939,12 +937,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
939937
#[must_use]
940938
pub fn generate_stacktrace(&self) -> Vec<FrameInfo<'tcx>> {
941939
let mut frames = Vec::new();
942-
for frame in self
943-
.stack()
944-
.iter()
945-
.rev()
946-
.skip_while(|frame| frame.instance.def.requires_caller_location(*self.tcx))
947-
{
940+
// This deliberately does *not* honor `requires_caller_location` since it is used for much
941+
// more than just panics.
942+
for frame in self.stack().iter().rev() {
948943
let lint_root = frame.current_source_info().and_then(|source_info| {
949944
match &frame.body.source_scopes[source_info.scope].local_data {
950945
mir::ClearCrossCrate::Set(data) => Some(data.lint_root),

0 commit comments

Comments
 (0)