Skip to content

Commit 3669892

Browse files
authored
Unrolled build for rust-lang#134191
Rollup merge of rust-lang#134191 - willcrichton:dev, r=RalfJung,lqd Make some types and methods related to Polonius + Miri public We have a tool, [Aquascope](https://github.com/cognitive-engineering-lab/aquascope/), which uses Polonius and Miri to visualize the compile-time and run-time semantics of a Rust program. Changes in the last few months to both APIs have hidden away details we depend upon. This PR re-exposes some of those details, specifically: **Polonius:** - `BorrowSet` and `BorrowData` are added to `rustc_borrowck::consumers`, and their fields are made `pub` instead of `pub(crate)`. We need this to interpret the `BorrowIndex`es generated by Polonius. - `BorrowSet::build` is now `pub`. We need this because the borrowck API doesn't provide access to the `BorrowSet` constructed during checking. - `PoloniusRegionVid` is added to `rustc_borrowck::consumers`. We need this because it's also contained in the Polonius facts. **Miri:** - `InterpCx::local_to_op` is now a special case of `local_at_frame_to_op`, which allows querying locals in any frame. We need this because we walk the whole stack at each step to collect the state of memory. - `InterpCx::layout_of_local` is now `pub`. We need this because we need to know the layout of every local at each step. If these changes go against some design goal for keeping certain types private, please let me know so we can hash out a better solution. Additionally, if there's a better way to document that it's important that certain types stay public, also let me know. For example, `BorrowSet` was previously public but was hidden in 6676cec, breaking our build. cc ```@RalfJung``` ```@nnethercote``` ```@gavinleroy```
2 parents 85641f7 + 4d5d470 commit 3669892

File tree

6 files changed

+83
-18
lines changed

6 files changed

+83
-18
lines changed

compiler/rustc_borrowck/src/borrow_set.rs

+48-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ pub struct BorrowSet<'tcx> {
3434
pub(crate) locals_state_at_exit: LocalsStateAtExit,
3535
}
3636

