Skip to content

Commit b5e770d

Browse files
committed
Finish some refactoring
1 parent 786885f commit b5e770d

File tree

10 files changed

+29
-134
lines changed

10 files changed

+29
-134
lines changed

src/borrow_pcg/action/mod.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use tracing::instrument;
22

3-
use super::borrow_pcg_edge::{BorrowPCGEdge, LocalNode, ToBorrowsEdge};
3+
use super::borrow_pcg_edge::{BorrowPCGEdge, LocalNode};
44
use super::borrow_pcg_expansion::BorrowPCGExpansion;
55
use super::edge::block::BlockEdge;
66
use super::edge::kind::BorrowPCGEdgeKind;
7-
use super::path_condition::PathConditions;
87
use super::state::BorrowsState;
9-
use crate::borrow_pcg::edge::abstraction::AbstractionType;
10-
use crate::combined_pcs::{PCGError, PCGNode, PCGNodeLike};
8+
use crate::combined_pcs::{PCGError, PCGNode};
119
use crate::free_pcs::CapabilityKind;
1210
use crate::rustc_interface::{ast::Mutability, middle::mir::Location};
1311
use crate::utils::display::{DebugLines, DisplayWithRepacker};
@@ -287,13 +285,7 @@ impl<'tcx> BorrowsState<'tcx> {
287285
}
288286
BorrowPCGEdgeKind::Abstraction(_) => changed,
289287
BorrowPCGEdgeKind::Block(block_edge) => {
290-
changed
291-
|| self.set_capabilities_for_block_edge(
292-
block_edge,
293-
edge.conditions,
294-
for_exclusive,
295-
repacker,
296-
)
288+
changed || self.set_capabilities_for_block_edge(block_edge, for_exclusive, repacker)
297289
}
298290
})
299291
}
@@ -304,7 +296,6 @@ impl<'tcx> BorrowsState<'tcx> {
304296
fn set_capabilities_for_block_edge(
305297
&mut self,
306298
member: BlockEdge<'tcx>,
307-
pc: PathConditions,
308299
for_exclusive: bool,
309300
repacker: PlaceRepacker<'_, 'tcx>,
310301
) -> bool {

src/borrow_pcg/borrow_pcg_capabilities.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ use crate::{
55
free_pcs::CapabilityKind,
66
rustc_interface::data_structures::fx::FxHashMap,
77
utils::{
8-
display::{DebugLines, DisplayWithRepacker}, maybe_old::MaybeOldPlace, Place, PlaceRepacker
8+
display::{DebugLines, DisplayWithRepacker},
9+
maybe_old::MaybeOldPlace,
10+
Place, PlaceRepacker,
911
},
1012
};
1113

12-
use super::{has_pcs_elem::{HasPcsElems, MakePlaceOld}, latest::Latest};
14+
use super::{
15+
has_pcs_elem::{HasPcsElems, MakePlaceOld},
16+
latest::Latest,
17+
};
1318

1419
/// Tracks the capabilities of places in the borrow PCG. We don't store this
1520
/// information in the borrows graph directly to facilitate simpler logic for

src/borrow_pcg/borrow_pcg_edge.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_interface::{
2-
ast::Mutability,
32
data_structures::fx::FxHashSet,
43
middle::mir::{self, BasicBlock, PlaceElem},
54
};
@@ -268,13 +267,6 @@ impl<'tcx> From<CGNode<'tcx>> for PCGNode<'tcx> {
268267
pub type BlockedNode<'tcx> = PCGNode<'tcx>;
269268

270269
impl<'tcx> PCGNode<'tcx> {
271-
pub(crate) fn mutability(&self, repacker: PlaceRepacker<'_, 'tcx>) -> Mutability {
272-
match self {
273-
PCGNode::Place(rp) => rp.mutability(repacker),
274-
PCGNode::RegionProjection(rp) => rp.mutability(repacker),
275-
}
276-
}
277-
278270
pub(crate) fn as_cg_node(self) -> Option<CGNode<'tcx>> {
279271
match self {
280272
PCGNode::Place(MaybeRemotePlace::Remote(remote_place)) => {

src/borrow_pcg/edge/block.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use serde_json::json;
22
use smallvec::SmallVec;
33

44
use crate::combined_pcs::PCGNode;
5-
use crate::pcg_validity_assert;
6-
use crate::rustc_interface::{ast::Mutability, data_structures::fx::FxHashSet};
5+
use crate::rustc_interface::data_structures::fx::FxHashSet;
76
use crate::utils::display::DisplayWithRepacker;
87
use crate::utils::place::maybe_old::MaybeOldPlace;
98
use crate::utils::validity::HasValidityCheck;
@@ -164,19 +163,6 @@ impl<'tcx> HasPcsElems<MaybeOldPlace<'tcx>> for BlockEdge<'tcx> {
164163
}
165164

166165
impl<'tcx> BlockEdge<'tcx> {
167-
/// Returns `true` iff the lifetime projections are mutable
168-
pub(crate) fn mutability(&self, repacker: PlaceRepacker<'_, 'tcx>) -> Mutability {
169-
let mut_values = self
170-
.inputs
171-
.iter()
172-
.map(|p| p.mutability(repacker))
173-
.collect::<Vec<_>>();
174-
pcg_validity_assert!(
175-
mut_values.windows(2).all(|w| w[0] == w[1]),
176-
"All mutability values must be the same"
177-
);
178-
mut_values[0]
179-
}
180166

181167
pub(crate) fn new(
182168
inputs: BlockEdgeInputs<'tcx>,

src/borrow_pcg/region_projection.rs

-23
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use crate::validity_checks_enabled;
1818
use crate::{
1919
combined_pcs::{LocalNodeLike, PCGNode, PCGNodeLike},
2020
rustc_interface::{
21-
ast::Mutability,
2221
data_structures::fx::FxHashSet,
2322
index::{Idx, IndexVec},
2423
middle::{
@@ -477,28 +476,6 @@ impl<'tcx> RegionProjection<'tcx> {
477476
self.base.as_local_place().map(|p| p.local())
478477
}
479478

480-
/// Returns `true` iff the place is a mutable reference, or if the place is
481-
/// a mutable struct. If the place is a remote place, it is mutable iff the
482-
/// corresponding input argument is a mutable reference.
483-
pub(crate) fn mutability(&self, repacker: PlaceRepacker<'_, 'tcx>) -> Mutability {
484-
let place = match self.base {
485-
MaybeRemoteRegionProjectionBase::Place(p) => p.related_local_place(),
486-
MaybeRemoteRegionProjectionBase::Const(_) => {
487-
return Mutability::Not;
488-
}
489-
};
490-
place.ref_mutability(repacker).unwrap_or_else(|| {
491-
if let Ok(root_place) =
492-
place.is_mutable(crate::utils::LocalMutationIsAllowed::Yes, repacker)
493-
&& root_place.is_local_mutation_allowed == crate::utils::LocalMutationIsAllowed::Yes
494-
{
495-
Mutability::Mut
496-
} else {
497-
Mutability::Not
498-
}
499-
})
500-
}
501-
502479
fn as_local_region_projection(
503480
&self,
504481
repacker: PlaceRepacker<'_, 'tcx>,

src/borrow_pcg/state/mod.rs

+13-27
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ use super::{
55
BlockedNode, BorrowPCGEdge, BorrowPCGEdgeLike, BorrowPCGEdgeRef, LocalNode, ToBorrowsEdge,
66
},
77
coupling_graph_constructor::BorrowCheckerInterface,
8-
edge::kind::BorrowPCGEdgeKind,
98
graph::{BorrowsGraph, FrozenGraphRef},
109
latest::Latest,
1110
path_condition::{PathCondition, PathConditions},
1211
};
13-
use crate::borrow_pcg::edge::borrow::BorrowEdge;
1412
use crate::utils::place::maybe_old::MaybeOldPlace;
1513
use crate::utils::place::maybe_remote::MaybeRemotePlace;
1614
use crate::{borrow_pcg::action::executed_actions::ExecutedActions, combined_pcs::PCGError};
15+
use crate::borrow_pcg::edge::borrow::BorrowEdge;
1716
use crate::{
1817
borrow_pcg::edge_data::EdgeData,
1918
combined_pcs::{PCGNode, PCGNodeLike},
@@ -236,21 +235,6 @@ impl<'tcx> BorrowsState<'tcx> {
236235
changed
237236
}
238237

239-
fn min_blocked_by_capability(
240-
&self,
241-
edge: &BorrowPCGEdgeKind<'tcx>,
242-
repacker: PlaceRepacker<'_, 'tcx>,
243-
) -> Option<CapabilityKind> {
244-
let mut iter = edge.blocked_by_nodes(repacker).into_iter();
245-
let first_node = iter.next()?;
246-
let mut cap = self.get_capability(first_node.into())?;
247-
for node in iter {
248-
let other_cap = self.get_capability(node.into())?;
249-
cap = cap.minimum(other_cap)?;
250-
}
251-
Some(cap)
252-
}
253-
254238
pub(super) fn remove_edge_and_set_latest(
255239
&mut self,
256240
edge: impl BorrowPCGEdgeLike<'tcx>,
@@ -286,16 +270,18 @@ impl<'tcx> BorrowsState<'tcx> {
286270
if let Some(local_node) = node.as_local_node(repacker) {
287271
let blocked_cap = self.get_capability(node);
288272

289-
let restore_cap = self.min_blocked_by_capability(edge.kind(), repacker);
273+
let restore_cap = if local_node.place().projects_shared_ref(repacker) {
274+
CapabilityKind::Read
275+
} else {
276+
CapabilityKind::Exclusive
277+
};
290278

291-
if let Some(restore_cap) = restore_cap {
292-
if blocked_cap.is_none_or(|bc| bc < restore_cap) {
293-
self.record_and_apply_action(
294-
BorrowPCGAction::restore_capability(local_node, restore_cap),
295-
&mut actions,
296-
repacker,
297-
)?;
298-
}
279+
if blocked_cap.is_none_or(|bc| bc < restore_cap) {
280+
self.record_and_apply_action(
281+
BorrowPCGAction::restore_capability(local_node, restore_cap),
282+
&mut actions,
283+
repacker,
284+
)?;
299285
}
300286
}
301287
}
@@ -493,7 +479,7 @@ impl<'tcx> BorrowsState<'tcx> {
493479
BorrowKind::Mut {
494480
kind: MutBorrowKind::Default,
495481
} => {
496-
self.remove_capability(blocked_place.into());
482+
let _ = self.remove_capability(blocked_place.into());
497483
}
498484
_ => {
499485
match self.get_capability(blocked_place.into()) {

src/borrow_pcg/state/obtain.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ impl<'tcx> BorrowsState<'tcx> {
7979
}
8080

8181
if !self.contains(place, repacker) {
82-
let extra_acts = self.expand_to(place, repacker, location, obtain_reason)?;
82+
let extra_acts = self.expand_to(place, repacker, location)?;
8383
actions.extend(extra_acts);
84-
} else {
85-
eprintln!("Already contains {:?}", place);
8684
}
8785

8886
Ok(actions)
@@ -189,7 +187,6 @@ impl<'tcx> BorrowsState<'tcx> {
189187
to_place: Place<'tcx>,
190188
repacker: PlaceRepacker<'_, 'tcx>,
191189
location: Location,
192-
obtain_reason: ObtainReason,
193190
) -> Result<ExecutedActions<'tcx>, PCGError> {
194191
let mut actions = ExecutedActions::new();
195192

@@ -236,8 +233,7 @@ impl<'tcx> BorrowsState<'tcx> {
236233
BorrowPCGEdge::new(
237234
BorrowPCGEdgeKind::BorrowPCGExpansion(expansion),
238235
PathConditions::new(location.block),
239-
)
240-
.into(),
236+
),
241237
true,
242238
);
243239
self.record_and_apply_action(action, &mut actions, repacker)?;
@@ -264,8 +260,7 @@ impl<'tcx> BorrowsState<'tcx> {
264260
BorrowPCGEdge::new(
265261
BorrowPCGEdgeKind::BorrowPCGExpansion(expansion),
266262
PathConditions::new(location.block),
267-
)
268-
.into(),
263+
),
269264
true,
270265
),
271266
&mut actions,

src/borrow_pcg/visitor/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
};
2323

2424
use super::{
25-
action::{BorrowPCGAction, BorrowPCGActionKind},
25+
action::BorrowPCGAction,
2626
borrow_pcg_edge::BorrowPCGEdge,
2727
coupling_graph_constructor::BorrowCheckerInterface,
2828
edge::block::BlockEdge,
@@ -166,8 +166,7 @@ impl<'tcx, 'mir, 'state> BorrowsVisitor<'tcx, 'mir, 'state> {
166166
)
167167
.into(),
168168
PathConditions::AtBlock(location.block),
169-
)
170-
.into(),
169+
),
171170
true,
172171
));
173172
}
@@ -262,7 +261,6 @@ impl<'tcx, 'mir, 'state> BorrowsVisitor<'tcx, 'mir, 'state> {
262261
BorrowPCGEdge::new(
263262
AbstractionType::FunctionCall(
264263
FunctionCallAbstraction::new(location, *func_def_id, substs, edges.clone())
265-
.into(),
266264
)
267265
.into(),
268266
PathConditions::AtBlock(location.block),

src/utils/place/maybe_old.rs

-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::borrow_pcg::region_projection::{
77
};
88
use crate::borrow_pcg::visitor::extract_regions;
99
use crate::combined_pcs::{LocalNodeLike, PCGError, PCGNode, PCGNodeLike};
10-
use crate::rustc_interface::ast::Mutability;
1110
use crate::rustc_interface::index::IndexVec;
1211
use crate::rustc_interface::middle::mir;
1312
use crate::rustc_interface::middle::mir::tcx::PlaceTy;
@@ -224,19 +223,6 @@ impl<'tcx> MaybeOldPlace<'tcx> {
224223
.map(|rp| rp.set_base(rp.base.into(), repacker))
225224
}
226225

227-
pub(crate) fn mutability(&self, repacker: PlaceRepacker<'_, 'tcx>) -> Mutability {
228-
let place: Place<'_> = self.place();
229-
place.ref_mutability(repacker).unwrap_or_else(|| {
230-
if let Ok(root_place) =
231-
place.is_mutable(crate::utils::LocalMutationIsAllowed::Yes, repacker)
232-
&& root_place.is_local_mutation_allowed == crate::utils::LocalMutationIsAllowed::Yes
233-
{
234-
Mutability::Mut
235-
} else {
236-
Mutability::Not
237-
}
238-
})
239-
}
240226
pub(crate) fn is_owned(&self, repacker: PlaceRepacker<'_, 'tcx>) -> bool {
241227
self.place().is_owned(repacker)
242228
}

src/utils/place/maybe_remote.rs

-21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::borrow_pcg::region_projection::{
33
MaybeRemoteRegionProjectionBase, PCGRegion, RegionIdx, RegionProjectionBaseLike,
44
};
55
use crate::combined_pcs::{PCGNode, PCGNodeLike};
6-
use crate::rustc_interface::ast::Mutability;
76
use crate::rustc_interface::index::IndexVec;
87
use crate::rustc_interface::middle::{mir, ty};
98
use crate::utils::display::DisplayWithRepacker;
@@ -86,12 +85,6 @@ impl std::fmt::Display for MaybeRemotePlace<'_> {
8685
}
8786

8887
impl<'tcx> MaybeRemotePlace<'tcx> {
89-
pub(crate) fn mutability(&self, repacker: PlaceRepacker<'_, 'tcx>) -> Mutability {
90-
match self {
91-
MaybeRemotePlace::Local(p) => p.mutability(repacker),
92-
MaybeRemotePlace::Remote(rp) => rp.mutability(repacker),
93-
}
94-
}
9588

9689
pub fn place_assigned_to_local(local: mir::Local) -> Self {
9790
MaybeRemotePlace::Remote(RemotePlace { local })
@@ -184,18 +177,4 @@ impl RemotePlace {
184177
_ => todo!(),
185178
}
186179
}
187-
188-
pub(crate) fn mutability(&self, repacker: PlaceRepacker<'_, '_>) -> Mutability {
189-
let place: Place<'_> = self.local.into();
190-
place.ref_mutability(repacker).unwrap_or_else(|| {
191-
if let Ok(root_place) =
192-
place.is_mutable(crate::utils::LocalMutationIsAllowed::Yes, repacker)
193-
&& root_place.is_local_mutation_allowed == crate::utils::LocalMutationIsAllowed::Yes
194-
{
195-
Mutability::Mut
196-
} else {
197-
Mutability::Not
198-
}
199-
})
200-
}
201180
}

0 commit comments

Comments
 (0)