Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 42e7a0c

Browse files
committed
rustc_mir: use IndexMap in BorrowSet
1 parent 952daa2 commit 42e7a0c

File tree

7 files changed

+52
-43
lines changed

7 files changed

+52
-43
lines changed

src/librustc_mir/borrow_check/borrow_set.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ use crate::borrow_check::path_utils::allow_two_phase_borrow;
33
use crate::borrow_check::place_ext::PlaceExt;
44
use crate::dataflow::indexes::BorrowIndex;
55
use crate::dataflow::move_paths::MoveData;
6-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
6+
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
77
use rustc_index::bit_set::BitSet;
8-
use rustc_index::vec::IndexVec;
98
use rustc_middle::mir::traversal;
109
use rustc_middle::mir::visit::{MutatingUseContext, NonUseContext, PlaceContext, Visitor};
1110
use rustc_middle::mir::{self, Body, Local, Location};
@@ -15,14 +14,11 @@ use std::ops::Index;
1514

1615
crate struct BorrowSet<'tcx> {
1716
/// The fundamental map relating bitvector indexes to the borrows
18-
/// in the MIR.
19-
crate borrows: IndexVec<BorrowIndex, BorrowData<'tcx>>,
20-
21-
/// Each borrow is also uniquely identified in the MIR by the
22-
/// `Location` of the assignment statement in which it appears on
23-
/// the right hand side; we map each such location to the
24-
/// corresponding `BorrowIndex`.
25-
crate location_map: FxHashMap<Location, BorrowIndex>,
17+
/// in the MIR. Each borrow is also uniquely identified in the MIR
18+
/// by the `Location` of the assignment statement in which it
19+
/// appears on the right hand side. Thus the location is the map
20+
/// key, and its position in the map corresponds to `BorrowIndex`.
21+
crate location_map: FxIndexMap<Location, BorrowData<'tcx>>,
2622

2723
/// Locations which activate borrows.
2824
/// NOTE: a given location may activate more than one borrow in the future
@@ -40,7 +36,7 @@ impl<'tcx> Index<BorrowIndex> for BorrowSet<'tcx> {
4036
type Output = BorrowData<'tcx>;
4137

4238
fn index(&self, index: BorrowIndex) -> &BorrowData<'tcx> {
43-
&self.borrows[index]
39+
&self.location_map[index.as_usize()]
4440
}
4541
}
4642

