@@ -81,14 +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.
86
84
node_id_to_def_id : FxHashMap < ast:: NodeId , LocalDefId > ,
87
85
def_id_to_node_id : IndexVec < LocalDefId , ast:: NodeId > ,
88
86
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 > ,
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`.
90
+ pub ( super ) hir_id_to_def_id : FxHashMap < hir:: HirId , LocalDefId > ,
92
91
93
92
/// If `ExpnId` is an ID of some macro expansion,
94
93
/// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
@@ -327,47 +326,27 @@ impl Definitions {
327
326
328
327
#[ inline]
329
328
pub fn local_def_id ( & self , node : ast:: NodeId ) -> LocalDefId {
330
- self . opt_local_def_id ( node) . unwrap_or_else ( || {
331
- panic ! ( "no entry for node id: `{:?}` / `{:?}`" , node, self . opt_node_id_to_hir_id( node) )
332
- } )
329
+ self . opt_local_def_id ( node) . unwrap_or_else ( || panic ! ( "no entry for node id: `{:?}`" , node) )
333
330
}
334
331
335
332
#[ inline]
336
333
pub fn as_local_hir_id ( & self , def_id : LocalDefId ) -> hir:: HirId {
337
334
self . local_def_id_to_hir_id ( def_id)
338
335
}
339
336
340
- #[ inline]
341
- pub fn hir_id_to_node_id ( & self , hir_id : hir:: HirId ) -> ast:: NodeId {
342
- self . hir_id_to_node_id [ & hir_id]
343
- }
344
-
345
- #[ inline]
346
- pub fn node_id_to_hir_id ( & self , node_id : ast:: NodeId ) -> hir:: HirId {
347
- self . node_id_to_hir_id [ node_id] . unwrap ( )
348
- }
349
-
350
- #[ inline]
351
- pub fn opt_node_id_to_hir_id ( & self , node_id : ast:: NodeId ) -> Option < hir:: HirId > {
352
- self . node_id_to_hir_id [ node_id]
353
- }
354
-
355
337
#[ inline]
356
338
pub fn local_def_id_to_hir_id ( & self , id : LocalDefId ) -> hir:: HirId {
357
- let node_id = self . def_id_to_node_id [ id] ;
358
- self . node_id_to_hir_id [ node_id] . unwrap ( )
339
+ self . def_id_to_hir_id [ id] . unwrap ( )
359
340
}
360
341
361
342
#[ inline]
362
343
pub fn opt_local_def_id_to_hir_id ( & self , id : LocalDefId ) -> Option < hir:: HirId > {
363
- let node_id = self . def_id_to_node_id [ id] ;
364
- self . node_id_to_hir_id [ node_id]
344
+ self . def_id_to_hir_id [ id]
365
345
}
366
346
367
347
#[ inline]
368
348
pub fn opt_hir_id_to_local_def_id ( & self , hir_id : hir:: HirId ) -> Option < LocalDefId > {
369
- let node_id = self . hir_id_to_node_id ( hir_id) ;
370
- self . opt_local_def_id ( node_id)
349
+ self . hir_id_to_def_id . get ( & hir_id) . copied ( )
371
350
}
372
351
373
352
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
@@ -477,16 +456,24 @@ impl Definitions {
477
456
mapping : IndexVec < ast:: NodeId , Option < hir:: HirId > > ,
478
457
) {
479
458
assert ! (
480
- self . node_id_to_hir_id . is_empty( ) ,
481
- "trying to initialize `NodeId` -> `HirId` mapping twice"
459
+ self . def_id_to_hir_id . is_empty( ) ,
460
+ "trying to initialize `LocalDefId` < -> `HirId` mappings twice"
482
461
) ;
483
- self . node_id_to_hir_id = mapping;
484
462
485
- // Build the reverse mapping of `node_id_to_hir_id`.
486
- self . hir_id_to_node_id = self
487
- . node_id_to_hir_id
488
- . iter_enumerated ( )
489
- . filter_map ( |( node_id, & hir_id) | hir_id. map ( |hir_id| ( hir_id, node_id) ) )
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) | {
473
+ hir_id. and_then ( |hir_id| {
474
+ self . node_id_to_def_id . get ( & node_id) . map ( |& def_id| ( hir_id, def_id) )
475
+ } )
476
+ } )
490
477
. collect ( ) ;
491
478
}
492
479
0 commit comments