@@ -3,9 +3,8 @@ use crate::borrow_check::path_utils::allow_two_phase_borrow;
33use crate :: borrow_check:: place_ext:: PlaceExt ;
44use crate :: dataflow:: indexes:: BorrowIndex ;
55use crate :: dataflow:: move_paths:: MoveData ;
6- use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
6+ use rustc_data_structures:: fx:: { FxHashMap , FxHashSet , FxIndexMap } ;
77use rustc_index:: bit_set:: BitSet ;
8- use rustc_index:: vec:: IndexVec ;
98use rustc_middle:: mir:: traversal;
109use rustc_middle:: mir:: visit:: { MutatingUseContext , NonUseContext , PlaceContext , Visitor } ;
1110use rustc_middle:: mir:: { self , Body , Local , Location } ;
@@ -15,14 +14,11 @@ use std::ops::Index;
1514
1615crate 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
162176struct 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 }
0 commit comments