Skip to content

Commit 2d83c89

Browse files
committed
WIP
1 parent 4bfb50c commit 2d83c89

23 files changed

+487
-487
lines changed

Diff for: src/borrows/borrow_edge.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ use super::{
77
has_pcs_elem::HasPcsElems,
88
region_projection::RegionProjection,
99
};
10-
use crate::rustc_interface::{
10+
use crate::{rustc_interface::{
1111
ast::Mutability,
1212
data_structures::fx::FxHashSet,
1313
middle::{
1414
mir::Location,
1515
ty::{self},
1616
},
17-
};
17+
}, utils::display::DisplayWithRepacker};
1818
use crate::utils::PlaceRepacker;
1919

2020
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
@@ -31,6 +31,17 @@ pub struct BorrowEdge<'tcx> {
3131
pub region: ty::Region<'tcx>,
3232
}
3333

34+
impl<'tcx> DisplayWithRepacker<'tcx> for BorrowEdge<'tcx> {
35+
fn to_short_string(&self, repacker: PlaceRepacker<'_, 'tcx>) -> String {
36+
format!(
37+
"borrow: {} = &{} {}",
38+
self.assigned_ref.to_short_string(repacker),
39+
if self.mutability == Mutability::Mut { "mut " } else { "" },
40+
self.blocked_place.to_short_string(repacker)
41+
)
42+
}
43+
}
44+
3445
impl<'tcx, T> HasPcsElems<RegionProjection<'tcx, T>> for BorrowEdge<'tcx> {
3546
fn pcs_elems(&mut self) -> Vec<&mut RegionProjection<'tcx, T>> {
3647
vec![]

Diff for: src/borrows/borrow_pcg_action.rs

+96-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use tracing::instrument;
22

33
use crate::free_pcs::CapabilityKind;
44
use crate::rustc_interface::{ast::Mutability, middle::mir::Location};
5+
use crate::utils::display::DisplayWithRepacker;
56
use crate::utils::{Place, PlaceRepacker, SnapshotLocation};
7+
use crate::{RestoreCapability, Weaken};
68

79
use super::borrow_pcg_edge::{BorrowPCGEdge, LocalNode, ToBorrowsEdge};
810
use super::borrow_pcg_expansion::BorrowPCGExpansion;
@@ -22,11 +24,67 @@ use super::region_projection_member::RegionProjectionMember;
2224
/// in the `BorrowsVisitor`?
2325
#[derive(Clone, Debug)]
2426
pub struct BorrowPCGAction<'tcx> {
25-
kind: BorrowPCGActionKind<'tcx>,
27+
pub(crate) kind: BorrowPCGActionKind<'tcx>,
2628
debug_context: Option<String>,
2729
}
2830