@@ -129,7 +125,6 @@ impl<'tcx> BorrowSet<'tcx> {
129125
let mut visitor = GatherBorrows {
130126
tcx,
131127
body: &body,
132-
idx_vec: IndexVec::new(),
133128
location_map: Default::default(),
134129
activation_map: Default::default(),
135130
local_map: Default::default(),
@@ -146,7 +141,6 @@ impl<'tcx> BorrowSet<'tcx> {
146141
}
147142

148143
BorrowSet {
149-
borrows: visitor.idx_vec,
150144
location_map: visitor.location_map,
151145
activation_map: visitor.activation_map,
152146
local_map: visitor.local_map,
@@ -157,13 +151,32 @@ impl<'tcx> BorrowSet<'tcx> {
157151
crate fn activations_at_location(&self, location: Location) -> &[BorrowIndex] {
158152
self.activation_map.get(&location).map(|activations| &activations[..]).unwrap_or(&[])
159153
}
154+
155+
crate fn len(&self) -> usize {
156+
self.location_map.len()
157+
}
158+
159+
crate fn indices(&self) -> impl Iterator<Item = BorrowIndex> {
160+
BorrowIndex::from_usize(0)..BorrowIndex::from_usize(self.len())
161+
}
162+
163+
crate fn iter_enumerated(&self) -> impl Iterator<Item = (BorrowIndex, &BorrowData<'tcx>)> {
164+
self.indices().zip(self.location_map.values())
165+
}
166+
167+
crate fn get_index_of(&self, location: &Location) -> Option<BorrowIndex> {
168+
self.location_map.get_index_of(location).map(BorrowIndex::from)
169+
}
170+
171+
crate fn contains(&self, location: &Location) -> bool {
172+
self.location_map.contains_key(location)
173+
}
160174
}
161175

162176
struct GatherBorrows<'a, 'tcx> {
163177
tcx: TyCtxt<'tcx>,
164178
body: &'a Body<'tcx>,
165-
idx_vec: IndexVec<BorrowIndex, BorrowData<'tcx>>,
166-
location_map: FxHashMap<Location, BorrowIndex>,
179+
location_map: FxIndexMap<Location, BorrowData<'tcx>>,
167180
activation_map: FxHashMap<Location, Vec<BorrowIndex>>,
168181
local_map: FxHashMap<mir::Local, FxHashSet<BorrowIndex>>,
169182

@@ -203,8 +216,8 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'tcx> {
203216
borrowed_place: *borrowed_place,
204217
assigned_place: *assigned_place,
205218
};
206-
let idx = self.idx_vec.push(borrow);
207-
self.location_map.insert(location, idx);
219+
let (idx, _) = self.location_map.insert_full(location, borrow);
220+
let idx = BorrowIndex::from(idx);
208221

209222
self.insert_as_pending_if_two_phase(location, assigned_place, kind, idx);
210223

@@ -224,7 +237,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'tcx> {
224237
//
225238
// TMP = &mut place
226239
if let Some(&borrow_index) = self.pending_activations.get(temp) {
227-
let borrow_data = &mut self.idx_vec[borrow_index];
240+
let borrow_data = &mut self.location_map[borrow_index.as_usize()];
228241

229242
// Watch out: the use of TMP in the borrow itself
230243
// doesn't count as an activation. =)
@@ -265,8 +278,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'tcx> {
265278
if let mir::Rvalue::Ref(region, kind, ref place) = *rvalue {
266279
// double-check that we already registered a BorrowData for this
267280

268-
let borrow_index = self.location_map[&location];
269-
let borrow_data = &self.idx_vec[borrow_index];
281+
let borrow_data = &self.location_map[&location];
270282
assert_eq!(borrow_data.reserve_location, location);
271283
assert_eq!(borrow_data.kind, kind);
272284
assert_eq!(borrow_data.region, region.to_region_vid());
@@ -316,7 +328,7 @@ impl<'a, 'tcx> GatherBorrows<'a, 'tcx> {
316328
// Consider the borrow not activated to start. When we find an activation, we'll update
317329
// this field.
318330
{
319-
let borrow_data = &mut self.idx_vec[borrow_index];
331+
let borrow_data = &mut self.location_map[borrow_index.as_usize()];
320332
borrow_data.activation_location = TwoPhaseActivation::NotActivated;
321333
}
322334

@@ -332,7 +344,7 @@ impl<'a, 'tcx> GatherBorrows<'a, 'tcx> {
332344
at borrow_index: {:?} with associated data {:?}",
333345
temp,
334346
old_index,
335-
self.idx_vec[old_index]
347+
self.location_map[old_index.as_usize()]
336348
);
337349
}
338350
}

src/librustc_mir/borrow_check/constraint_generation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<'cx, 'cg, 'tcx> ConstraintGeneration<'cx, 'cg, 'tcx> {
217217
let places_conflict = places_conflict::places_conflict(
218218
self.infcx.tcx,
219219
self.body,
220-
self.borrow_set.borrows[borrow_index].borrowed_place,
220+
self.borrow_set[borrow_index].borrowed_place,
221221
place,
222222
places_conflict::PlaceConflictBias::NoOverlap,
223223
);

src/librustc_mir/borrow_check/invalidation.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
166166
// Invalidate all borrows of local places
167167
let borrow_set = self.borrow_set.clone();
168168
let resume = self.location_table.start_index(resume.start_location());
169-
for i in borrow_set.borrows.indices() {
170-
if borrow_of_local_data(borrow_set.borrows[i].borrowed_place) {
169+
for (i, data) in borrow_set.iter_enumerated() {
170+
if borrow_of_local_data(data.borrowed_place) {
171171
self.all_facts.invalidates.push((resume, i));
172172
}
173173
}
@@ -178,8 +178,8 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
178178
// Invalidate all borrows of local places
179179
let borrow_set = self.borrow_set.clone();
180180
let start = self.location_table.start_index(location);
181-
for i in borrow_set.borrows.indices() {
182-
if borrow_of_local_data(borrow_set.borrows[i].borrowed_place) {
181+
for (i, data) in borrow_set.iter_enumerated() {
182+
if borrow_of_local_data(data.borrowed_place) {
183183
self.all_facts.invalidates.push((start, i));
184184
}
185185
}
@@ -369,7 +369,7 @@ impl<'cx, 'tcx> InvalidationGenerator<'cx, 'tcx> {
369369
let tcx = self.tcx;
370370
let body = self.body;
371371
let borrow_set = self.borrow_set.clone();
372-
let indices = self.borrow_set.borrows.indices();
372+
let indices = self.borrow_set.indices();
373373
each_borrow_involving_path(
374374
self,
375375
tcx,

src/librustc_mir/borrow_check/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,11 +1131,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11311131
(
11321132
Reservation(WriteKind::MutableBorrow(bk)),
11331133
BorrowKind::Shallow | BorrowKind::Shared,
1134-
) if {
1135-
tcx.migrate_borrowck() && this.borrow_set.location_map.contains_key(&location)
1136-
} =>
1137-
{
1138-
let bi = this.borrow_set.location_map[&location];
1134+
) if { tcx.migrate_borrowck() && this.borrow_set.contains(&location) } => {
1135+
let bi = this.borrow_set.get_index_of(&location).unwrap();
11391136
debug!(
11401137
"recording invalid reservation of place: {:?} with \
11411138
borrow index {:?} as warning",

src/librustc_mir/borrow_check/nll.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
206206
// the `borrow_set`, their `BorrowIndex` are synthesized as the universal region index
207207
// added to the existing number of loans, as if they succeeded them in the set.
208208
//
209-
let borrow_count = borrow_set.borrows.len();
209+
let borrow_count = borrow_set.len();
210210
debug!(
211211
"compute_regions: polonius placeholders, num_universals={}, borrow_count={}",
212212
universal_regions.len(),

src/librustc_mir/borrow_check/type_check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,11 +2469,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24692469
// example).
24702470
if let Some(all_facts) = all_facts {
24712471
let _prof_timer = self.infcx.tcx.prof.generic_activity("polonius_fact_generation");
2472-
if let Some(borrow_index) = borrow_set.location_map.get(&location) {
2472+
if let Some(borrow_index) = borrow_set.get_index_of(&location) {
24732473
let region_vid = borrow_region.to_region_vid();
24742474
all_facts.borrow_region.push((
24752475
region_vid,
2476-
*borrow_index,
2476+
borrow_index,
24772477
location_table.mid_index(location),
24782478
));
24792479
}

src/librustc_mir/dataflow/impls/borrows.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
136136
borrow_set: &Rc<BorrowSet<'tcx>>,
137137
) -> Self {
138138
let mut borrows_out_of_scope_at_location = FxHashMap::default();
139-
for (borrow_index, borrow_data) in borrow_set.borrows.iter_enumerated() {
139+
for (borrow_index, borrow_data) in borrow_set.iter_enumerated() {
140140
let borrow_region = borrow_data.region.to_region_vid();
141-
let location = borrow_set.borrows[borrow_index].reserve_location;
141+
let location = borrow_data.reserve_location;
142142

143143
precompute_borrows_out_of_scope(
144144
body,
@@ -160,7 +160,7 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
160160
}
161161

162162
pub fn location(&self, idx: BorrowIndex) -> &Location {
163-
&self.borrow_set.borrows[idx].reserve_location
163+
&self.borrow_set[idx].reserve_location
164164
}
165165

166166
/// Add all borrows to the kill set, if those borrows are out of scope at `location`.
@@ -216,7 +216,7 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
216216
places_conflict(
217217
self.tcx,
218218
self.body,
219-
self.borrow_set.borrows[i].borrowed_place,
219+
self.borrow_set[i].borrowed_place,
220220
place,
221221
PlaceConflictBias::NoOverlap,
222222
)
@@ -232,7 +232,7 @@ impl<'tcx> dataflow::AnalysisDomain<'tcx> for Borrows<'_, 'tcx> {
232232
const NAME: &'static str = "borrows";
233233

234234
fn bits_per_block(&self, _: &mir::Body<'tcx>) -> usize {
235-
self.borrow_set.borrows.len() * 2
235+
self.borrow_set.len() * 2
236236
}
237237

238238
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut BitSet<Self::Idx>) {
@@ -271,11 +271,11 @@ impl<'tcx> dataflow::GenKillAnalysis<'tcx> for Borrows<'_, 'tcx> {
271271
) {
272272
return;
273273
}
274-
let index = self.borrow_set.location_map.get(&location).unwrap_or_else(|| {
274+
let index = self.borrow_set.get_index_of(&location).unwrap_or_else(|| {
275275
panic!("could not find BorrowIndex for location {:?}", location);
276276
});
277277

278-
trans.gen(*index);
278+
trans.gen(index);
279279
}
280280

281281
// Make sure there are no remaining borrows for variables

0 commit comments

Comments
 (0)