Use None when the stack is empty

This commit is contained in:
Ben Kimock 2022-11-25 13:12:43 -05:00
parent a312329b0a
commit 8961e13802
2 changed files with 11 additions and 10 deletions

View File

@ -179,12 +179,14 @@ impl<'mir, 'tcx> Thread<'mir, 'tcx> {
self.top_user_relevant_frame = Some(frame_idx);
}
pub fn top_user_relevant_frame(&self) -> usize {
/// Returns the topmost frame that is considered user-relevant, or the
/// top of the stack if there is no such frame, or `None` if the stack is empty.
pub fn top_user_relevant_frame(&self) -> Option<usize> {
debug_assert_eq!(self.top_user_relevant_frame, self.compute_top_user_relevant_frame());
// This can be called upon creation of an allocation. We create allocations while setting up
// parts of the Rust runtime when we do not have any stack frames yet, so we need to handle
// empty stacks.
self.top_user_relevant_frame.unwrap_or_else(|| self.stack.len().saturating_sub(1))
self.top_user_relevant_frame.or_else(|| self.stack.len().checked_sub(1))
}
}

View File

@ -940,9 +940,8 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
/// `#[track_caller]`.
/// This function is backed by a cache, and can be assumed to be very fast.
pub fn current_span(&self) -> Span {
self.stack()
.get(self.top_user_relevant_frame())
.map(Frame::current_span)
self.top_user_relevant_frame()
.map(|frame_idx| self.stack()[frame_idx].current_span())
.unwrap_or(rustc_span::DUMMY_SP)
}
@ -954,17 +953,17 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
pub fn caller_span(&self) -> Span {
// We need to go down at least to the caller (len - 2), or however
// far we have to go to find a frame in a local crate which is also not #[track_caller].
let frame_idx = self.top_user_relevant_frame();
let stack = self.stack();
let frame_idx = cmp::min(frame_idx, stack.len().saturating_sub(2));
stack.get(frame_idx).map(Frame::current_span).unwrap_or(rustc_span::DUMMY_SP)
self.top_user_relevant_frame()
.map(|frame_idx| cmp::min(frame_idx, self.stack().len() - 2))
.map(|frame_idx| self.stack()[frame_idx].current_span())
.unwrap_or(rustc_span::DUMMY_SP)
}
fn stack(&self) -> &[Frame<'mir, 'tcx, Provenance, machine::FrameData<'tcx>>] {
self.threads.active_thread_stack()
}
fn top_user_relevant_frame(&self) -> usize {
fn top_user_relevant_frame(&self) -> Option<usize> {
self.threads.active_thread_ref().top_user_relevant_frame()
}