2931
impl<'tcx> BorrowPCGAction<'tcx> {
32+
pub(crate) fn debug_line(&self, repacker: PlaceRepacker<'_, 'tcx>) -> String {
33+
match &self.kind {
34+
BorrowPCGActionKind::Weaken(weaken) => weaken.debug_line(repacker),
35+
BorrowPCGActionKind::Restore(restore_capability) => {
36+
restore_capability.debug_line(repacker)
37+
}
38+
BorrowPCGActionKind::MakePlaceOld(place) => {
39+
format!("Make {} an old place", place.to_short_string(repacker))
40+
}
41+
BorrowPCGActionKind::SetLatest(place, location) => format!(
42+
"Set Latest of {} to {:?}",
43+
place.to_short_string(repacker),
44+
location
45+
),
46+
BorrowPCGActionKind::RemoveEdge(borrow_pcgedge) => {
47+
format!("Remove Edge {}", borrow_pcgedge.to_short_string(repacker))
48+
}
49+
BorrowPCGActionKind::AddRegionProjectionMember(
50+
region_projection_member,
51+
path_conditions,
52+
) => format!(
53+
"Add Region Projection Member: {}; path conditions: {}",
54+
region_projection_member.to_short_string(repacker),
55+
path_conditions
56+
),
57+
BorrowPCGActionKind::InsertBorrowPCGExpansion(borrow_pcgexpansion, location) => {
58+
format!(
59+
"Insert Expansion {} at {:?}",
60+
borrow_pcgexpansion.to_short_string(repacker),
61+
location
62+
)
63+
}
64+
BorrowPCGActionKind::RenamePlace { old, new } => {
65+
format!("Rename {:?} to {:?}", old, new)
66+
}
67+
}
68+
}
69+
70+
pub fn kind(&self) -> &BorrowPCGActionKind<'tcx> {
71+
&self.kind
72+
}
73+
74+
pub(super) fn restore_capability(node: LocalNode<'tcx>, capability: CapabilityKind) -> Self {
75+
BorrowPCGAction {
76+
kind: BorrowPCGActionKind::Restore(RestoreCapability::new(node, capability)),
77+
debug_context: None,
78+
}
79+
}
80+
81+
pub(super) fn weaken(place: Place<'tcx>, from: CapabilityKind, to: CapabilityKind) -> Self {
82+
BorrowPCGAction {
83+
kind: BorrowPCGActionKind::Weaken(Weaken::new(place, from, to)),
84+
debug_context: None,
85+
}
86+
}
87+
3088
pub(super) fn rename_place(old: MaybeOldPlace<'tcx>, new: MaybeOldPlace<'tcx>) -> Self {
3189
BorrowPCGAction {
3290
kind: BorrowPCGActionKind::RenamePlace { old, new },
@@ -91,7 +149,9 @@ impl<'tcx> From<BorrowPCGActionKind<'tcx>> for BorrowPCGAction<'tcx> {
91149
}
92150

93151
#[derive(Clone, Debug)]
94-
pub(crate) enum BorrowPCGActionKind<'tcx> {
152+
pub enum BorrowPCGActionKind<'tcx> {
153+
Weaken(Weaken<'tcx>),
154+
Restore(RestoreCapability<'tcx>),
95155
MakePlaceOld(Place<'tcx>),
96156
SetLatest(Place<'tcx>, Location),
97157
RemoveEdge(BorrowPCGEdge<'tcx>),
@@ -121,6 +181,16 @@ impl<'tcx> BorrowsState<'tcx> {
121181
repacker: PlaceRepacker<'_, 'tcx>,
122182
) -> bool {
123183
let result = match action.kind {
184+
BorrowPCGActionKind::Restore(restore) => {
185+
assert!(self.get_capability(restore.node()).unwrap() < restore.capability());
186+
assert!(self.set_capability(restore.node(), restore.capability()));
187+
true
188+
}
189+
BorrowPCGActionKind::Weaken(weaken) => {
190+
assert_eq!(self.get_capability(weaken.place()), Some(weaken.from));
191+
assert!(self.set_capability(weaken.place(), weaken.to));
192+
true
193+
}
124194
BorrowPCGActionKind::MakePlaceOld(place) => self.make_place_old(place, repacker, None),
125195
BorrowPCGActionKind::SetLatest(place, location) => {
126196
self.set_latest(place, location, repacker)
@@ -130,34 +200,45 @@ impl<'tcx> BorrowsState<'tcx> {
130200
self.add_region_projection_member(member, pc, repacker)
131201
}
132202
BorrowPCGActionKind::InsertBorrowPCGExpansion(de, location) => {
133-
let inserted = self.insert(
203+
let updated = self.insert(
134204
de.clone()
135205
.to_borrow_pcg_edge(PathConditions::new(location.block)),
136206
);
137-
if inserted {
207+
if updated {
138208
let base = de.base();
139-
let capability = match self.get_capability(base) {
140-
Some(c) => c,
141-
None => {
142-
// The expansion presumably already exists under
143-
// different path conditions. TODO: Maybe this is
144-
// worth checking?
145-
return inserted;
209+
let capability = match &de {
210+
BorrowPCGExpansion::FromOwned(expansion_of_owned) => {
211+
match expansion_of_owned.base().ty(repacker).ty.ref_mutability() {
212+
Some(Mutability::Mut) => CapabilityKind::Exclusive,
213+
Some(Mutability::Not) => CapabilityKind::Read,
214+
None => unreachable!(),
215+
}
216+
}
217+
BorrowPCGExpansion::FromBorrow(expansion_of_borrowed) => {
218+
if let Some(capability) =
219+
self.get_capability(expansion_of_borrowed.base)
220+
{
221+
capability
222+
} else {
223+
// Presumably already expanded in another branch
224+
// TODO: Check expansion capability exists
225+
return true;
226+
}
146227
}
147228
};
148229
match capability {
149230
CapabilityKind::Read => {
150-
self.set_capability(base, CapabilityKind::Read);
231+
_ = self.set_capability(base, CapabilityKind::Read);
151232
}
152233
_ => {
153-
self.remove_capability(base);
234+
_ = self.remove_capability(base);
154235
}
155236
}
156237
for p in de.expansion(repacker).iter() {
157-
self.set_capability(*p, capability);
238+
_ = self.set_capability(*p, capability);
158239
}
159240
}
160-
inserted
241+
updated
161242
}
162243
BorrowPCGActionKind::RenamePlace { old, new } => self.change_pcs_elem(old, new),
163244
};

Diff for: src/borrows/borrow_pcg_capabilities.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22

3-
use crate::{free_pcs::CapabilityKind, utils::Place};
3+
use crate::free_pcs::CapabilityKind;
44

55
use super::borrow_pcg_edge::PCGNode;
66

@@ -41,14 +41,6 @@ impl<'tcx> BorrowPCGCapabilities<'tcx> {
4141
self.0.iter().map(|(k, v)| (k.clone(), v.clone()))
4242
}
4343

44-
pub(crate) fn place_capabilities(
45-
&self,
46-
) -> impl Iterator<Item = (Place<'tcx>, CapabilityKind)> + '_ {
47-
self.0
48-
.iter()
49-
.flat_map(|(k, v)| k.as_current_place().map(|p| (p, v.clone())))
50-
}
51-
5244
pub(crate) fn get<T: Into<PCGNode<'tcx>>>(&self, node: T) -> Option<CapabilityKind> {
5345
self.0.get(&node.into()).cloned()
5446
}

Diff for: src/borrows/borrow_pcg_edge.rs

+37-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_interface::{
66

77
use crate::{
88
edgedata_enum, rustc_interface,
9-
utils::{HasPlace, Place, PlaceRepacker},
9+
utils::{display::DisplayWithRepacker, HasPlace, Place, PlaceRepacker},
1010
};
1111

1212
use super::{
@@ -29,6 +29,16 @@ pub struct BorrowPCGEdge<'tcx> {
2929
pub(crate) kind: BorrowPCGEdgeKind<'tcx>,
3030
}
3131

32+
impl<'tcx> DisplayWithRepacker<'tcx> for BorrowPCGEdge<'tcx> {
33+
fn to_short_string(&self, repacker: PlaceRepacker<'_, 'tcx>) -> String {
34+
format!(
35+
"{} under conditions {:?}",
36+
self.kind.to_short_string(repacker),
37+
self.conditions
38+
)
39+
}
40+
}
41+
3242
/// Any node in the PCG that is "local" in the sense that it can be named
3343
/// (include nodes that potentially refer to a past program point), i.e. any
3444
/// node other than a [`super::domain::RemotePlace`]
@@ -102,12 +112,12 @@ impl<'tcx> HasPlace<'tcx> for LocalNode<'tcx> {
102112
}
103113

104114
impl<'tcx> LocalNode<'tcx> {
105-
pub(crate) fn is_current(&self) -> bool {
106-
match self {
107-
LocalNode::Place(p) => p.is_current(),
108-
LocalNode::RegionProjection(rp) => rp.place.is_current(),
109-
}
110-
}
115+
// pub(crate) fn is_current(&self) -> bool {
116+
// match self {
117+
// LocalNode::Place(p) => p.is_current(),
118+
// LocalNode::RegionProjection(rp) => rp.place.is_current(),
119+
// }
120+
// }
111121

112122
pub fn as_cg_node(&self) -> Option<CGNode<'tcx>> {
113123
match self {
@@ -131,6 +141,15 @@ pub enum PCGNode<'tcx, T = MaybeRemotePlace<'tcx>> {
131141
RegionProjection(RegionProjection<'tcx, T>),
132142
}
133143

144+
impl<'tcx, T: DisplayWithRepacker<'tcx>> DisplayWithRepacker<'tcx> for PCGNode<'tcx, T> {
145+
fn to_short_string(&self, repacker: PlaceRepacker<'_, 'tcx>) -> String {
146+
match self {
147+
PCGNode::Place(p) => p.to_short_string(repacker),
148+
PCGNode::RegionProjection(rp) => rp.to_short_string(repacker),
149+
}
150+
}
151+
}
152+
134153
impl<'tcx, T: std::fmt::Display> std::fmt::Display for PCGNode<'tcx, T> {
135154
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136155
match self {
@@ -370,6 +389,17 @@ pub enum BorrowPCGEdgeKind<'tcx> {
370389
RegionProjectionMember(RegionProjectionMember<'tcx>),
371390
}
372391

392+
impl<'tcx> DisplayWithRepacker<'tcx> for BorrowPCGEdgeKind<'tcx> {
393+
fn to_short_string(&self, repacker: PlaceRepacker<'_, 'tcx>) -> String {
394+
match self {
395+
BorrowPCGEdgeKind::Borrow(borrow) => borrow.to_short_string(repacker),
396+
BorrowPCGEdgeKind::BorrowPCGExpansion(expansion) => expansion.to_short_string(repacker),
397+
BorrowPCGEdgeKind::Abstraction(abstraction) => abstraction.to_short_string(repacker),
398+
BorrowPCGEdgeKind::RegionProjectionMember(member) => member.to_short_string(repacker),
399+
}
400+
}
401+
}
402+
373403
edgedata_enum!(
374404
BorrowPCGEdgeKind<'tcx>,
375405
Borrow(BorrowEdge<'tcx>),

Diff for: src/borrows/borrow_pcg_expansion.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use crate::{
1414
span::Symbol,
1515
target::abi::{FieldIdx, VariantIdx},
1616
},
17-
utils::{ConstantIndex, CorrectedPlace, HasPlace, Place, PlaceRepacker},
17+
utils::{
18+
display::DisplayWithRepacker, ConstantIndex, CorrectedPlace, HasPlace, Place, PlaceRepacker,
19+
},
1820
};
1921

2022
use super::{
@@ -264,6 +266,30 @@ pub enum BorrowPCGExpansion<'tcx, P = LocalNode<'tcx>> {
264266
FromBorrow(ExpansionOfBorrowed<'tcx, P>),
265267
}
266268

269+
impl<
270+
'tcx,
271+
T: std::hash::Hash
272+
+ PartialEq
273+
+ Eq
274+
+ Copy
275+
+ HasPlace<'tcx>
276+
+ DisplayWithRepacker<'tcx>
277+
+ From<MaybeOldPlace<'tcx>>,
278+
> DisplayWithRepacker<'tcx> for BorrowPCGExpansion<'tcx, T>
279+
{
280+
fn to_short_string(&self, repacker: PlaceRepacker<'_, 'tcx>) -> String {
281+
format!(
282+
"{{{}}} -> {{{}}}",
283+
self.base().to_short_string(repacker),
284+
self.expansion(repacker)
285+
.iter()
286+
.map(|p| p.to_short_string(repacker))
287+
.collect::<Vec<_>>()
288+
.join(", ")
289+
)
290+
}
291+
}
292+
267293
impl<'tcx> TryFrom<BorrowPCGExpansion<'tcx, LocalNode<'tcx>>>
268294
for BorrowPCGExpansion<'tcx, MaybeOldPlace<'tcx>>
269295
{

0 commit comments

Comments
 (0)