1
1
use super :: {
2
2
for_each_consumable, record_consumed_borrow:: ConsumedAndBorrowedPlaces , DropRangesBuilder ,
3
- HirIdIndex , NodeInfo , PostOrderId ,
3
+ NodeInfo , PostOrderId , TrackedValue , TrackedValueIndex ,
4
4
} ;
5
5
use hir:: {
6
6
intravisit:: { self , NestedVisitorMap , Visitor } ,
7
- Body , Expr , ExprKind , Guard , HirId , HirIdMap ,
7
+ Body , Expr , ExprKind , Guard , HirId ,
8
8
} ;
9
+ use rustc_data_structures:: fx:: FxHashMap ;
9
10
use rustc_hir as hir;
10
11
use rustc_index:: vec:: IndexVec ;
11
12
use rustc_middle:: {
@@ -61,20 +62,20 @@ impl<'a, 'tcx> DropRangeVisitor<'a, 'tcx> {
61
62
) -> Self {
62
63
debug ! ( "consumed_places: {:?}" , places. consumed) ;
63
64
let drop_ranges = DropRangesBuilder :: new (
64
- places. consumed . iter ( ) . flat_map ( |( _, places) | places. iter ( ) . copied ( ) ) ,
65
+ places. consumed . iter ( ) . flat_map ( |( _, places) | places. iter ( ) . cloned ( ) ) ,
65
66
hir,
66
67
num_exprs,
67
68
) ;
68
69
Self { hir, places, drop_ranges, expr_index : PostOrderId :: from_u32 ( 0 ) , typeck_results, tcx }
69
70
}
70
71
71
- fn record_drop ( & mut self , hir_id : HirId ) {
72
- if self . places . borrowed . contains ( & hir_id ) {
73
- debug ! ( "not marking {:?} as dropped because it is borrowed at some point" , hir_id ) ;
72
+ fn record_drop ( & mut self , value : TrackedValue ) {
73
+ if self . places . borrowed . contains ( & value ) {
74
+ debug ! ( "not marking {:?} as dropped because it is borrowed at some point" , value ) ;
74
75
} else {
75
- debug ! ( "marking {:?} as dropped at {:?}" , hir_id , self . expr_index) ;
76
+ debug ! ( "marking {:?} as dropped at {:?}" , value , self . expr_index) ;
76
77
let count = self . expr_index ;
77
- self . drop_ranges . drop_at ( hir_id , count) ;
78
+ self . drop_ranges . drop_at ( value , count) ;
78
79
}
79
80
}
80
81
@@ -88,7 +89,9 @@ impl<'a, 'tcx> DropRangeVisitor<'a, 'tcx> {
88
89
. get ( & expr. hir_id )
89
90
. map_or ( vec ! [ ] , |places| places. iter ( ) . cloned ( ) . collect ( ) ) ;
90
91
for place in places {
91
- for_each_consumable ( place, self . hir . find ( place) , |hir_id| self . record_drop ( hir_id) ) ;
92
+ for_each_consumable ( place, self . hir . find ( place. hir_id ( ) ) , |value| {
93
+ self . record_drop ( value)
94
+ } ) ;
92
95
}
93
96
}
94
97
@@ -100,7 +103,7 @@ impl<'a, 'tcx> DropRangeVisitor<'a, 'tcx> {
100
103
{
101
104
let location = self . expr_index ;
102
105
debug ! ( "reinitializing {:?} at {:?}" , hir_id, location) ;
103
- self . drop_ranges . reinit_at ( * hir_id, location) ;
106
+ self . drop_ranges . reinit_at ( TrackedValue :: Variable ( * hir_id) , location) ;
104
107
} else {
105
108
debug ! ( "reinitializing {:?} is not supported" , expr) ;
106
109
}
@@ -264,36 +267,40 @@ impl<'a, 'tcx> Visitor<'tcx> for DropRangeVisitor<'a, 'tcx> {
264
267
}
265
268
266
269
impl DropRangesBuilder {
267
- fn new ( hir_ids : impl Iterator < Item = HirId > , hir : Map < ' _ > , num_exprs : usize ) -> Self {
268
- let mut hir_id_map = HirIdMap :: < HirIdIndex > :: default ( ) ;
270
+ fn new (
271
+ tracked_values : impl Iterator < Item = TrackedValue > ,
272
+ hir : Map < ' _ > ,
273
+ num_exprs : usize ,
274
+ ) -> Self {
275
+ let mut tracked_value_map = FxHashMap :: < _ , TrackedValueIndex > :: default ( ) ;
269
276
let mut next = <_ >:: from ( 0u32 ) ;
270
- for hir_id in hir_ids {
271
- for_each_consumable ( hir_id , hir. find ( hir_id) , |hir_id | {
272
- if !hir_id_map . contains_key ( & hir_id ) {
273
- hir_id_map . insert ( hir_id , next) ;
274
- next = < _ > :: from ( next. index ( ) + 1 ) ;
277
+ for value in tracked_values {
278
+ for_each_consumable ( value , hir. find ( value . hir_id ( ) ) , |value | {
279
+ if !tracked_value_map . contains_key ( & value ) {
280
+ tracked_value_map . insert ( value , next) ;
281
+ next = next + 1 ;
275
282
}
276
283
} ) ;
277
284
}
278
- debug ! ( "hir_id_map: {:?}" , hir_id_map ) ;
279
- let num_values = hir_id_map . len ( ) ;
285
+ debug ! ( "hir_id_map: {:?}" , tracked_value_map ) ;
286
+ let num_values = tracked_value_map . len ( ) ;
280
287
Self {
281
- hir_id_map ,
288
+ tracked_value_map ,
282
289
nodes : IndexVec :: from_fn_n ( |_| NodeInfo :: new ( num_values) , num_exprs + 1 ) ,
283
290
deferred_edges : <_ >:: default ( ) ,
284
291
post_order_map : <_ >:: default ( ) ,
285
292
}
286
293
}
287
294
288
- fn hidx ( & self , hir_id : HirId ) -> HirIdIndex {
289
- * self . hir_id_map . get ( & hir_id ) . unwrap ( )
295
+ fn tracked_value_index ( & self , tracked_value : TrackedValue ) -> TrackedValueIndex {
296
+ * self . tracked_value_map . get ( & tracked_value ) . unwrap ( )
290
297
}
291
298
292
299
/// Adds an entry in the mapping from HirIds to PostOrderIds
293
300
///
294
301
/// Needed so that `add_control_edge_hir_id` can work.
295
- fn add_node_mapping ( & mut self , hir_id : HirId , post_order_id : PostOrderId ) {
296
- self . post_order_map . insert ( hir_id , post_order_id) ;
302
+ fn add_node_mapping ( & mut self , node_hir_id : HirId , post_order_id : PostOrderId ) {
303
+ self . post_order_map . insert ( node_hir_id , post_order_id) ;
297
304
}
298
305
299
306
/// Like add_control_edge, but uses a hir_id as the target.
@@ -304,13 +311,13 @@ impl DropRangesBuilder {
304
311
self . deferred_edges . push ( ( from, to) ) ;
305
312
}
306
313
307
- fn drop_at ( & mut self , value : HirId , location : PostOrderId ) {
308
- let value = self . hidx ( value) ;
314
+ fn drop_at ( & mut self , value : TrackedValue , location : PostOrderId ) {
315
+ let value = self . tracked_value_index ( value) ;
309
316
self . node_mut ( location. into ( ) ) . drops . push ( value) ;
310
317
}
311
318
312
- fn reinit_at ( & mut self , value : HirId , location : PostOrderId ) {
313
- let value = match self . hir_id_map . get ( & value) {
319
+ fn reinit_at ( & mut self , value : TrackedValue , location : PostOrderId ) {
320
+ let value = match self . tracked_value_map . get ( & value) {
314
321
Some ( value) => * value,
315
322
// If there's no value, this is never consumed and therefore is never dropped. We can
316
323
// ignore this.
0 commit comments