Skip to content

Commit 8961e13

Browse files
committed
Use None when the stack is empty
1 parent a312329 commit 8961e13

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/tools/miri/src/concurrency/thread.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ impl<'mir, 'tcx> Thread<'mir, 'tcx> {
179179
self.top_user_relevant_frame = Some(frame_idx);
180180
}
181181

182-
pub fn top_user_relevant_frame(&self) -> usize {
182+
/// Returns the topmost frame that is considered user-relevant, or the
183+
/// top of the stack if there is no such frame, or `None` if the stack is empty.
184+
pub fn top_user_relevant_frame(&self) -> Option<usize> {
183185
debug_assert_eq!(self.top_user_relevant_frame, self.compute_top_user_relevant_frame());
184186
// This can be called upon creation of an allocation. We create allocations while setting up
185187
// parts of the Rust runtime when we do not have any stack frames yet, so we need to handle
186188
// empty stacks.
187-
self.top_user_relevant_frame.unwrap_or_else(|| self.stack.len().saturating_sub(1))
189+
self.top_user_relevant_frame.or_else(|| self.stack.len().checked_sub(1))
188190
}
189191
}
190192

src/tools/miri/src/helpers.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -940,9 +940,8 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
940940
/// `#[track_caller]`.
941941
/// This function is backed by a cache, and can be assumed to be very fast.
942942
pub fn current_span(&self) -> Span {
943-
self.stack()
944-
.get(self.top_user_relevant_frame())
945-
.map(Frame::current_span)
943+
self.top_user_relevant_frame()
944+
.map(|frame_idx| self.stack()[frame_idx].current_span())
946945
.unwrap_or(rustc_span::DUMMY_SP)
947946
}
948947

@@ -954,17 +953,17 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
954953
pub fn caller_span(&self) -> Span {
955954
// We need to go down at least to the caller (len - 2), or however
956955
// far we have to go to find a frame in a local crate which is also not #[track_caller].
957-
let frame_idx = self.top_user_relevant_frame();
958-
let stack = self.stack();
959-
let frame_idx = cmp::min(frame_idx, stack.len().saturating_sub(2));
960-
stack.get(frame_idx).map(Frame::current_span).unwrap_or(rustc_span::DUMMY_SP)
956+
self.top_user_relevant_frame()
957+
.map(|frame_idx| cmp::min(frame_idx, self.stack().len() - 2))
958+
.map(|frame_idx| self.stack()[frame_idx].current_span())
959+
.unwrap_or(rustc_span::DUMMY_SP)
961960
}
962961

963962
fn stack(&self) -> &[Frame<'mir, 'tcx, Provenance, machine::FrameData<'tcx>>] {
964963
self.threads.active_thread_stack()
965964
}
966965

967-
fn top_user_relevant_frame(&self) -> usize {
966+
fn top_user_relevant_frame(&self) -> Option<usize> {
968967
self.threads.active_thread_ref().top_user_relevant_frame()
969968
}
970969

0 commit comments

Comments
 (0)