1
1
use crate :: arena:: Arena ;
2
2
use crate :: hir:: map:: { HirOwnerData , Map } ;
3
- use crate :: hir:: { Owner , OwnerNodes , ParentedNode } ;
3
+ use crate :: hir:: { IndexedHir , Owner , OwnerNodes , ParentedNode } ;
4
4
use crate :: ich:: StableHashingContext ;
5
5
use rustc_data_structures:: fingerprint:: Fingerprint ;
6
6
use rustc_data_structures:: fx:: FxHashMap ;
@@ -29,6 +29,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
29
29
source_map : & ' a SourceMap ,
30
30
31
31
map : IndexVec < LocalDefId , HirOwnerData < ' hir > > ,
32
+ parenting : FxHashMap < LocalDefId , HirId > ,
32
33
33
34
/// The parent of this node
34
35
parent_node : hir:: HirId ,
@@ -109,6 +110,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
109
110
map : ( 0 ..definitions. def_index_count ( ) )
110
111
. map ( |_| HirOwnerData { signature : None , with_bodies : None } )
111
112
. collect ( ) ,
113
+ parenting : FxHashMap :: default ( ) ,
112
114
} ;
113
115
collector. insert_entry (
114
116
hir:: CRATE_HIR_ID ,
@@ -119,15 +121,13 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
119
121
collector
120
122
}
121
123
122
- pub ( super ) fn finalize_and_compute_crate_hash (
123
- mut self ,
124
- ) -> IndexVec < LocalDefId , HirOwnerData < ' hir > > {
124
+ pub ( super ) fn finalize_and_compute_crate_hash ( mut self ) -> IndexedHir < ' hir > {
125
125
// Insert bodies into the map
126
126
for ( id, body) in self . krate . bodies . iter ( ) {
127
127
let bodies = & mut self . map [ id. hir_id . owner ] . with_bodies . as_mut ( ) . unwrap ( ) . bodies ;
128
128
assert ! ( bodies. insert( id. hir_id. local_id, body) . is_none( ) ) ;
129
129
}
130
- self . map
130
+ IndexedHir { map : self . map , parenting : self . parenting }
131
131
}
132
132
133
133
fn insert_entry ( & mut self , id : HirId , entry : Entry < ' hir > , hash : Fingerprint ) {
@@ -152,8 +152,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
152
152
nodes. hash = hash;
153
153
154
154
debug_assert ! ( data. signature. is_none( ) ) ;
155
- data. signature =
156
- Some ( self . arena . alloc ( Owner { parent : entry. parent , node : entry. node } ) ) ;
155
+ data. signature = Some ( self . arena . alloc ( Owner { node : entry. node } ) ) ;
157
156
158
157
let dk_parent = self . definitions . def_key ( id. owner ) . parent ;
159
158
if let Some ( dk_parent) = dk_parent {
@@ -165,6 +164,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
165
164
id. owner, dk_parent, entry. parent,
166
165
)
167
166
}
167
+
168
+ debug_assert_eq ! ( self . parenting. get( & id. owner) , Some ( & entry. parent) ) ;
168
169
}
169
170
} else {
170
171
assert_eq ! ( entry. parent. owner, id. owner) ;
@@ -234,6 +235,22 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
234
235
f ( self , hash) ;
235
236
self . current_dep_node_owner = prev_owner;
236
237
}
238
+
239
+ fn insert_nested ( & mut self , item : LocalDefId ) {
240
+ #[ cfg( debug_assertions) ]
241
+ {
242
+ let dk_parent = self . definitions . def_key ( item) . parent . unwrap ( ) ;
243
+ let dk_parent = LocalDefId { local_def_index : dk_parent } ;
244
+ let dk_parent = self . definitions . local_def_id_to_hir_id ( dk_parent) ;
245
+ debug_assert_eq ! (
246
+ dk_parent. owner, self . parent_node. owner,
247
+ "Different parents for {:?}" ,
248
+ item
249
+ )
250
+ }
251
+
252
+ assert_eq ! ( self . parenting. insert( item, self . parent_node) , None ) ;
253
+ }
237
254
}
238
255
239
256
impl < ' a , ' hir > Visitor < ' hir > for NodeCollector < ' a , ' hir > {
@@ -249,18 +266,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
249
266
250
267
fn visit_nested_item ( & mut self , item : ItemId ) {
251
268
debug ! ( "visit_nested_item: {:?}" , item) ;
269
+ self . insert_nested ( item. def_id ) ;
252
270
self . visit_item ( self . krate . item ( item) ) ;
253
271
}
254
272
255
273
fn visit_nested_trait_item ( & mut self , item_id : TraitItemId ) {
274
+ self . insert_nested ( item_id. def_id ) ;
256
275
self . visit_trait_item ( self . krate . trait_item ( item_id) ) ;
257
276
}
258
277
259
278
fn visit_nested_impl_item ( & mut self , item_id : ImplItemId ) {
279
+ self . insert_nested ( item_id. def_id ) ;
260
280
self . visit_impl_item ( self . krate . impl_item ( item_id) ) ;
261
281
}
262
282
263
283
fn visit_nested_foreign_item ( & mut self , foreign_id : ForeignItemId ) {
284
+ self . insert_nested ( foreign_id. def_id ) ;
264
285
self . visit_foreign_item ( self . krate . foreign_item ( foreign_id) ) ;
265
286
}
266
287
@@ -448,6 +469,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
448
469
self . definitions . local_def_id_to_hir_id ( LocalDefId { local_def_index } )
449
470
} ) ;
450
471
self . with_parent ( parent, |this| {
472
+ this. insert_nested ( macro_def. def_id ) ;
451
473
this. with_dep_node_owner ( macro_def. def_id , macro_def, |this, hash| {
452
474
this. insert_with_hash (
453
475
macro_def. span ,
0 commit comments