Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d2d2bd2

Browse files
committed
Move generate_stacktrace_from_stack away from InterpCx to avoid having to know the Machine type
1 parent 6b936b6 commit d2d2bd2

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use rustc_middle::ty::TyCtxt;
88
use rustc_middle::ty::{layout::LayoutError, ConstInt};
99
use rustc_span::{Span, Symbol, DUMMY_SP};
1010

11-
use super::{CompileTimeInterpreter, InterpCx};
11+
use super::CompileTimeInterpreter;
1212
use crate::errors::{self, FrameNote, ReportErrorExt};
13-
use crate::interpret::{ErrorHandled, InterpError, InterpErrorInfo, MachineStopType};
13+
use crate::interpret::{ErrorHandled, Frame, InterpError, InterpErrorInfo, MachineStopType};
1414

1515
/// The CTFE machine has some custom error kinds.
1616
#[derive(Clone, Debug)]
@@ -63,10 +63,7 @@ pub fn get_span_and_frames<'tcx, 'mir>(
6363
where
6464
'tcx: 'mir,
6565
{
66-
let mut stacktrace =
67-
InterpCx::<CompileTimeInterpreter<'mir, 'tcx>>::generate_stacktrace_from_stack(
68-
&machine.stack,
69-
);
66+
let mut stacktrace = Frame::generate_stacktrace_from_stack(&machine.stack);
7067
// Filter out `requires_caller_location` frames.
7168
stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(*tcx));
7269
let span = stacktrace.first().map(|f| f.span).unwrap_or(tcx.span);

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_middle::mir::interpret::InterpErrorInfo;
55
use rustc_middle::query::TyCtxtAt;
66
use rustc_middle::ty::{self, Ty};
77

8-
use crate::interpret::{format_interp_error, InterpCx};
8+
use crate::interpret::format_interp_error;
99

1010
mod error;
1111
mod eval_queries;

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,32 @@ impl<'mir, 'tcx, Prov: Provenance, Extra> Frame<'mir, 'tcx, Prov, Extra> {
283283
pub(super) fn locals_addr(&self) -> usize {
284284
self.locals.raw.as_ptr().addr()
285285
}
286+
287+
#[must_use]
288+
pub fn generate_stacktrace_from_stack(stack: &[Self]) -> Vec<FrameInfo<'tcx>> {
289+
let mut frames = Vec::new();
290+
// This deliberately does *not* honor `requires_caller_location` since it is used for much
291+
// more than just panics.
292+
for frame in stack.iter().rev() {
293+
let span = match frame.loc {
294+
Left(loc) => {
295+
// If the stacktrace passes through MIR-inlined source scopes, add them.
296+
let mir::SourceInfo { mut span, scope } = *frame.body.source_info(loc);
297+
let mut scope_data = &frame.body.source_scopes[scope];
298+
while let Some((instance, call_span)) = scope_data.inlined {
299+
frames.push(FrameInfo { span, instance });
300+
span = call_span;
301+
scope_data = &frame.body.source_scopes[scope_data.parent_scope.unwrap()];
302+
}
303+
span
304+
}
305+
Right(span) => span,
306+
};
307+
frames.push(FrameInfo { span, instance: frame.instance });
308+
}
309+
trace!("generate stacktrace: {:#?}", frames);
310+
frames
311+
}
286312
}
287313

288314
// FIXME: only used by miri, should be removed once translatable.
@@ -1170,37 +1196,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
11701196
PlacePrinter { ecx: self, place: *place.place() }
11711197
}
11721198

1173-
#[must_use]
1174-
pub fn generate_stacktrace_from_stack(
1175-
stack: &[Frame<'mir, 'tcx, M::Provenance, M::FrameExtra>],
1176-
) -> Vec<FrameInfo<'tcx>> {
1177-
let mut frames = Vec::new();
1178-
// This deliberately does *not* honor `requires_caller_location` since it is used for much
1179-
// more than just panics.
1180-
for frame in stack.iter().rev() {
1181-
let span = match frame.loc {
1182-
Left(loc) => {
1183-
// If the stacktrace passes through MIR-inlined source scopes, add them.
1184-
let mir::SourceInfo { mut span, scope } = *frame.body.source_info(loc);
1185-
let mut scope_data = &frame.body.source_scopes[scope];
1186-
while let Some((instance, call_span)) = scope_data.inlined {
1187-
frames.push(FrameInfo { span, instance });
1188-
span = call_span;
1189-
scope_data = &frame.body.source_scopes[scope_data.parent_scope.unwrap()];
1190-
}
1191-
span
1192-
}
1193-
Right(span) => span,
1194-
};
1195-
frames.push(FrameInfo { span, instance: frame.instance });
1196-
}
1197-
trace!("generate stacktrace: {:#?}", frames);
1198-
frames
1199-
}
1200-
12011199
#[must_use]
12021200
pub fn generate_stacktrace(&self) -> Vec<FrameInfo<'tcx>> {
1203-
Self::generate_stacktrace_from_stack(self.stack())
1201+
Frame::generate_stacktrace_from_stack(self.stack())
12041202
}
12051203
}
12061204

src/tools/miri/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
528528
use NonHaltingDiagnostic::*;
529529

530530
let stacktrace =
531-
MiriInterpCx::generate_stacktrace_from_stack(self.threads.active_thread_stack());
531+
Frame::generate_stacktrace_from_stack(self.threads.active_thread_stack());
532532
let (stacktrace, _was_pruned) = prune_stacktrace(stacktrace, self);
533533

534534
let (title, diag_level) = match &e {

0 commit comments

Comments
 (0)