3636#![ doc( rust_logo) ]
3737#![ feature( assert_matches) ]
3838#![ feature( box_patterns) ]
39+ #![ feature( closure_track_caller) ]
3940#![ feature( exact_size_is_empty) ]
4041#![ feature( if_let_guard) ]
4142#![ feature( let_chains) ]
@@ -61,7 +62,7 @@ use rustc_hir::{
6162use rustc_index:: { Idx , IndexSlice , IndexVec } ;
6263use rustc_macros:: extension;
6364use rustc_middle:: span_bug;
64- use rustc_middle:: ty:: { ResolverAstLowering , TyCtxt } ;
65+ use rustc_middle:: ty:: { PerOwnerResolverData , ResolverAstLowering , TyCtxt } ;
6566use rustc_session:: parse:: { add_feature_diagnostics, feature_err} ;
6667use rustc_span:: symbol:: { Ident , Symbol , kw, sym} ;
6768use rustc_span:: { DUMMY_SP , DesugaringKind , Span } ;
@@ -125,6 +126,7 @@ struct LoweringContext<'a, 'hir> {
125126 is_in_dyn_type : bool ,
126127
127128 current_hir_id_owner : hir:: OwnerId ,
129+ current_ast_id_owner : PerOwnerResolverData ,
128130 item_local_id_counter : hir:: ItemLocalId ,
129131 trait_map : ItemLocalMap < Box < [ TraitCandidate ] > > ,
130132
@@ -163,6 +165,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
163165 children : Vec :: default ( ) ,
164166 contract_ensures : None ,
165167 current_hir_id_owner : hir:: CRATE_OWNER_ID ,
168+ current_ast_id_owner : PerOwnerResolverData {
169+ node_id_to_def_id : Default :: default ( ) ,
170+ id : DUMMY_NODE_ID ,
171+ } ,
166172 item_local_id_counter : hir:: ItemLocalId :: ZERO ,
167173 ident_and_label_to_local_id : Default :: default ( ) ,
168174 #[ cfg( debug_assertions) ]
@@ -254,7 +260,7 @@ impl ResolverAstLowering {
254260 ///
255261 /// The extra lifetimes that appear from the parenthesized `Fn`-trait desugaring
256262 /// should appear at the enclosing `PolyTraitRef`.
257- fn extra_lifetime_params ( & mut self , id : NodeId ) -> Vec < ( Ident , NodeId , LifetimeRes ) > {
263+ fn extra_lifetime_params ( & self , id : NodeId ) -> Vec < ( Ident , NodeId , LifetimeRes ) > {
258264 self . extra_lifetime_params_map . get ( & id) . cloned ( ) . unwrap_or_default ( )
259265 }
260266}
@@ -365,17 +371,22 @@ enum AstOwner<'a> {
365371}
366372
367373fn index_crate < ' a > (
368- node_id_to_def_id : & NodeMap < LocalDefId > ,
374+ owners : & NodeMap < PerOwnerResolverData > ,
369375 krate : & ' a Crate ,
370376) -> IndexVec < LocalDefId , AstOwner < ' a > > {
371- let mut indexer = Indexer { node_id_to_def_id, index : IndexVec :: new ( ) } ;
377+ let mut indexer = Indexer {
378+ owners,
379+ node_id_to_def_id : & owners[ & CRATE_NODE_ID ] . node_id_to_def_id ,
380+ index : IndexVec :: new ( ) ,
381+ } ;
372382 * indexer. index . ensure_contains_elem ( CRATE_DEF_ID , || AstOwner :: NonOwner ) =
373383 AstOwner :: Crate ( krate) ;
374384 visit:: walk_crate ( & mut indexer, krate) ;
375385 return indexer. index ;
376386
377387 struct Indexer < ' s , ' a > {
378388 node_id_to_def_id : & ' s NodeMap < LocalDefId > ,
389+ owners : & ' s NodeMap < PerOwnerResolverData > ,
379390 index : IndexVec < LocalDefId , AstOwner < ' a > > ,
380391 }
381392
@@ -386,23 +397,38 @@ fn index_crate<'a>(
386397 }
387398
388399 fn visit_item ( & mut self , item : & ' a ast:: Item ) {
400+ let old = std:: mem:: replace (
401+ & mut self . node_id_to_def_id ,
402+ & self . owners [ & item. id ] . node_id_to_def_id ,
403+ ) ;
389404 let def_id = self . node_id_to_def_id [ & item. id ] ;
390405 * self . index . ensure_contains_elem ( def_id, || AstOwner :: NonOwner ) = AstOwner :: Item ( item) ;
391- visit:: walk_item ( self , item)
406+ visit:: walk_item ( self , item) ;
407+ self . node_id_to_def_id = old;
392408 }
393409
394410 fn visit_assoc_item ( & mut self , item : & ' a ast:: AssocItem , ctxt : visit:: AssocCtxt ) {
411+ let old = std:: mem:: replace (
412+ & mut self . node_id_to_def_id ,
413+ & self . owners [ & item. id ] . node_id_to_def_id ,
414+ ) ;
395415 let def_id = self . node_id_to_def_id [ & item. id ] ;
396416 * self . index . ensure_contains_elem ( def_id, || AstOwner :: NonOwner ) =
397417 AstOwner :: AssocItem ( item, ctxt) ;
398418 visit:: walk_assoc_item ( self , item, ctxt) ;
419+ self . node_id_to_def_id = old;
399420 }
400421
401422 fn visit_foreign_item ( & mut self , item : & ' a ast:: ForeignItem ) {
423+ let old = std:: mem:: replace (
424+ & mut self . node_id_to_def_id ,
425+ & self . owners [ & item. id ] . node_id_to_def_id ,
426+ ) ;
402427 let def_id = self . node_id_to_def_id [ & item. id ] ;
403428 * self . index . ensure_contains_elem ( def_id, || AstOwner :: NonOwner ) =
404429 AstOwner :: ForeignItem ( item) ;
405430 visit:: walk_item ( self , item) ;
431+ self . node_id_to_def_id = old;
406432 }
407433 }
408434}
@@ -439,7 +465,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
439465 tcx. ensure_done ( ) . get_lang_items ( ( ) ) ;
440466 let ( mut resolver, krate) = tcx. resolver_for_lowering ( ) . steal ( ) ;
441467
442- let ast_index = index_crate ( & resolver. node_id_to_def_id , & krate) ;
468+ let ast_index = index_crate ( & resolver. owners , & krate) ;
443469 let mut owners = IndexVec :: from_fn_n (
444470 |_| hir:: MaybeOwner :: Phantom ,
445471 tcx. definitions_untracked ( ) . def_index_count ( ) ,
@@ -495,7 +521,6 @@ enum GenericArgsMode {
495521impl < ' a , ' hir > LoweringContext < ' a , ' hir > {
496522 fn create_def (
497523 & mut self ,
498- parent : LocalDefId ,
499524 node_id : ast:: NodeId ,
500525 name : Option < Symbol > ,
501526 def_kind : DefKind ,
@@ -510,10 +535,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
510535 self . tcx. hir_def_key( self . local_def_id( node_id) ) ,
511536 ) ;
512537
513- let def_id = self . tcx . at ( span) . create_def ( parent, name, def_kind) . def_id ( ) ;
538+ let def_id =
539+ self . tcx . at ( span) . create_def ( self . current_hir_id_owner . def_id , name, def_kind) . def_id ( ) ;
514540
515541 debug ! ( "create_def: def_id_to_node_id[{:?}] <-> {:?}" , def_id, node_id) ;
516- self . resolver . node_id_to_def_id . insert ( node_id, def_id) ;
542+ self . current_ast_id_owner . node_id_to_def_id . insert ( node_id, def_id) ;
517543
518544 def_id
519545 }
@@ -528,16 +554,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
528554 /// Given the id of some node in the AST, finds the `LocalDefId` associated with it by the name
529555 /// resolver (if any).
530556 fn opt_local_def_id ( & self , node : NodeId ) -> Option < LocalDefId > {
531- self . resolver . node_id_to_def_id . get ( & node) . copied ( )
557+ self . current_ast_id_owner . node_id_to_def_id . get ( & node) . copied ( )
532558 }
533559
534560 fn local_def_id ( & self , node : NodeId ) -> LocalDefId {
535- self . opt_local_def_id ( node) . unwrap_or_else ( || panic ! ( "no entry for node id: `{node:?}`" ) )
561+ self . opt_local_def_id ( node) . unwrap_or_else ( || {
562+ self . resolver . owners . items ( ) . any ( |( id, items) | {
563+ items. node_id_to_def_id . items ( ) . any ( |( node_id, def_id) | {
564+ if * node_id == node {
565+ panic ! (
566+ "{def_id:?} ({node_id}) was found in {:?} ({id})" ,
567+ items. node_id_to_def_id. get( id) ,
568+ )
569+ }
570+ false
571+ } )
572+ } ) ;
573+ panic ! ( "no entry for node id: `{node:?}`" ) ;
574+ } )
536575 }
537576
538577 /// Given the id of an owner node in the AST, returns the corresponding `OwnerId`.
539578 fn owner_id ( & self , node : NodeId ) -> hir:: OwnerId {
540- hir:: OwnerId { def_id : self . local_def_id ( node) }
579+ hir:: OwnerId { def_id : self . resolver . owners [ & node] . node_id_to_def_id [ & node ] }
541580 }
542581
543582 /// Freshen the `LoweringContext` and ready it to lower a nested item.
@@ -556,6 +595,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
556595 let current_attrs = std:: mem:: take ( & mut self . attrs ) ;
557596 let current_bodies = std:: mem:: take ( & mut self . bodies ) ;
558597 let current_define_opaque = std:: mem:: take ( & mut self . define_opaque ) ;
598+ let current_ast_owner = std:: mem:: replace (
599+ & mut self . current_ast_id_owner ,
600+ self . resolver . owners . remove ( & owner) . unwrap ( ) ,
601+ ) ;
559602 let current_ident_and_label_to_local_id =
560603 std:: mem:: take ( & mut self . ident_and_label_to_local_id ) ;
561604
@@ -600,6 +643,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
600643 self . item_local_id_counter = current_local_counter;
601644 self . impl_trait_defs = current_impl_trait_defs;
602645 self . impl_trait_bounds = current_impl_trait_bounds;
646+ self . current_ast_id_owner = current_ast_owner;
603647
604648 debug_assert ! ( !self . children. iter( ) . any( |( id, _) | id == & owner_id. def_id) ) ;
605649 self . children . push ( ( owner_id. def_id , hir:: MaybeOwner :: Owner ( info) ) ) ;
@@ -782,7 +826,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
782826 LifetimeRes :: Fresh { param, kind, .. } => {
783827 // Late resolution delegates to us the creation of the `LocalDefId`.
784828 let _def_id = self . create_def (
785- self . current_hir_id_owner . def_id ,
786829 param,
787830 Some ( kw:: UnderscoreLifetime ) ,
788831 DefKind :: LifetimeParam ,
@@ -2089,15 +2132,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20892132 } else {
20902133 // Construct an AnonConst where the expr is the "ty"'s path.
20912134
2092- let parent_def_id = self . current_hir_id_owner . def_id ;
20932135 let node_id = self . next_node_id ( ) ;
20942136 let span = self . lower_span ( span) ;
20952137
20962138 // Add a definition for the in-band const def.
20972139 // We're lowering a const argument that was originally thought to be a type argument,
20982140 // so the def collector didn't create the def ahead of time. That's why we have to do
20992141 // it here.
2100- let def_id = self . create_def ( parent_def_id , node_id, None , DefKind :: AnonConst , span) ;
2142+ let def_id = self . create_def ( node_id, None , DefKind :: AnonConst , span) ;
21012143 let hir_id = self . lower_node_id ( node_id) ;
21022144
21032145 let path_expr = Expr {
0 commit comments