@@ -546,7 +546,7 @@ fn index_ast<'tcx>(
546546 UseTreeKind :: Glob ( _) | UseTreeKind :: Simple ( _) => { }
547547 UseTreeKind :: Nested { items : ref nested_vec, span } => {
548548 for & ( ref nested, id) in nested_vec {
549- self . insert ( id, AstOwner :: Synthetic ( parent) ) ;
549+ self . insert ( id, AstOwner :: NestedUseTree ( parent) ) ;
550550 items. push ( self . make_dummy ( id, span, ItemKind :: MacCall ) ) ;
551551
552552 let def_id = self . owners [ & id] . def_id ;
@@ -624,10 +624,17 @@ fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_> {
624624 let ast_index = tcx. index_ast ( ( ) ) ;
625625 let resolver_and_node = ast_index. get ( def_id) . map ( Steal :: steal) ;
626626
627- let fallback_to_parent = |parent_id| {
628- // The item did not exist in the AST, it was created by its parent.
627+ let fallback_to_ancestor = |parent_id| {
628+ // The item did not exist in the AST, it was created while lowering another item.
629+ // `parent_id` may be different from the direct parent of `def_id`,
630+ // for instance use-trees are lowered by the first sibling.
629631 let mut parent_info = tcx. lower_to_hir ( parent_id) ;
630632 if let hir:: MaybeOwner :: NonOwner ( hir_id) = parent_info {
633+ // `parent_id` could also not be a owner either.
634+ // For instance if `def_id` is an enum variant field,
635+ // the direct parent is the enum variant.
636+ // In that case `hir_id.owner` point to the actual HIR owner
637+ // and skips all non-owner parents, so fetch the HIR associated to it.
631638 parent_info = tcx. lower_to_hir ( hir_id. owner ) ;
632639 }
633640
@@ -642,7 +649,10 @@ fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_> {
642649 } ;
643650
644651 let Some ( ( resolver, node) ) = resolver_and_node else {
645- return fallback_to_parent ( tcx. local_parent ( def_id) ) ;
652+ // `ast_index` does not contain all definitions, only up-to the highest
653+ // `LocalDefId` which has a non-trivial `AstOwner`. Gracefully handle
654+ // other definitions, in particular those nested inside this highest definition.
655+ return fallback_to_ancestor ( tcx. local_parent ( def_id) ) ;
646656 } ;
647657
648658 let mut item_lowerer = item:: ItemLowerer { tcx, resolver : & * resolver } ;
@@ -654,8 +664,10 @@ fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_> {
654664 AstOwner :: TraitItem ( item) => item_lowerer. lower_trait_item ( & item) ,
655665 AstOwner :: ImplItem ( item) => item_lowerer. lower_impl_item ( & item) ,
656666 AstOwner :: ForeignItem ( item) => item_lowerer. lower_foreign_item ( & item) ,
657- AstOwner :: Synthetic ( parent_id) => fallback_to_parent ( * parent_id) ,
658- AstOwner :: NonOwner => fallback_to_parent ( tcx. local_parent ( def_id) ) ,
667+ AstOwner :: NestedUseTree ( owner_id) => fallback_to_ancestor ( * owner_id) ,
668+ // The item existed in the AST, but is not a HIR owner.
669+ // Fetch the correct information from its parent.
670+ AstOwner :: NonOwner => fallback_to_ancestor ( tcx. local_parent ( def_id) ) ,
659671 } ;
660672
661673 tcx. sess . time ( "drop_ast" , || std:: mem:: drop ( node) ) ;
0 commit comments