@@ -27,9 +27,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
27
27
/// The node map
28
28
map : Vec < Option < Entry < ' hir > > > ,
29
29
/// The parent of this node
30
- parent_node : NodeId ,
31
-
32
- parent_hir : hir:: HirId ,
30
+ parent_node : hir:: HirId ,
33
31
34
32
// These fields keep track of the currently relevant DepNodes during
35
33
// the visitor's traversal.
@@ -40,6 +38,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
40
38
41
39
dep_graph : & ' a DepGraph ,
42
40
definitions : & ' a definitions:: Definitions ,
41
+ hir_to_node_id : & ' a FxHashMap < HirId , NodeId > ,
43
42
44
43
hcx : StableHashingContext < ' a > ,
45
44
@@ -100,6 +99,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
100
99
krate : & ' hir Crate ,
101
100
dep_graph : & ' a DepGraph ,
102
101
definitions : & ' a definitions:: Definitions ,
102
+ hir_to_node_id : & ' a FxHashMap < HirId , NodeId > ,
103
103
mut hcx : StableHashingContext < ' a > )
104
104
-> NodeCollector < ' a , ' hir > {
105
105
let root_mod_def_path_hash = definitions. def_path_hash ( CRATE_DEF_INDEX ) ;
@@ -147,14 +147,14 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
147
147
krate,
148
148
source_map : sess. source_map ( ) ,
149
149
map : repeat ( None ) . take ( sess. current_node_id_count ( ) ) . collect ( ) ,
150
- parent_node : CRATE_NODE_ID ,
151
- parent_hir : hir:: CRATE_HIR_ID ,
150
+ parent_node : hir:: CRATE_HIR_ID ,
152
151
current_signature_dep_index : root_mod_sig_dep_index,
153
152
current_full_dep_index : root_mod_full_dep_index,
154
153
current_dep_node_owner : CRATE_DEF_INDEX ,
155
154
currently_in_body : false ,
156
155
dep_graph,
157
156
definitions,
157
+ hir_to_node_id,
158
158
hcx,
159
159
hir_body_nodes,
160
160
} ;
@@ -228,10 +228,10 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
228
228
self . map [ id. as_usize ( ) ] = Some ( entry) ;
229
229
}
230
230
231
- fn insert ( & mut self , span : Span , id : NodeId , node : Node < ' hir > ) {
231
+ fn insert ( & mut self , span : Span , hir_id : HirId , node : Node < ' hir > ) {
232
232
let entry = Entry {
233
- parent : self . parent_node ,
234
- parent_hir : self . parent_hir ,
233
+ parent : self . hir_to_node_id [ & self . parent_node ] ,
234
+ parent_hir : self . parent_node ,
235
235
dep_node : if self . currently_in_body {
236
236
self . current_full_dep_index
237
237
} else {
@@ -240,21 +240,23 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
240
240
node,
241
241
} ;
242
242
243
+ let node_id = self . hir_to_node_id [ & hir_id] ;
244
+
243
245
// Make sure that the DepNode of some node coincides with the HirId
244
246
// owner of that node.
245
247
if cfg ! ( debug_assertions) {
246
- let hir_id = self . definitions . node_to_hir_id ( id ) ;
248
+ assert_eq ! ( self . definitions. node_to_hir_id( node_id ) , hir_id ) ;
247
249
248
250
if hir_id. owner != self . current_dep_node_owner {
249
- let node_str = match self . definitions . opt_def_index ( id ) {
251
+ let node_str = match self . definitions . opt_def_index ( node_id ) {
250
252
Some ( def_index) => {
251
253
self . definitions . def_path ( def_index) . to_string_no_crate ( )
252
254
}
253
255
None => format ! ( "{:?}" , node)
254
256
} ;
255
257
256
258
let forgot_str = if hir_id == crate :: hir:: DUMMY_HIR_ID {
257
- format ! ( "\n Maybe you forgot to lower the node id {:?}?" , id )
259
+ format ! ( "\n Maybe you forgot to lower the node id {:?}?" , node_id )
258
260
} else {
259
261
String :: new ( )
260
262
} ;
@@ -276,12 +278,16 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
276
278
}
277
279
}
278
280
279
- self . insert_entry ( id , entry) ;
281
+ self . insert_entry ( node_id , entry) ;
280
282
}
281
283
282
- fn with_parent < F : FnOnce ( & mut Self ) > ( & mut self , parent_id : NodeId , f : F ) {
284
+ fn with_parent < F : FnOnce ( & mut Self ) > (
285
+ & mut self ,
286
+ parent_node_id : HirId ,
287
+ f : F ,
288
+ ) {
283
289
let parent_node = self . parent_node ;
284
- self . parent_node = parent_id ;
290
+ self . parent_node = parent_node_id ;
285
291
f ( self ) ;
286
292
self . parent_node = parent_node;
287
293
}
@@ -352,12 +358,12 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
352
358
debug_assert_eq ! ( i. hir_id. owner,
353
359
self . definitions. opt_def_index( i. id) . unwrap( ) ) ;
354
360
self . with_dep_node_owner ( i. hir_id . owner , i, |this| {
355
- this. insert ( i. span , i. id , Node :: Item ( i) ) ;
356
- this. with_parent ( i. id , |this| {
361
+ this. insert ( i. span , i. hir_id , Node :: Item ( i) ) ;
362
+ this. with_parent ( i. hir_id , |this| {
357
363
if let ItemKind :: Struct ( ref struct_def, _) = i. node {
358
364
// If this is a tuple-like struct, register the constructor.
359
365
if !struct_def. is_struct ( ) {
360
- this. insert ( i. span , struct_def. id ( ) , Node :: StructCtor ( struct_def) ) ;
366
+ this. insert ( i. span , struct_def. hir_id ( ) , Node :: StructCtor ( struct_def) ) ;
361
367
}
362
368
}
363
369
intravisit:: walk_item ( this, i) ;
@@ -366,25 +372,25 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
366
372
}
367
373
368
374
fn visit_foreign_item ( & mut self , foreign_item : & ' hir ForeignItem ) {
369
- self . insert ( foreign_item. span , foreign_item. id , Node :: ForeignItem ( foreign_item) ) ;
375
+ self . insert ( foreign_item. span , foreign_item. hir_id , Node :: ForeignItem ( foreign_item) ) ;
370
376
371
- self . with_parent ( foreign_item. id , |this| {
377
+ self . with_parent ( foreign_item. hir_id , |this| {
372
378
intravisit:: walk_foreign_item ( this, foreign_item) ;
373
379
} ) ;
374
380
}
375
381
376
382
fn visit_generic_param ( & mut self , param : & ' hir GenericParam ) {
377
- self . insert ( param. span , param. id , Node :: GenericParam ( param) ) ;
383
+ self . insert ( param. span , param. hir_id , Node :: GenericParam ( param) ) ;
378
384
intravisit:: walk_generic_param ( self , param) ;
379
385
}
380
386
381
387
fn visit_trait_item ( & mut self , ti : & ' hir TraitItem ) {
382
388
debug_assert_eq ! ( ti. hir_id. owner,
383
389
self . definitions. opt_def_index( ti. id) . unwrap( ) ) ;
384
390
self . with_dep_node_owner ( ti. hir_id . owner , ti, |this| {
385
- this. insert ( ti. span , ti. id , Node :: TraitItem ( ti) ) ;
391
+ this. insert ( ti. span , ti. hir_id , Node :: TraitItem ( ti) ) ;
386
392
387
- this. with_parent ( ti. id , |this| {
393
+ this. with_parent ( ti. hir_id , |this| {
388
394
intravisit:: walk_trait_item ( this, ti) ;
389
395
} ) ;
390
396
} ) ;
@@ -394,9 +400,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
394
400
debug_assert_eq ! ( ii. hir_id. owner,
395
401
self . definitions. opt_def_index( ii. id) . unwrap( ) ) ;
396
402
self . with_dep_node_owner ( ii. hir_id . owner , ii, |this| {
397
- this. insert ( ii. span , ii. id , Node :: ImplItem ( ii) ) ;
403
+ this. insert ( ii. span , ii. hir_id , Node :: ImplItem ( ii) ) ;
398
404
399
- this. with_parent ( ii. id , |this| {
405
+ this. with_parent ( ii. hir_id , |this| {
400
406
intravisit:: walk_impl_item ( this, ii) ;
401
407
} ) ;
402
408
} ) ;
@@ -408,93 +414,92 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
408
414
} else {
409
415
Node :: Pat ( pat)
410
416
} ;
411
- self . insert ( pat. span , pat. id , node) ;
417
+ self . insert ( pat. span , pat. hir_id , node) ;
412
418
413
- self . with_parent ( pat. id , |this| {
419
+ self . with_parent ( pat. hir_id , |this| {
414
420
intravisit:: walk_pat ( this, pat) ;
415
421
} ) ;
416
422
}
417
423
418
424
fn visit_anon_const ( & mut self , constant : & ' hir AnonConst ) {
419
- self . insert ( DUMMY_SP , constant. id , Node :: AnonConst ( constant) ) ;
425
+ self . insert ( DUMMY_SP , constant. hir_id , Node :: AnonConst ( constant) ) ;
420
426
421
- self . with_parent ( constant. id , |this| {
427
+ self . with_parent ( constant. hir_id , |this| {
422
428
intravisit:: walk_anon_const ( this, constant) ;
423
429
} ) ;
424
430
}
425
431
426
432
fn visit_expr ( & mut self , expr : & ' hir Expr ) {
427
- self . insert ( expr. span , expr. id , Node :: Expr ( expr) ) ;
433
+ self . insert ( expr. span , expr. hir_id , Node :: Expr ( expr) ) ;
428
434
429
- self . with_parent ( expr. id , |this| {
435
+ self . with_parent ( expr. hir_id , |this| {
430
436
intravisit:: walk_expr ( this, expr) ;
431
437
} ) ;
432
438
}
433
439
434
440
fn visit_stmt ( & mut self , stmt : & ' hir Stmt ) {
435
- let id = stmt. id ;
436
- self . insert ( stmt. span , id, Node :: Stmt ( stmt) ) ;
441
+ self . insert ( stmt. span , stmt. hir_id , Node :: Stmt ( stmt) ) ;
437
442
438
- self . with_parent ( id , |this| {
443
+ self . with_parent ( stmt . hir_id , |this| {
439
444
intravisit:: walk_stmt ( this, stmt) ;
440
445
} ) ;
441
446
}
442
447
443
448
fn visit_path_segment ( & mut self , path_span : Span , path_segment : & ' hir PathSegment ) {
444
- if let Some ( id ) = path_segment. id {
445
- self . insert ( path_span, id , Node :: PathSegment ( path_segment) ) ;
449
+ if let Some ( hir_id ) = path_segment. hir_id {
450
+ self . insert ( path_span, hir_id , Node :: PathSegment ( path_segment) ) ;
446
451
}
447
452
intravisit:: walk_path_segment ( self , path_span, path_segment) ;
448
453
}
449
454
450
455
fn visit_ty ( & mut self , ty : & ' hir Ty ) {
451
- self . insert ( ty. span , ty. id , Node :: Ty ( ty) ) ;
456
+ self . insert ( ty. span , ty. hir_id , Node :: Ty ( ty) ) ;
452
457
453
- self . with_parent ( ty. id , |this| {
458
+ self . with_parent ( ty. hir_id , |this| {
454
459
intravisit:: walk_ty ( this, ty) ;
455
460
} ) ;
456
461
}
457
462
458
463
fn visit_trait_ref ( & mut self , tr : & ' hir TraitRef ) {
459
- self . insert ( tr. path . span , tr. ref_id , Node :: TraitRef ( tr) ) ;
464
+ self . insert ( tr. path . span , tr. hir_ref_id , Node :: TraitRef ( tr) ) ;
460
465
461
- self . with_parent ( tr. ref_id , |this| {
466
+ self . with_parent ( tr. hir_ref_id , |this| {
462
467
intravisit:: walk_trait_ref ( this, tr) ;
463
468
} ) ;
464
469
}
465
470
466
471
fn visit_fn ( & mut self , fk : intravisit:: FnKind < ' hir > , fd : & ' hir FnDecl ,
467
- b : BodyId , s : Span , id : NodeId ) {
472
+ b : BodyId , s : Span , id : HirId ) {
468
473
assert_eq ! ( self . parent_node, id) ;
469
474
intravisit:: walk_fn ( self , fk, fd, b, s, id) ;
470
475
}
471
476
472
477
fn visit_block ( & mut self , block : & ' hir Block ) {
473
- self . insert ( block. span , block. id , Node :: Block ( block) ) ;
474
- self . with_parent ( block. id , |this| {
478
+ self . insert ( block. span , block. hir_id , Node :: Block ( block) ) ;
479
+ self . with_parent ( block. hir_id , |this| {
475
480
intravisit:: walk_block ( this, block) ;
476
481
} ) ;
477
482
}
478
483
479
484
fn visit_local ( & mut self , l : & ' hir Local ) {
480
- self . insert ( l. span , l. id , Node :: Local ( l) ) ;
481
- self . with_parent ( l. id , |this| {
485
+ self . insert ( l. span , l. hir_id , Node :: Local ( l) ) ;
486
+ self . with_parent ( l. hir_id , |this| {
482
487
intravisit:: walk_local ( this, l)
483
488
} )
484
489
}
485
490
486
491
fn visit_lifetime ( & mut self , lifetime : & ' hir Lifetime ) {
487
- self . insert ( lifetime. span , lifetime. id , Node :: Lifetime ( lifetime) ) ;
492
+ self . insert ( lifetime. span , lifetime. hir_id , Node :: Lifetime ( lifetime) ) ;
488
493
}
489
494
490
495
fn visit_vis ( & mut self , visibility : & ' hir Visibility ) {
491
496
match visibility. node {
492
497
VisibilityKind :: Public |
493
498
VisibilityKind :: Crate ( _) |
494
499
VisibilityKind :: Inherited => { }
495
- VisibilityKind :: Restricted { id , .. } => {
496
- self . insert ( visibility. span , id , Node :: Visibility ( visibility) ) ;
497
- self . with_parent ( id , |this| {
500
+ VisibilityKind :: Restricted { hir_id , .. } => {
501
+ self . insert ( visibility. span , hir_id , Node :: Visibility ( visibility) ) ;
502
+ self . with_parent ( hir_id , |this| {
498
503
intravisit:: walk_vis ( this, visibility) ;
499
504
} ) ;
500
505
}
@@ -505,21 +510,20 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
505
510
let def_index = self . definitions . opt_def_index ( macro_def. id ) . unwrap ( ) ;
506
511
507
512
self . with_dep_node_owner ( def_index, macro_def, |this| {
508
- this. insert ( macro_def. span , macro_def. id , Node :: MacroDef ( macro_def) ) ;
513
+ this. insert ( macro_def. span , macro_def. hir_id , Node :: MacroDef ( macro_def) ) ;
509
514
} ) ;
510
515
}
511
516
512
- fn visit_variant ( & mut self , v : & ' hir Variant , g : & ' hir Generics , item_id : NodeId ) {
513
- let id = v. node . data . id ( ) ;
514
- self . insert ( v. span , id, Node :: Variant ( v) ) ;
515
- self . with_parent ( id, |this| {
517
+ fn visit_variant ( & mut self , v : & ' hir Variant , g : & ' hir Generics , item_id : HirId ) {
518
+ self . insert ( v. span , v. node . data . hir_id ( ) , Node :: Variant ( v) ) ;
519
+ self . with_parent ( v. node . data . hir_id ( ) , |this| {
516
520
intravisit:: walk_variant ( this, v, g, item_id) ;
517
521
} ) ;
518
522
}
519
523
520
524
fn visit_struct_field ( & mut self , field : & ' hir StructField ) {
521
- self . insert ( field. span , field. id , Node :: Field ( field) ) ;
522
- self . with_parent ( field. id , |this| {
525
+ self . insert ( field. span , field. hir_id , Node :: Field ( field) ) ;
526
+ self . with_parent ( field. hir_id , |this| {
523
527
intravisit:: walk_struct_field ( this, field) ;
524
528
} ) ;
525
529
}
0 commit comments