@@ -61,7 +61,7 @@ use rustc_index::{Idx, IndexSlice, IndexVec};
6161use rustc_macros:: extension;
6262use rustc_middle:: hir:: { self as mid_hir} ;
6363use rustc_middle:: span_bug;
64- use rustc_middle:: ty:: { DelegationInfo , ResolverAstLowering , TyCtxt } ;
64+ use rustc_middle:: ty:: { DelegationInfo , PerOwnerResolverData , ResolverAstLowering , TyCtxt } ;
6565use rustc_session:: errors:: add_feature_diagnostics;
6666use rustc_span:: symbol:: { Ident , Symbol , kw, sym} ;
6767use rustc_span:: { DUMMY_SP , DesugaringKind , Span } ;
@@ -126,6 +126,7 @@ struct LoweringContext<'a, 'hir> {
126126 is_in_dyn_type : bool ,
127127
128128 current_hir_id_owner : hir:: OwnerId ,
129+ owner : & ' a PerOwnerResolverData ,
129130 item_local_id_counter : hir:: ItemLocalId ,
130131 trait_map : ItemLocalMap < & ' hir [ TraitCandidate < ' hir > ] > ,
131132
@@ -173,6 +174,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
173174 tcx,
174175 resolver,
175176 current_disambiguator : Default :: default ( ) ,
177+ owner : & resolver. owners [ & CRATE_NODE_ID ] ,
176178 arena : tcx. hir_arena ,
177179
178180 // HirId handling.
@@ -316,12 +318,8 @@ impl<'tcx> ResolverAstLowering<'tcx> {
316318 self . delegation_infos . get ( & id)
317319 }
318320
319- fn opt_local_def_id ( & self , id : NodeId ) -> Option < LocalDefId > {
320- self . node_id_to_def_id . get ( & id) . copied ( )
321- }
322-
323- fn local_def_id ( & self , id : NodeId ) -> LocalDefId {
324- self . opt_local_def_id ( id) . expect ( "must have def_id" )
321+ fn owner_def_id ( & self , id : NodeId ) -> LocalDefId {
322+ self . owners [ & id] . def_id
325323 }
326324
327325 fn lifetime_elision_allowed ( & self , id : NodeId ) -> bool {
@@ -489,20 +487,20 @@ fn index_crate<'a, 'b>(
489487 }
490488
491489 fn visit_item ( & mut self , item : & ' a ast:: Item ) {
492- let def_id = self . resolver . local_def_id ( item. id ) ;
490+ let def_id = self . resolver . owner_def_id ( item. id ) ;
493491 * self . index . ensure_contains_elem ( def_id, || AstOwner :: NonOwner ) = AstOwner :: Item ( item) ;
494492 visit:: walk_item ( self , item)
495493 }
496494
497495 fn visit_assoc_item ( & mut self , item : & ' a ast:: AssocItem , ctxt : visit:: AssocCtxt ) {
498- let def_id = self . resolver . local_def_id ( item. id ) ;
496+ let def_id = self . resolver . owner_def_id ( item. id ) ;
499497 * self . index . ensure_contains_elem ( def_id, || AstOwner :: NonOwner ) =
500498 AstOwner :: AssocItem ( item, ctxt) ;
501499 visit:: walk_assoc_item ( self , item, ctxt) ;
502500 }
503501
504502 fn visit_foreign_item ( & mut self , item : & ' a ast:: ForeignItem ) {
505- let def_id = self . resolver . local_def_id ( item. id ) ;
503+ let def_id = self . resolver . owner_def_id ( item. id ) ;
506504 * self . index . ensure_contains_elem ( def_id, || AstOwner :: NonOwner ) =
507505 AstOwner :: ForeignItem ( item) ;
508506 visit:: walk_item ( self , item) ;
@@ -668,12 +666,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
668666 fn opt_local_def_id ( & self , node : NodeId ) -> Option < LocalDefId > {
669667 self . node_id_to_def_id
670668 . get ( & node)
671- . or_else ( || self . resolver . node_id_to_def_id . get ( & node) )
669+ . or_else ( || self . owner . node_id_to_def_id . get ( & node) )
672670 . copied ( )
673671 }
674672
675673 fn local_def_id ( & self , node : NodeId ) -> LocalDefId {
676- self . opt_local_def_id ( node) . unwrap_or_else ( || panic ! ( "no entry for node id: `{node:?}`" ) )
674+ self . opt_local_def_id ( node) . unwrap_or_else ( || {
675+ self . resolver . owners . items ( ) . any ( |( id, items) | {
676+ items. node_id_to_def_id . items ( ) . any ( |( node_id, def_id) | {
677+ if * node_id == node {
678+ let actual_owner = items. node_id_to_def_id . get ( id) ;
679+ panic ! ( "{def_id:?} ({node_id}) was found in {actual_owner:?} ({id})" , )
680+ }
681+ false
682+ } )
683+ } ) ;
684+ panic ! ( "no entry for node id: `{node:?}`" ) ;
685+ } )
677686 }
678687
679688 fn get_partial_res ( & self , id : NodeId ) -> Option < PartialRes > {
@@ -685,7 +694,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
685694
686695 /// Given the id of an owner node in the AST, returns the corresponding `OwnerId`.
687696 fn owner_id ( & self , node : NodeId ) -> hir:: OwnerId {
688- hir:: OwnerId { def_id : self . local_def_id ( node) }
697+ hir:: OwnerId { def_id : self . resolver . owners [ & node] . def_id }
689698 }
690699
691700 /// Freshen the `LoweringContext` and ready it to lower a nested item.
@@ -710,6 +719,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
710719 . unwrap_or_else ( || PerParentDisambiguatorState :: new ( def_id) ) ;
711720
712721 let disambiguator = std:: mem:: replace ( & mut self . current_disambiguator , new_disambig) ;
722+ let current_ast_owner = std:: mem:: replace ( & mut self . owner , & self . resolver . owners [ & owner] ) ;
713723 let current_attrs = std:: mem:: take ( & mut self . attrs ) ;
714724 let current_bodies = std:: mem:: take ( & mut self . bodies ) ;
715725 let current_define_opaque = std:: mem:: take ( & mut self . define_opaque ) ;
@@ -745,6 +755,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
745755 let info = self . make_owner_info ( item) ;
746756
747757 self . current_disambiguator = disambiguator;
758+ self . owner = current_ast_owner;
748759 self . attrs = current_attrs;
749760 self . bodies = current_bodies;
750761 self . define_opaque = current_define_opaque;
@@ -1793,27 +1804,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
17931804
17941805 let output = match coro {
17951806 Some ( coro) => {
1796- let fn_def_id = self . local_def_id ( fn_node_id ) ;
1807+ let fn_def_id = self . owner . def_id ;
17971808 self . lower_coroutine_fn_ret_ty ( & decl. output , fn_def_id, coro, kind)
17981809 }
17991810 None => match & decl. output {
18001811 FnRetTy :: Ty ( ty) => {
18011812 let itctx = match kind {
18021813 FnDeclKind :: Fn | FnDeclKind :: Inherent => ImplTraitContext :: OpaqueTy {
18031814 origin : hir:: OpaqueTyOrigin :: FnReturn {
1804- parent : self . local_def_id ( fn_node_id ) ,
1815+ parent : self . owner . def_id ,
18051816 in_trait_or_impl : None ,
18061817 } ,
18071818 } ,
18081819 FnDeclKind :: Trait => ImplTraitContext :: OpaqueTy {
18091820 origin : hir:: OpaqueTyOrigin :: FnReturn {
1810- parent : self . local_def_id ( fn_node_id ) ,
1821+ parent : self . owner . def_id ,
18111822 in_trait_or_impl : Some ( hir:: RpitContext :: Trait ) ,
18121823 } ,
18131824 } ,
18141825 FnDeclKind :: Impl => ImplTraitContext :: OpaqueTy {
18151826 origin : hir:: OpaqueTyOrigin :: FnReturn {
1816- parent : self . local_def_id ( fn_node_id ) ,
1827+ parent : self . owner . def_id ,
18171828 in_trait_or_impl : Some ( hir:: RpitContext :: TraitImpl ) ,
18181829 } ,
18191830 } ,
0 commit comments