Skip to content

Commit 8e92278

Browse files
committed
Document AstOwner.
1 parent 0e9f931 commit 8e92278

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

compiler/rustc_ast/src/ast.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
3030
use rustc_data_structures::tagged_ptr::Tag;
3131
use rustc_macros::{Decodable, Encodable, StableHash, Walkable};
3232
pub use rustc_span::AttrId;
33+
use rustc_span::def_id::LocalDefId;
3334
use rustc_span::{
3435
ByteSymbol, DUMMY_SP, ErrorGuaranteed, Ident, LocalExpnId, Span, Spanned, Symbol, kw, respan,
3536
sym,
@@ -4362,10 +4363,16 @@ impl TryFrom<ItemKind> for ForeignItemKind {
43624363

43634364
pub type ForeignItem = Item<ForeignItemKind>;
43644365

4366+
/// Fragment of the AST according to "HIR owner" semantics.
4367+
///
4368+
/// This is used to map each `LocalDefId` to its content's AST.
43654369
#[derive(Debug)]
43664370
pub enum AstOwner {
4371+
/// This definition does not correspond to a HIR owner.
43674372
NonOwner,
4368-
Synthetic(rustc_span::def_id::LocalDefId),
4373+
/// This definition corresponds to a nested `use` tree.
4374+
/// The `LocalDefId` points to its HIR owner.
4375+
NestedUseTree(LocalDefId),
43694376
Crate(Box<Crate>),
43704377
Item(Box<Item>),
43714378
TraitItem(Box<AssocItem>),

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)