@@ -7,7 +7,7 @@ use rustc_data_structures::fx::FxHashMap;
77use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
88use rustc_hir as hir;
99use rustc_hir:: def_id:: LocalDefId ;
10- use rustc_hir:: def_id:: CRATE_DEF_INDEX ;
10+ use rustc_hir:: def_id:: CRATE_DEF_ID ;
1111use rustc_hir:: definitions;
1212use rustc_hir:: intravisit:: { self , NestedVisitorMap , Visitor } ;
1313use rustc_hir:: * ;
@@ -75,26 +75,20 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
7575 arena : & ' hir Arena < ' hir > ,
7676 krate : & ' hir Crate < ' hir > ,
7777 definitions : & ' a definitions:: Definitions ,
78- mut hcx : StableHashingContext < ' a > ,
78+ hcx : StableHashingContext < ' a > ,
7979 ) -> NodeCollector < ' a , ' hir > {
80- let hash = hash_body ( & mut hcx, krate. module ( ) ) ;
81-
8280 let mut collector = NodeCollector {
8381 arena,
8482 krate,
8583 source_map : sess. source_map ( ) ,
8684 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 ,
8886 definitions,
8987 hcx,
9088 map : IndexVec :: from_fn_n ( |_| None , definitions. def_index_count ( ) ) ,
9189 parenting : FxHashMap :: default ( ) ,
9290 } ;
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 ( ) ) ) ;
9892
9993 collector
10094 }
@@ -108,53 +102,20 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
108102 IndexedHir { map : self . map , parenting : self . parenting }
109103 }
110104
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) ;
142107
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 ( ) } ) ) ;
144110
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 ( ) } ) ) ;
150114 }
151115
152116 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 ) ;
158119
159120 // Make sure that the DepNode of some node coincides with the HirId
160121 // owner of that node.
@@ -181,7 +142,14 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
181142 }
182143 }
183144
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+ ) ;
185153 }
186154
187155 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> {
191159 self . parent_node = parent_node;
192160 }
193161
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 ) ) {
203163 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 ;
205165
206166 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 ) ;
208169 self . current_dep_node_owner = prev_owner;
170+ self . parent_node = prev_parent;
209171 }
210172
211173 fn insert_nested ( & mut self , item : LocalDefId ) {
@@ -271,28 +233,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
271233
272234 fn visit_item ( & mut self , i : & ' hir Item < ' hir > ) {
273235 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) ) ;
283242 }
284- intravisit :: walk_item ( this , i ) ;
285- } ) ;
243+ }
244+ intravisit :: walk_item ( this , i ) ;
286245 } ) ;
287246 }
288247
289248 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) ;
296252 } ) ;
297253 }
298254
@@ -302,26 +258,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
302258 }
303259
304260 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+ } )
306264 }
307265
308266 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) ;
315270 } ) ;
316271 }
317272
318273 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) ;
325277 } ) ;
326278 }
327279
@@ -413,7 +365,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
413365
414366 fn visit_local ( & mut self , l : & ' hir Local < ' hir > ) {
415367 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+ } )
417371 }
418372
419373 fn visit_lifetime ( & mut self , lifetime : & ' hir Lifetime ) {
@@ -440,16 +394,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
440394 let parent = def_key. parent . map_or ( hir:: CRATE_HIR_ID , |local_def_index| {
441395 self . definitions . local_def_id_to_hir_id ( LocalDefId { local_def_index } )
442396 } ) ;
397+ self . insert_owner ( macro_def. def_id , OwnerNode :: MacroDef ( macro_def) ) ;
443398 self . with_parent ( parent, |this| {
444399 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- } )
453400 } ) ;
454401 }
455402
0 commit comments