@@ -7,7 +7,7 @@ use rustc_data_structures::fx::FxHashMap;
7
7
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
8
8
use rustc_hir as hir;
9
9
use rustc_hir:: def_id:: LocalDefId ;
10
- use rustc_hir:: def_id:: CRATE_DEF_INDEX ;
10
+ use rustc_hir:: def_id:: CRATE_DEF_ID ;
11
11
use rustc_hir:: definitions;
12
12
use rustc_hir:: intravisit:: { self , NestedVisitorMap , Visitor } ;
13
13
use rustc_hir:: * ;
@@ -75,26 +75,20 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
75
75
arena : & ' hir Arena < ' hir > ,
76
76
krate : & ' hir Crate < ' hir > ,
77
77
definitions : & ' a definitions:: Definitions ,
78
- mut hcx : StableHashingContext < ' a > ,
78
+ hcx : StableHashingContext < ' a > ,
79
79
) -> NodeCollector < ' a , ' hir > {
80
- let hash = hash_body ( & mut hcx, krate. module ( ) ) ;
81
-
82
80
let mut collector = NodeCollector {
83
81
arena,
84
82
krate,
85
83
source_map : sess. source_map ( ) ,
86
84
parent_node : hir:: CRATE_HIR_ID ,
87
- current_dep_node_owner : LocalDefId { local_def_index : CRATE_DEF_INDEX } ,
85
+ current_dep_node_owner : CRATE_DEF_ID ,
88
86
definitions,
89
87
hcx,
90
88
map : IndexVec :: from_fn_n ( |_| None , definitions. def_index_count ( ) ) ,
91
89
parenting : FxHashMap :: default ( ) ,
92
90
} ;
93
- collector. insert_entry (
94
- hir:: CRATE_HIR_ID ,
95
- Entry { parent : hir:: CRATE_HIR_ID , node : Node :: Crate ( & krate. module ( ) ) } ,
96
- hash,
97
- ) ;
91
+ collector. insert_owner ( CRATE_DEF_ID , OwnerNode :: Crate ( krate. module ( ) ) ) ;
98
92
99
93
collector
100
94
}
@@ -108,53 +102,20 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
108
102
IndexedHir { map : self . map , parenting : self . parenting }
109
103
}
110
104
111
- fn insert_entry ( & mut self , id : HirId , entry : Entry < ' hir > , hash : Fingerprint ) {
112
- let i = id. local_id . as_u32 ( ) as usize ;
113
-
114
- let arena = self . arena ;
115
-
116
- let data = & mut self . map [ id. owner ] ;
117
-
118
- if i == 0 {
119
- debug_assert ! ( data. is_none( ) ) ;
120
- * data = Some ( arena. alloc ( OwnerNodes {
121
- hash,
122
- nodes : IndexVec :: new ( ) ,
123
- bodies : FxHashMap :: default ( ) ,
124
- } ) ) ;
125
-
126
- let dk_parent = self . definitions . def_key ( id. owner ) . parent ;
127
- if let Some ( dk_parent) = dk_parent {
128
- let dk_parent = LocalDefId { local_def_index : dk_parent } ;
129
- let dk_parent = self . definitions . local_def_id_to_hir_id ( dk_parent) ;
130
- if dk_parent. owner != entry. parent . owner {
131
- panic ! (
132
- "Different parents for {:?} => dk_parent={:?} actual={:?}" ,
133
- id. owner, dk_parent, entry. parent,
134
- )
135
- }
136
-
137
- debug_assert_eq ! ( self . parenting. get( & id. owner) , Some ( & entry. parent) ) ;
138
- }
139
- } else {
140
- debug_assert_eq ! ( entry. parent. owner, id. owner) ;
141
- }
105
+ fn insert_owner ( & mut self , owner : LocalDefId , node : OwnerNode < ' hir > ) {
106
+ let hash = hash_body ( & mut self . hcx , node) ;
142
107
143
- let data = data. as_mut ( ) . unwrap ( ) ;
108
+ let mut nodes = IndexVec :: new ( ) ;
109
+ nodes. push ( Some ( ParentedNode { parent : ItemLocalId :: new ( 0 ) , node : node. into ( ) } ) ) ;
144
110
145
- insert_vec_map (
146
- & mut data. nodes ,
147
- id. local_id ,
148
- ParentedNode { parent : entry. parent . local_id , node : entry. node } ,
149
- ) ;
111
+ debug_assert ! ( self . map[ owner] . is_none( ) ) ;
112
+ self . map [ owner] =
113
+ Some ( self . arena . alloc ( OwnerNodes { hash, nodes, bodies : FxHashMap :: default ( ) } ) ) ;
150
114
}
151
115
152
116
fn insert ( & mut self , span : Span , hir_id : HirId , node : Node < ' hir > ) {
153
- self . insert_with_hash ( span, hir_id, node, Fingerprint :: ZERO )
154
- }
155
-
156
- fn insert_with_hash ( & mut self , span : Span , hir_id : HirId , node : Node < ' hir > , hash : Fingerprint ) {
157
- let entry = Entry { parent : self . parent_node , node } ;
117
+ debug_assert_eq ! ( self . current_dep_node_owner, hir_id. owner) ;
118
+ debug_assert_ne ! ( hir_id. local_id. as_u32( ) , 0 ) ;
158
119
159
120
// Make sure that the DepNode of some node coincides with the HirId
160
121
// owner of that node.
@@ -181,7 +142,14 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
181
142
}
182
143
}
183
144
184
- self . insert_entry ( hir_id, entry, hash) ;
145
+ let nodes = self . map [ hir_id. owner ] . as_mut ( ) . unwrap ( ) ;
146
+
147
+ debug_assert_eq ! ( self . parent_node. owner, self . current_dep_node_owner) ;
148
+ insert_vec_map (
149
+ & mut nodes. nodes ,
150
+ hir_id. local_id ,
151
+ ParentedNode { parent : self . parent_node . local_id , node : node } ,
152
+ ) ;
185
153
}
186
154
187
155
fn with_parent < F : FnOnce ( & mut Self ) > ( & mut self , parent_node_id : HirId , f : F ) {
@@ -191,21 +159,15 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
191
159
self . parent_node = parent_node;
192
160
}
193
161
194
- fn with_dep_node_owner <
195
- T : for < ' b > HashStable < StableHashingContext < ' b > > ,
196
- F : FnOnce ( & mut Self , Fingerprint ) ,
197
- > (
198
- & mut self ,
199
- dep_node_owner : LocalDefId ,
200
- item_like : & T ,
201
- f : F ,
202
- ) {
162
+ fn with_dep_node_owner ( & mut self , dep_node_owner : LocalDefId , f : impl FnOnce ( & mut Self ) ) {
203
163
let prev_owner = self . current_dep_node_owner ;
204
- let hash = hash_body ( & mut self . hcx , item_like ) ;
164
+ let prev_parent = self . parent_node ;
205
165
206
166
self . current_dep_node_owner = dep_node_owner;
207
- f ( self , hash) ;
167
+ self . parent_node = HirId :: make_owner ( dep_node_owner) ;
168
+ f ( self ) ;
208
169
self . current_dep_node_owner = prev_owner;
170
+ self . parent_node = prev_parent;
209
171
}
210
172
211
173
fn insert_nested ( & mut self , item : LocalDefId ) {
@@ -271,28 +233,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
271
233
272
234
fn visit_item ( & mut self , i : & ' hir Item < ' hir > ) {
273
235
debug ! ( "visit_item: {:?}" , i) ;
274
- self . with_dep_node_owner ( i. def_id , i, |this, hash| {
275
- let hir_id = i. hir_id ( ) ;
276
- this. insert_with_hash ( i. span , hir_id, Node :: Item ( i) , hash) ;
277
- this. with_parent ( hir_id, |this| {
278
- if let ItemKind :: Struct ( ref struct_def, _) = i. kind {
279
- // If this is a tuple or unit-like struct, register the constructor.
280
- if let Some ( ctor_hir_id) = struct_def. ctor_hir_id ( ) {
281
- this. insert ( i. span , ctor_hir_id, Node :: Ctor ( struct_def) ) ;
282
- }
236
+ self . insert_owner ( i. def_id , OwnerNode :: Item ( i) ) ;
237
+ self . with_dep_node_owner ( i. def_id , |this| {
238
+ if let ItemKind :: Struct ( ref struct_def, _) = i. kind {
239
+ // If this is a tuple or unit-like struct, register the constructor.
240
+ if let Some ( ctor_hir_id) = struct_def. ctor_hir_id ( ) {
241
+ this. insert ( i. span , ctor_hir_id, Node :: Ctor ( struct_def) ) ;
283
242
}
284
- intravisit :: walk_item ( this , i ) ;
285
- } ) ;
243
+ }
244
+ intravisit :: walk_item ( this , i ) ;
286
245
} ) ;
287
246
}
288
247
289
248
fn visit_foreign_item ( & mut self , fi : & ' hir ForeignItem < ' hir > ) {
290
- self . with_dep_node_owner ( fi. def_id , fi, |this, hash| {
291
- this. insert_with_hash ( fi. span , fi. hir_id ( ) , Node :: ForeignItem ( fi) , hash) ;
292
-
293
- this. with_parent ( fi. hir_id ( ) , |this| {
294
- intravisit:: walk_foreign_item ( this, fi) ;
295
- } ) ;
249
+ self . insert_owner ( fi. def_id , OwnerNode :: ForeignItem ( fi) ) ;
250
+ self . with_dep_node_owner ( fi. def_id , |this| {
251
+ intravisit:: walk_foreign_item ( this, fi) ;
296
252
} ) ;
297
253
}
298
254
@@ -302,26 +258,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
302
258
}
303
259
304
260
fn visit_const_param_default ( & mut self , param : HirId , ct : & ' hir AnonConst ) {
305
- self . with_parent ( param, |this| intravisit:: walk_const_param_default ( this, ct) )
261
+ self . with_parent ( param, |this| {
262
+ intravisit:: walk_const_param_default ( this, ct) ;
263
+ } )
306
264
}
307
265
308
266
fn visit_trait_item ( & mut self , ti : & ' hir TraitItem < ' hir > ) {
309
- self . with_dep_node_owner ( ti. def_id , ti, |this, hash| {
310
- this. insert_with_hash ( ti. span , ti. hir_id ( ) , Node :: TraitItem ( ti) , hash) ;
311
-
312
- this. with_parent ( ti. hir_id ( ) , |this| {
313
- intravisit:: walk_trait_item ( this, ti) ;
314
- } ) ;
267
+ self . insert_owner ( ti. def_id , OwnerNode :: TraitItem ( ti) ) ;
268
+ self . with_dep_node_owner ( ti. def_id , |this| {
269
+ intravisit:: walk_trait_item ( this, ti) ;
315
270
} ) ;
316
271
}
317
272
318
273
fn visit_impl_item ( & mut self , ii : & ' hir ImplItem < ' hir > ) {
319
- self . with_dep_node_owner ( ii. def_id , ii, |this, hash| {
320
- this. insert_with_hash ( ii. span , ii. hir_id ( ) , Node :: ImplItem ( ii) , hash) ;
321
-
322
- this. with_parent ( ii. hir_id ( ) , |this| {
323
- intravisit:: walk_impl_item ( this, ii) ;
324
- } ) ;
274
+ self . insert_owner ( ii. def_id , OwnerNode :: ImplItem ( ii) ) ;
275
+ self . with_dep_node_owner ( ii. def_id , |this| {
276
+ intravisit:: walk_impl_item ( this, ii) ;
325
277
} ) ;
326
278
}
327
279
@@ -413,7 +365,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
413
365
414
366
fn visit_local ( & mut self , l : & ' hir Local < ' hir > ) {
415
367
self . insert ( l. span , l. hir_id , Node :: Local ( l) ) ;
416
- self . with_parent ( l. hir_id , |this| intravisit:: walk_local ( this, l) )
368
+ self . with_parent ( l. hir_id , |this| {
369
+ intravisit:: walk_local ( this, l) ;
370
+ } )
417
371
}
418
372
419
373
fn visit_lifetime ( & mut self , lifetime : & ' hir Lifetime ) {
@@ -440,16 +394,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
440
394
let parent = def_key. parent . map_or ( hir:: CRATE_HIR_ID , |local_def_index| {
441
395
self . definitions . local_def_id_to_hir_id ( LocalDefId { local_def_index } )
442
396
} ) ;
397
+ self . insert_owner ( macro_def. def_id , OwnerNode :: MacroDef ( macro_def) ) ;
443
398
self . with_parent ( parent, |this| {
444
399
this. insert_nested ( macro_def. def_id ) ;
445
- this. with_dep_node_owner ( macro_def. def_id , macro_def, |this, hash| {
446
- this. insert_with_hash (
447
- macro_def. span ,
448
- macro_def. hir_id ( ) ,
449
- Node :: MacroDef ( macro_def) ,
450
- hash,
451
- ) ;
452
- } )
453
400
} ) ;
454
401
}
455
402
0 commit comments