Skip to content

Commit

Permalink
Use None when the stack is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Nov 25, 2022
1 parent 68223d6 commit 2c76f0c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/concurrency/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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()
}

Expand Down

0 comments on commit 2c76f0c

Please sign in to comment.