37+
// These methods are public to support borrowck consumers.
38+
impl<'tcx> BorrowSet<'tcx> {
39+
pub fn location_map(&self) -> &FxIndexMap<Location, BorrowData<'tcx>> {
40+
&self.location_map
41+
}
42+
43+
pub fn activation_map(&self) -> &FxIndexMap<Location, Vec<BorrowIndex>> {
44+
&self.activation_map
45+
}
46+
47+
pub fn local_map(&self) -> &FxIndexMap<mir::Local, FxIndexSet<BorrowIndex>> {
48+
&self.local_map
49+
}
50+
51+
pub fn locals_state_at_exit(&self) -> &LocalsStateAtExit {
52+
&self.locals_state_at_exit
53+
}
54+
}
55+
3756
impl<'tcx> Index<BorrowIndex> for BorrowSet<'tcx> {
3857
type Output = BorrowData<'tcx>;
3958

@@ -45,7 +64,7 @@ impl<'tcx> Index<BorrowIndex> for BorrowSet<'tcx> {
4564
/// Location where a two-phase borrow is activated, if a borrow
4665
/// is in fact a two-phase borrow.
4766
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
48-
pub(crate) enum TwoPhaseActivation {
67+
pub enum TwoPhaseActivation {
4968
NotTwoPhase,
5069
NotActivated,
5170
ActivatedAt(Location),
@@ -68,6 +87,33 @@ pub struct BorrowData<'tcx> {
6887
pub(crate) assigned_place: mir::Place<'tcx>,
6988
}
7089

90+
// These methods are public to support borrowck consumers.
91+
impl<'tcx> BorrowData<'tcx> {
92+
pub fn reserve_location(&self) -> Location {
93+
self.reserve_location
94+
}
95+
96+
pub fn activation_location(&self) -> TwoPhaseActivation {
97+
self.activation_location
98+
}
99+
100+
pub fn kind(&self) -> mir::BorrowKind {
101+
self.kind
102+
}
103+
104+
pub fn region(&self) -> RegionVid {
105+
self.region
106+
}
107+
108+
pub fn borrowed_place(&self) -> mir::Place<'tcx> {
109+
self.borrowed_place
110+
}
111+
112+
pub fn assigned_place(&self) -> mir::Place<'tcx> {
113+
self.assigned_place
114+
}
115+
}
116+
71117
impl<'tcx> fmt::Display for BorrowData<'tcx> {
72118
fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
73119
let kind = match self.kind {
@@ -120,7 +166,7 @@ impl LocalsStateAtExit {
120166
}
121167

122168
impl<'tcx> BorrowSet<'tcx> {
123-
pub(crate) fn build(
169+
pub fn build(
124170
tcx: TyCtxt<'tcx>,
125171
body: &Body<'tcx>,
126172
locals_are_invalidated_at_exit: bool,

compiler/rustc_borrowck/src/consumers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use rustc_index::{IndexSlice, IndexVec};
55
use rustc_middle::mir::{Body, Promoted};
66
use rustc_middle::ty::TyCtxt;
77

8+
pub use super::borrow_set::{BorrowData, BorrowSet, TwoPhaseActivation};
89
pub use super::constraints::OutlivesConstraint;
910
pub use super::dataflow::{BorrowIndex, Borrows, calculate_borrows_out_of_scope_at_location};
10-
pub use super::facts::{AllFacts as PoloniusInput, RustcFacts};
11+
pub use super::facts::{AllFacts as PoloniusInput, PoloniusRegionVid, RustcFacts};
1112
pub use super::location::{LocationTable, RichLocation};
1213
pub use super::nll::PoloniusOutput;
1314
pub use super::place_ext::PlaceExt;
1415
pub use super::places_conflict::{PlaceConflictBias, places_conflict};
1516
pub use super::region_infer::RegionInferenceContext;
16-
use crate::borrow_set::BorrowSet;
1717

1818
/// Options determining the output behavior of [`get_body_with_borrowck_facts`].
1919
///

compiler/rustc_const_eval/src/interpret/machine.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,14 @@ pub trait Machine<'tcx>: Sized {
540540
interp_ok(ReturnAction::Normal)
541541
}
542542

543-
/// Called immediately after an "immediate" local variable is read
543+
/// Called immediately after an "immediate" local variable is read in a given frame
544544
/// (i.e., this is called for reads that do not end up accessing addressable memory).
545545
#[inline(always)]
546-
fn after_local_read(_ecx: &InterpCx<'tcx, Self>, _local: mir::Local) -> InterpResult<'tcx> {
546+
fn after_local_read(
547+
_ecx: &InterpCx<'tcx, Self>,
548+
_frame: &Frame<'tcx, Self::Provenance, Self::FrameExtra>,
549+
_local: mir::Local,
550+
) -> InterpResult<'tcx> {
547551
interp_ok(())
548552
}
549553

compiler/rustc_const_eval/src/interpret/operand.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use rustc_middle::{bug, mir, span_bug, ty};
1515
use tracing::trace;
1616

1717
use super::{
18-
CtfeProvenance, InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, OffsetMode,
19-
PlaceTy, Pointer, Projectable, Provenance, Scalar, alloc_range, err_ub, from_known_layout,
20-
interp_ok, mir_assign_valid_types, throw_ub,
18+
CtfeProvenance, Frame, InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta,
19+
OffsetMode, PlaceTy, Pointer, Projectable, Provenance, Scalar, alloc_range, err_ub,
20+
from_known_layout, interp_ok, mir_assign_valid_types, throw_ub,
2121
};
2222

2323
/// An `Immediate` represents a single immediate self-contained Rust value.
@@ -708,23 +708,32 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
708708
interp_ok(str)
709709
}
710710

711-
/// Read from a local of the current frame.
711+
/// Read from a local of the current frame. Convenience method for [`InterpCx::local_at_frame_to_op`].
712+
pub fn local_to_op(
713+
&self,
714+
local: mir::Local,
715+
layout: Option<TyAndLayout<'tcx>>,
716+
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
717+
self.local_at_frame_to_op(self.frame(), local, layout)
718+
}
719+
720+
/// Read from a local of a given frame.
712721
/// Will not access memory, instead an indirect `Operand` is returned.
713722
///
714-
/// This is public because it is used by [priroda](https://github.com/oli-obk/priroda) to get an
715-
/// OpTy from a local.
716-
pub fn local_to_op(
723+
/// This is public because it is used by [Aquascope](https://github.com/cognitive-engineering-lab/aquascope/)
724+
/// to get an OpTy from a local.
725+
pub fn local_at_frame_to_op(
717726
&self,
727+
frame: &Frame<'tcx, M::Provenance, M::FrameExtra>,
718728
local: mir::Local,
719729
layout: Option<TyAndLayout<'tcx>>,
720730
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
721-
let frame = self.frame();
722731
let layout = self.layout_of_local(frame, local, layout)?;
723732
let op = *frame.locals[local].access()?;
724733
if matches!(op, Operand::Immediate(_)) {
725734
assert!(!layout.is_unsized());
726735
}
727-
M::after_local_read(self, local)?;
736+
M::after_local_read(self, frame, local)?;
728737
interp_ok(OpTy { op, layout })
729738
}
730739

compiler/rustc_const_eval/src/interpret/stack.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
584584
interp_ok(())
585585
}
586586

587+
/// This is public because it is used by [Aquascope](https://github.com/cognitive-engineering-lab/aquascope/)
588+
/// to analyze all the locals in a stack frame.
587589
#[inline(always)]
588-
pub(super) fn layout_of_local(
590+
pub fn layout_of_local(
589591
&self,
590592
frame: &Frame<'tcx, M::Provenance, M::FrameExtra>,
591593
local: mir::Local,

src/tools/miri/src/machine.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1571,8 +1571,12 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
15711571
res
15721572
}
15731573

1574-
fn after_local_read(ecx: &InterpCx<'tcx, Self>, local: mir::Local) -> InterpResult<'tcx> {
1575-
if let Some(data_race) = &ecx.frame().extra.data_race {
1574+
fn after_local_read(
1575+
ecx: &InterpCx<'tcx, Self>,
1576+
frame: &Frame<'tcx, Provenance, FrameExtra<'tcx>>,
1577+
local: mir::Local,
1578+
) -> InterpResult<'tcx> {
1579+
if let Some(data_race) = &frame.extra.data_race {
15761580
data_race.local_read(local, &ecx.machine);
15771581
}
15781582
interp_ok(())

0 commit comments

Comments
 (0)