@@ -3,9 +3,8 @@ use crate::borrow_check::path_utils::allow_two_phase_borrow;
3
3
use crate :: borrow_check:: place_ext:: PlaceExt ;
4
4
use crate :: dataflow:: indexes:: BorrowIndex ;
5
5
use crate :: dataflow:: move_paths:: MoveData ;
6
- use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
6
+ use rustc_data_structures:: fx:: { FxHashMap , FxHashSet , FxIndexMap } ;
7
7
use rustc_index:: bit_set:: BitSet ;
8
- use rustc_index:: vec:: IndexVec ;
9
8
use rustc_middle:: mir:: traversal;
10
9
use rustc_middle:: mir:: visit:: { MutatingUseContext , NonUseContext , PlaceContext , Visitor } ;
11
10
use rustc_middle:: mir:: { self , Body , Local , Location } ;
@@ -15,14 +14,11 @@ use std::ops::Index;
15
14
16
15
crate struct BorrowSet < ' tcx > {
17
16
/// 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 > > ,
26
22
27
23
/// Locations which activate borrows.
28
24
/// NOTE: a given location may activate more than one borrow in the future
@@ -40,7 +36,7 @@ impl<'tcx> Index<BorrowIndex> for BorrowSet<'tcx> {
40
36
type Output = BorrowData < ' tcx > ;
41
37
42
38
fn index ( & self , index : BorrowIndex ) -> & BorrowData < ' tcx > {
43
- & self . borrows [ index]
39
+ & self . location_map [ index. as_usize ( ) ]
44
40
}
45
41
}
46
42
@@ -129,7 +125,6 @@ impl<'tcx> BorrowSet<'tcx> {
129
125
let mut visitor = GatherBorrows {
130
126
tcx,
131
127
body : & body,
132
- idx_vec : IndexVec :: new ( ) ,
133
128
location_map : Default :: default ( ) ,
134
129
activation_map : Default :: default ( ) ,
135
130
local_map : Default :: default ( ) ,
@@ -146,7 +141,6 @@ impl<'tcx> BorrowSet<'tcx> {
146
141
}
147
142
148
143
BorrowSet {
149
- borrows : visitor. idx_vec ,
150
144
location_map : visitor. location_map ,
151
145
activation_map : visitor. activation_map ,
152
146
local_map : visitor. local_map ,
@@ -157,13 +151,32 @@ impl<'tcx> BorrowSet<'tcx> {
157
151
crate fn activations_at_location ( & self , location : Location ) -> & [ BorrowIndex ] {
158
152
self . activation_map . get ( & location) . map ( |activations| & activations[ ..] ) . unwrap_or ( & [ ] )
159
153
}
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
+ }
160
174
}
161
175
162
176
struct GatherBorrows < ' a , ' tcx > {
163
177
tcx : TyCtxt < ' tcx > ,
164
178
body : & ' a Body < ' tcx > ,
165
- idx_vec : IndexVec < BorrowIndex , BorrowData < ' tcx > > ,
166
- location_map : FxHashMap < Location , BorrowIndex > ,
179
+ location_map : FxIndexMap < Location , BorrowData < ' tcx > > ,
167
180
activation_map : FxHashMap < Location , Vec < BorrowIndex > > ,
168
181
local_map : FxHashMap < mir:: Local , FxHashSet < BorrowIndex > > ,
169
182
@@ -203,8 +216,8 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'tcx> {
203
216
borrowed_place : * borrowed_place,
204
217
assigned_place : * assigned_place,
205
218
} ;
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) ;
208
221
209
222
self . insert_as_pending_if_two_phase ( location, assigned_place, kind, idx) ;
210
223
@@ -224,7 +237,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'tcx> {
224
237
//
225
238
// TMP = &mut place
226
239
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 ( ) ] ;
228
241
229
242
// Watch out: the use of TMP in the borrow itself
230
243
// doesn't count as an activation. =)
@@ -265,8 +278,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'tcx> {
265
278
if let mir:: Rvalue :: Ref ( region, kind, ref place) = * rvalue {
266
279
// double-check that we already registered a BorrowData for this
267
280
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] ;
270
282
assert_eq ! ( borrow_data. reserve_location, location) ;
271
283
assert_eq ! ( borrow_data. kind, kind) ;
272
284
assert_eq ! ( borrow_data. region, region. to_region_vid( ) ) ;
@@ -316,7 +328,7 @@ impl<'a, 'tcx> GatherBorrows<'a, 'tcx> {
316
328
// Consider the borrow not activated to start. When we find an activation, we'll update
317
329
// this field.
318
330
{
319
- let borrow_data = & mut self . idx_vec [ borrow_index] ;
331
+ let borrow_data = & mut self . location_map [ borrow_index. as_usize ( ) ] ;
320
332
borrow_data. activation_location = TwoPhaseActivation :: NotActivated ;
321
333
}
322
334
@@ -332,7 +344,7 @@ impl<'a, 'tcx> GatherBorrows<'a, 'tcx> {
332
344
at borrow_index: {:?} with associated data {:?}",
333
345
temp,
334
346
old_index,
335
- self . idx_vec [ old_index]
347
+ self . location_map [ old_index. as_usize ( ) ]
336
348
) ;
337
349
}
338
350
}
0 commit comments