7
7
pub use crate :: def_id:: DefPathHash ;
8
8
use crate :: def_id:: { CrateNum , DefId , DefIndex , LocalDefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
9
9
use crate :: hir;
10
- use crate :: hir_id:: DUMMY_HIR_ID ;
11
10
12
11
use rustc_ast:: ast;
13
12
use rustc_ast:: crate_disambiguator:: CrateDisambiguator ;
@@ -87,7 +86,7 @@ pub struct Definitions {
87
86
node_id_to_def_id : FxHashMap < ast:: NodeId , LocalDefId > ,
88
87
def_id_to_node_id : IndexVec < LocalDefId , ast:: NodeId > ,
89
88
90
- pub ( super ) node_id_to_hir_id : IndexVec < ast:: NodeId , hir:: HirId > ,
89
+ pub ( super ) node_id_to_hir_id : IndexVec < ast:: NodeId , Option < hir:: HirId > > ,
91
90
/// The reverse mapping of `node_id_to_hir_id`.
92
91
pub ( super ) hir_id_to_node_id : FxHashMap < hir:: HirId , ast:: NodeId > ,
93
92
@@ -345,8 +344,7 @@ impl Definitions {
345
344
#[ inline]
346
345
pub fn as_local_hir_id ( & self , def_id : DefId ) -> Option < hir:: HirId > {
347
346
if let Some ( def_id) = def_id. as_local ( ) {
348
- let hir_id = self . local_def_id_to_hir_id ( def_id) ;
349
- if hir_id != DUMMY_HIR_ID { Some ( hir_id) } else { None }
347
+ Some ( self . local_def_id_to_hir_id ( def_id) )
350
348
} else {
351
349
None
352
350
}
@@ -359,11 +357,22 @@ impl Definitions {
359
357
360
358
#[ inline]
361
359
pub fn node_id_to_hir_id ( & self , node_id : ast:: NodeId ) -> hir:: HirId {
360
+ self . node_id_to_hir_id [ node_id] . unwrap ( )
361
+ }
362
+
363
+ #[ inline]
364
+ pub fn opt_node_id_to_hir_id ( & self , node_id : ast:: NodeId ) -> Option < hir:: HirId > {
362
365
self . node_id_to_hir_id [ node_id]
363
366
}
364
367
365
368
#[ inline]
366
369
pub fn local_def_id_to_hir_id ( & self , id : LocalDefId ) -> hir:: HirId {
370
+ let node_id = self . def_id_to_node_id [ id] ;
371
+ self . node_id_to_hir_id [ node_id] . unwrap ( )
372
+ }
373
+
374
+ #[ inline]
375
+ pub fn opt_local_def_id_to_hir_id ( & self , id : LocalDefId ) -> Option < hir:: HirId > {
367
376
let node_id = self . def_id_to_node_id [ id] ;
368
377
self . node_id_to_hir_id [ node_id]
369
378
}
@@ -470,7 +479,10 @@ impl Definitions {
470
479
471
480
/// Initializes the `ast::NodeId` to `HirId` mapping once it has been generated during
472
481
/// AST to HIR lowering.
473
- pub fn init_node_id_to_hir_id_mapping ( & mut self , mapping : IndexVec < ast:: NodeId , hir:: HirId > ) {
482
+ pub fn init_node_id_to_hir_id_mapping (
483
+ & mut self ,
484
+ mapping : IndexVec < ast:: NodeId , Option < hir:: HirId > > ,
485
+ ) {
474
486
assert ! (
475
487
self . node_id_to_hir_id. is_empty( ) ,
476
488
"trying to initialize `NodeId` -> `HirId` mapping twice"
@@ -481,7 +493,7 @@ impl Definitions {
481
493
self . hir_id_to_node_id = self
482
494
. node_id_to_hir_id
483
495
. iter_enumerated ( )
484
- . map ( |( node_id, & hir_id) | ( hir_id, node_id) )
496
+ . filter_map ( |( node_id, & hir_id) | hir_id . map ( | hir_id| ( hir_id , node_id) ) )
485
497
. collect ( ) ;
486
498
}
487
499
0 commit comments