Skip to content

Commit c6df5b2

Browse files
committed
Actually factor out stacked_borrows::FrameExtra
1 parent c92ad33 commit c6df5b2

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/machine.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ use std::borrow::Cow;
55
use std::cell::RefCell;
66
use std::collections::HashSet;
77
use std::fmt;
8-
use std::num::NonZeroU64;
98
use std::time::Instant;
109

1110
use rand::rngs::StdRng;
1211
use rand::SeedableRng;
13-
use smallvec::SmallVec;
1412

1513
use rustc_ast::ast::Mutability;
1614
use rustc_data_structures::fx::FxHashMap;
@@ -44,7 +42,7 @@ pub const NUM_CPUS: u64 = 1;
4442
/// Extra data stored with each stack frame
4543
pub struct FrameData<'tcx> {
4644
/// Extra data for Stacked Borrows.
47-
pub stacked_borrows: stacked_borrows::FrameExtra,
45+
pub stacked_borrows: Option<stacked_borrows::FrameExtra>,
4846

4947
/// If this is Some(), then this is a special "catch unwind" frame (the frame of `try_fn`
5048
/// called by `try`). When this frame is popped during unwinding a panic,
@@ -892,15 +890,9 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
892890
};
893891

894892
let stacked_borrows = ecx.machine.stacked_borrows.as_ref();
895-
let call_id = stacked_borrows.map_or(NonZeroU64::new(1).unwrap(), |stacked_borrows| {
896-
stacked_borrows.borrow_mut().new_call()
897-
});
898893

899894
let extra = FrameData {
900-
stacked_borrows: stacked_borrows::FrameExtra {
901-
call_id,
902-
protected_tags: SmallVec::new(),
903-
},
895+
stacked_borrows: stacked_borrows.map(|sb| sb.borrow_mut().new_frame()),
904896
catch_unwind: None,
905897
timing,
906898
};

src/stacked_borrows.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl SbTagExtra {
8484
#[derive(Debug)]
8585
pub struct FrameExtra {
8686
/// The ID of the call this frame corresponds to.
87-
pub call_id: CallId,
87+
call_id: CallId,
8888

8989
/// If this frame is protecting any tags, they are listed here. We use this list to do
9090
/// incremental updates of the global list of protected tags stored in the
@@ -94,7 +94,7 @@ pub struct FrameExtra {
9494
///
9595
/// This will contain one tag per reference passed to the function, so
9696
/// a size of 2 is enough for the vast majority of functions.
97-
pub protected_tags: SmallVec<[SbTag; 2]>,
97+
protected_tags: SmallVec<[SbTag; 2]>,
9898
}
9999

100100
/// Extra per-allocation state.
@@ -202,18 +202,23 @@ impl GlobalStateInner {
202202
id
203203
}
204204

205-
pub fn new_call(&mut self) -> CallId {
206-
let id = self.next_call_id;
207-
trace!("new_call: Assigning ID {}", id);
208-
if self.tracked_call_ids.contains(&id) {
209-
register_diagnostic(NonHaltingDiagnostic::CreatedCallId(id));
205+
pub fn new_frame(&mut self) -> FrameExtra {
206+
let call_id = self.next_call_id;
207+
trace!("new_frame: Assigning call ID {}", call_id);
208+
if self.tracked_call_ids.contains(&call_id) {
209+
register_diagnostic(NonHaltingDiagnostic::CreatedCallId(call_id));
210210
}
211-
self.next_call_id = NonZeroU64::new(id.get() + 1).unwrap();
212-
id
211+
self.next_call_id = NonZeroU64::new(call_id.get() + 1).unwrap();
212+
FrameExtra { call_id, protected_tags: SmallVec::new() }
213213
}
214214

215215
pub fn end_call(&mut self, frame: &machine::FrameData<'_>) {
216-
for tag in &frame.stacked_borrows.protected_tags {
216+
for tag in &frame
217+
.stacked_borrows
218+
.as_ref()
219+
.expect("we should have Stacked Borrows data")
220+
.protected_tags
221+
{
217222
self.protected_tags.remove(tag);
218223
}
219224
}
@@ -342,8 +347,15 @@ impl<'tcx> Stack {
342347
let call_id = threads
343348
.all_stacks()
344349
.flatten()
345-
.find(|t| t.extra.stacked_borrows.protected_tags.contains(&item.tag()))
346-
.map(|frame| frame.extra.stacked_borrows.call_id)
350+
.map(|frame| {
351+
frame
352+
.extra
353+
.stacked_borrows
354+
.as_ref()
355+
.expect("we should have Stacked Borrows data")
356+
})
357+
.find(|frame| frame.protected_tags.contains(&item.tag()))
358+
.map(|frame| frame.call_id)
347359
.unwrap(); // FIXME: Surely we should find something, but a panic seems wrong here?
348360
if let Some((tag, _alloc_range, _offset, _access)) = provoking_access {
349361
Err(err_sb_ub(
@@ -835,7 +847,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
835847
);
836848

837849
if protect {
838-
this.frame_mut().extra.stacked_borrows.protected_tags.push(new_tag);
850+
this.frame_mut().extra.stacked_borrows.as_mut().unwrap().protected_tags.push(new_tag);
839851
this.machine.stacked_borrows.as_mut().unwrap().get_mut().protected_tags.insert(new_tag);
840852
}
841853
// FIXME: can't hold the current span handle across the borrows of self above

0 commit comments

Comments
 (0)