@@ -81,12 +81,13 @@ pub struct Definitions {
81
81
82
82
def_id_to_span : IndexVec < LocalDefId , Span > ,
83
83
84
+ // FIXME(eddyb) don't go through `ast::NodeId` to convert between `HirId`
85
+ // and `LocalDefId` - ideally all `LocalDefId`s would be HIR owners.
84
86
node_id_to_def_id : FxHashMap < ast:: NodeId , LocalDefId > ,
85
87
def_id_to_node_id : IndexVec < LocalDefId , ast:: NodeId > ,
86
88
87
- // FIXME(eddyb) ideally all `LocalDefId`s would be HIR owners.
88
- pub ( super ) def_id_to_hir_id : IndexVec < LocalDefId , Option < hir:: HirId > > ,
89
- /// The reverse mapping of `def_id_to_hir_id`.
89
+ pub ( super ) node_id_to_hir_id : IndexVec < ast:: NodeId , Option < hir:: HirId > > ,
90
+ /// The pre-computed mapping of `hir_id_to_node_id` -> `node_id_to_def_id`.
90
91
pub ( super ) hir_id_to_def_id : FxHashMap < hir:: HirId , LocalDefId > ,
91
92
92
93
/// If `ExpnId` is an ID of some macro expansion,
@@ -326,7 +327,9 @@ impl Definitions {
326
327
327
328
#[ inline]
328
329
pub fn local_def_id ( & self , node : ast:: NodeId ) -> LocalDefId {
329
- self . opt_local_def_id ( node) . unwrap_or_else ( || panic ! ( "no entry for node id: `{:?}`" , node) )
330
+ self . opt_local_def_id ( node) . unwrap_or_else ( || {
331
+ panic ! ( "no entry for node id: `{:?}` / `{:?}`" , node, self . node_id_to_hir_id. get( node) )
332
+ } )
330
333
}
331
334
332
335
#[ inline]
@@ -336,12 +339,14 @@ impl Definitions {
336
339
337
340
#[ inline]
338
341
pub fn local_def_id_to_hir_id ( & self , id : LocalDefId ) -> hir:: HirId {
339
- self . def_id_to_hir_id [ id] . unwrap ( )
342
+ let node_id = self . def_id_to_node_id [ id] ;
343
+ self . node_id_to_hir_id [ node_id] . unwrap ( )
340
344
}
341
345
342
346
#[ inline]
343
347
pub fn opt_local_def_id_to_hir_id ( & self , id : LocalDefId ) -> Option < hir:: HirId > {
344
- self . def_id_to_hir_id [ id]
348
+ let node_id = self . def_id_to_node_id [ id] ;
349
+ self . node_id_to_hir_id [ node_id]
345
350
}
346
351
347
352
#[ inline]
@@ -456,20 +461,16 @@ impl Definitions {
456
461
mapping : IndexVec < ast:: NodeId , Option < hir:: HirId > > ,
457
462
) {
458
463
assert ! (
459
- self . def_id_to_hir_id . is_empty( ) ,
460
- "trying to initialize `LocalDefId` < -> `HirId` mappings twice"
464
+ self . node_id_to_hir_id . is_empty( ) ,
465
+ "trying to initialize `NodeId` -> `HirId` mapping twice"
461
466
) ;
467
+ self . node_id_to_hir_id = mapping;
462
468
463
- self . def_id_to_hir_id = self
464
- . def_id_to_node_id
465
- . iter ( )
466
- . map ( |& node_id| mapping. get ( node_id) . and_then ( |& hir_id| hir_id) )
467
- . collect ( ) ;
468
-
469
- // Build the reverse mapping of `def_id_to_hir_id`.
470
- self . hir_id_to_def_id = mapping
471
- . into_iter_enumerated ( )
472
- . filter_map ( |( node_id, hir_id) | {
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
471
+ . node_id_to_hir_id
472
+ . iter_enumerated ( )
473
+ . filter_map ( |( node_id, & hir_id) | {
473
474
hir_id. and_then ( |hir_id| {
474
475
self . node_id_to_def_id . get ( & node_id) . map ( |& def_id| ( hir_id, def_id) )
475
476
} )
0 commit comments