mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 07:14:28 +00:00
Use None when the stack is empty
This commit is contained in:
parent
a312329b0a
commit
8961e13802
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user