77pub use crate :: def_id:: DefPathHash ;
88use crate :: def_id:: { CrateNum , DefId , DefIndex , LocalDefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
99use crate :: hir;
10- use crate :: hir_id:: DUMMY_HIR_ID ;
1110
1211use rustc_ast:: ast;
1312use rustc_ast:: crate_disambiguator:: CrateDisambiguator ;
@@ -87,7 +86,7 @@ pub struct Definitions {
8786 node_id_to_def_id : FxHashMap < ast:: NodeId , LocalDefId > ,
8887 def_id_to_node_id : IndexVec < LocalDefId , ast:: NodeId > ,
8988
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 > > ,
9190 /// The reverse mapping of `node_id_to_hir_id`.
9291 pub ( super ) hir_id_to_node_id : FxHashMap < hir:: HirId , ast:: NodeId > ,
9392
@@ -345,8 +344,7 @@ impl Definitions {
345344 #[ inline]
346345 pub fn as_local_hir_id ( & self , def_id : DefId ) -> Option < hir:: HirId > {
347346 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) )
350348 } else {
351349 None
352350 }
@@ -359,11 +357,22 @@ impl Definitions {
359357
360358 #[ inline]
361359 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 > {
362365 self . node_id_to_hir_id [ node_id]
363366 }
364367
365368 #[ inline]
366369 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 > {
367376 let node_id = self . def_id_to_node_id [ id] ;
368377 self . node_id_to_hir_id [ node_id]
369378 }
@@ -470,7 +479,10 @@ impl Definitions {
470479
471480 /// Initializes the `ast::NodeId` to `HirId` mapping once it has been generated during
472481 /// 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+ ) {
474486 assert ! (
475487 self . node_id_to_hir_id. is_empty( ) ,
476488 "trying to initialize `NodeId` -> `HirId` mapping twice"
@@ -481,7 +493,7 @@ impl Definitions {
481493 self . hir_id_to_node_id = self
482494 . node_id_to_hir_id
483495 . 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) ) )
485497 . collect ( ) ;
486498 }
487499
0 commit comments