]> git.lizzy.rs Git - rust.git/commitdiff
Use None when the stack is empty
authorBen Kimock <kimockb@gmail.com>
Fri, 25 Nov 2022 18:12:43 +0000 (13:12 -0500)
committerBen Kimock <kimockb@gmail.com>
Fri, 25 Nov 2022 18:13:11 +0000 (13:13 -0500)
src/tools/miri/src/concurrency/thread.rs
src/tools/miri/src/helpers.rs

index 96d988eb9a3bbdfaa8b04a64cfbd757a0c4bdf8b..dacb3a9b88f8f660f67bb234c552f7fbf90960f3 100644 (file)
@@ -179,12 +179,14 @@ pub fn set_top_user_relevant_frame(&mut self, frame_idx: usize) {
         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))
     }
 }
 
index 70971cdc20ee2ba78c615f663fbd1aa430fa58ca..cfa47c5c004d564eee5ae7ab86634c0c6961555d 100644 (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 @@ pub fn current_span(&self) -> Span {
     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()
     }