@@ -87,8 +87,8 @@ pub struct Definitions {
87
87
def_id_to_node_id : IndexVec < LocalDefId , ast:: NodeId > ,
88
88
89
89
pub ( super ) node_id_to_hir_id : IndexVec < ast:: NodeId , Option < hir:: HirId > > ,
90
- /// The reverse mapping of `node_id_to_hir_id `.
91
- pub ( super ) hir_id_to_node_id : FxHashMap < hir:: HirId , ast :: NodeId > ,
90
+ /// The pre-computed mapping of `hir_id_to_node_id` -> `node_id_to_def_id `.
91
+ pub ( super ) hir_id_to_def_id : FxHashMap < hir:: HirId , LocalDefId > ,
92
92
93
93
/// If `ExpnId` is an ID of some macro expansion,
94
94
/// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
@@ -351,8 +351,7 @@ impl Definitions {
351
351
352
352
#[ inline]
353
353
pub fn opt_hir_id_to_local_def_id ( & self , hir_id : hir:: HirId ) -> Option < LocalDefId > {
354
- let node_id = self . hir_id_to_node_id [ & hir_id] ;
355
- self . opt_local_def_id ( node_id)
354
+ self . hir_id_to_def_id . get ( & hir_id) . copied ( )
356
355
}
357
356
358
357
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
@@ -467,11 +466,15 @@ impl Definitions {
467
466
) ;
468
467
self . node_id_to_hir_id = mapping;
469
468
470
- // Build the reverse mapping of `node_id_to_hir_id `.
471
- self . hir_id_to_node_id = self
469
+ // Build the pre-computed mapping of `hir_id_to_node_id` -> `node_id_to_def_id `.
470
+ self . hir_id_to_def_id = self
472
471
. node_id_to_hir_id
473
472
. iter_enumerated ( )
474
- . filter_map ( |( node_id, & hir_id) | hir_id. map ( |hir_id| ( hir_id, node_id) ) )
473
+ . filter_map ( |( node_id, & hir_id) | {
474
+ hir_id. and_then ( |hir_id| {
475
+ self . node_id_to_def_id . get ( & node_id) . map ( |& def_id| ( hir_id, def_id) )
476
+ } )
477
+ } )
475
478
. collect ( ) ;
476
479
}
477
480
0 commit comments