1
Fork 0

Make BorrowSet/BorrowData fields accessible via public getters

This commit is contained in:
Will Crichton 2024-12-12 12:34:43 -08:00
parent eb10db0a76
commit 4d5d4700f3
4 changed files with 71 additions and 18 deletions

View file

@ -20,18 +20,37 @@ pub struct BorrowSet<'tcx> {
/// by the `Location` of the assignment statement in which it /// by the `Location` of the assignment statement in which it
/// appears on the right hand side. Thus the location is the map /// appears on the right hand side. Thus the location is the map
/// key, and its position in the map corresponds to `BorrowIndex`. /// key, and its position in the map corresponds to `BorrowIndex`.
pub location_map: FxIndexMap<Location, BorrowData<'tcx>>, pub(crate) location_map: FxIndexMap<Location, BorrowData<'tcx>>,
/// Locations which activate borrows. /// Locations which activate borrows.
/// NOTE: a given location may activate more than one borrow in the future /// NOTE: a given location may activate more than one borrow in the future
/// when more general two-phase borrow support is introduced, but for now we /// when more general two-phase borrow support is introduced, but for now we
/// only need to store one borrow index. /// only need to store one borrow index.
pub activation_map: FxIndexMap<Location, Vec<BorrowIndex>>, pub(crate) activation_map: FxIndexMap<Location, Vec<BorrowIndex>>,
/// Map from local to all the borrows on that local. /// Map from local to all the borrows on that local.
pub local_map: FxIndexMap<mir::Local, FxIndexSet<BorrowIndex>>, pub(crate) local_map: FxIndexMap<mir::Local, FxIndexSet<BorrowIndex>>,
pub locals_state_at_exit: LocalsStateAtExit, pub(crate) locals_state_at_exit: LocalsStateAtExit,
}
// These methods are public to support borrowck consumers.
impl<'tcx> BorrowSet<'tcx> {
pub fn location_map(&self) -> &FxIndexMap<Location, BorrowData<'tcx>> {
&self.location_map
}
pub fn activation_map(&self) -> &FxIndexMap<Location, Vec<BorrowIndex>> {
&self.activation_map
}
pub fn local_map(&self) -> &FxIndexMap<mir::Local, FxIndexSet<BorrowIndex>> {
&self.local_map
}
pub fn locals_state_at_exit(&self) -> &LocalsStateAtExit {
&self.locals_state_at_exit
}
} }
impl<'tcx> Index<BorrowIndex> for BorrowSet<'tcx> { impl<'tcx> Index<BorrowIndex> for BorrowSet<'tcx> {
@ -55,17 +74,44 @@ pub enum TwoPhaseActivation {
pub struct BorrowData<'tcx> { pub struct BorrowData<'tcx> {
/// Location where the borrow reservation starts. /// Location where the borrow reservation starts.
/// In many cases, this will be equal to the activation location but not always. /// In many cases, this will be equal to the activation location but not always.
pub reserve_location: Location, pub(crate) reserve_location: Location,
/// Location where the borrow is activated. /// Location where the borrow is activated.
pub activation_location: TwoPhaseActivation, pub(crate) activation_location: TwoPhaseActivation,
/// What kind of borrow this is /// What kind of borrow this is
pub kind: mir::BorrowKind, pub(crate) kind: mir::BorrowKind,
/// The region for which this borrow is live /// The region for which this borrow is live
pub region: RegionVid, pub(crate) region: RegionVid,
/// Place from which we are borrowing /// Place from which we are borrowing
pub borrowed_place: mir::Place<'tcx>, pub(crate) borrowed_place: mir::Place<'tcx>,
/// Place to which the borrow was stored /// Place to which the borrow was stored
pub assigned_place: mir::Place<'tcx>, pub(crate) assigned_place: mir::Place<'tcx>,
}
// These methods are public to support borrowck consumers.
impl<'tcx> BorrowData<'tcx> {
pub fn reserve_location(&self) -> Location {
self.reserve_location
}
pub fn activation_location(&self) -> TwoPhaseActivation {
self.activation_location
}
pub fn kind(&self) -> mir::BorrowKind {
self.kind
}
pub fn region(&self) -> RegionVid {
self.region
}
pub fn borrowed_place(&self) -> mir::Place<'tcx> {
self.borrowed_place
}
pub fn assigned_place(&self) -> mir::Place<'tcx> {
self.assigned_place
}
} }
impl<'tcx> fmt::Display for BorrowData<'tcx> { impl<'tcx> fmt::Display for BorrowData<'tcx> {

View file

@ -540,10 +540,14 @@ pub trait Machine<'tcx>: Sized {
interp_ok(ReturnAction::Normal) interp_ok(ReturnAction::Normal)
} }
/// Called immediately after an "immediate" local variable is read /// Called immediately after an "immediate" local variable is read in a given frame
/// (i.e., this is called for reads that do not end up accessing addressable memory). /// (i.e., this is called for reads that do not end up accessing addressable memory).
#[inline(always)] #[inline(always)]
fn after_local_read(_ecx: &InterpCx<'tcx, Self>, _local: mir::Local) -> InterpResult<'tcx> { fn after_local_read(
_ecx: &InterpCx<'tcx, Self>,
_frame: &Frame<'tcx, Self::Provenance, Self::FrameExtra>,
_local: mir::Local,
) -> InterpResult<'tcx> {
interp_ok(()) interp_ok(())
} }

View file

@ -718,9 +718,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
/// Read from a local of a given frame. /// Read from a local of a given frame.
/// Will not access memory, instead an indirect `Operand` is returned. /// Will not access memory, instead an indirect `Operand` is returned.
/// ///
/// This is public because it is used by [priroda](https://github.com/oli-obk/priroda) and /// This is public because it is used by [Aquascope](https://github.com/cognitive-engineering-lab/aquascope/)
/// [Aquascope](https://github.com/cognitive-engineering-lab/aquascope/) to get an /// to get an OpTy from a local.
/// OpTy from a local.
pub fn local_at_frame_to_op( pub fn local_at_frame_to_op(
&self, &self,
frame: &Frame<'tcx, M::Provenance, M::FrameExtra>, frame: &Frame<'tcx, M::Provenance, M::FrameExtra>,
@ -732,7 +731,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
if matches!(op, Operand::Immediate(_)) { if matches!(op, Operand::Immediate(_)) {
assert!(!layout.is_unsized()); assert!(!layout.is_unsized());
} }
M::after_local_read(self, local)?; M::after_local_read(self, frame, local)?;
interp_ok(OpTy { op, layout }) interp_ok(OpTy { op, layout })
} }

View file

@ -1571,8 +1571,12 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
res res
} }
fn after_local_read(ecx: &InterpCx<'tcx, Self>, local: mir::Local) -> InterpResult<'tcx> { fn after_local_read(
if let Some(data_race) = &ecx.frame().extra.data_race { ecx: &InterpCx<'tcx, Self>,
frame: &Frame<'tcx, Provenance, FrameExtra<'tcx>>,
local: mir::Local,
) -> InterpResult<'tcx> {
if let Some(data_race) = &frame.extra.data_race {
data_race.local_read(local, &ecx.machine); data_race.local_read(local, &ecx.machine);
} }
interp_ok(()) interp_ok(())