Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a helper for building an owner id in ast lowering #138895

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
@@ -132,8 +132,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
let mut node_ids =
smallvec![hir::ItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } }];
let mut node_ids = smallvec![hir::ItemId { owner_id: self.owner_id(i.id) }];
if let ItemKind::Use(use_tree) = &i.kind {
self.lower_item_id_use_tree(use_tree, &mut node_ids);
}
@@ -144,9 +143,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
match &tree.kind {
UseTreeKind::Nested { items, .. } => {
for &(ref nested, id) in items {
vec.push(hir::ItemId {
owner_id: hir::OwnerId { def_id: self.local_def_id(id) },
});
vec.push(hir::ItemId { owner_id: self.owner_id(id) });
self.lower_item_id_use_tree(nested, vec);
}
}
@@ -585,7 +582,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

// Add all the nested `PathListItem`s to the HIR.
for &(ref use_tree, id) in trees {
let new_hir_id = self.local_def_id(id);
let owner_id = self.owner_id(id);

// Each `use` import is an item and thus are owners of the
// names in the path. Up to this point the nested import is
@@ -602,7 +599,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

let item = hir::Item {
owner_id: hir::OwnerId { def_id: new_hir_id },
owner_id,
kind,
vis_span,
span: this.lower_span(use_tree.span),
@@ -710,7 +707,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef {
hir::ForeignItemRef {
id: hir::ForeignItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } },
id: hir::ForeignItemId { owner_id: self.owner_id(i.id) },
ident: self.lower_ident(i.ident),
span: self.lower_span(i.span),
}
@@ -931,7 +928,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
panic!("macros should have been expanded by now")
}
};
let id = hir::TraitItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } };
let id = hir::TraitItemId { owner_id: self.owner_id(i.id) };
hir::TraitItemRef {
id,
ident: self.lower_ident(i.ident),
@@ -1046,7 +1043,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
hir::ImplItemRef {
id: hir::ImplItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } },
id: hir::ImplItemId { owner_id: self.owner_id(i.id) },
ident: self.lower_ident(i.ident),
span: self.lower_span(i.span),
kind: match &i.kind {
16 changes: 10 additions & 6 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
@@ -536,6 +536,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{node:?}`"))
}

/// Given the id of an owner node in the AST, returns the corresponding `OwnerId`.
fn owner_id(&self, node: NodeId) -> hir::OwnerId {
hir::OwnerId { def_id: self.local_def_id(node) }
}

/// Freshen the `LoweringContext` and ready it to lower a nested item.
/// The lowered item is registered into `self.children`.
///
@@ -547,7 +552,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
owner: NodeId,
f: impl FnOnce(&mut Self) -> hir::OwnerNode<'hir>,
) {
let def_id = self.local_def_id(owner);
let owner_id = self.owner_id(owner);

let current_attrs = std::mem::take(&mut self.attrs);
let current_bodies = std::mem::take(&mut self.bodies);
@@ -558,8 +563,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
#[cfg(debug_assertions)]
let current_node_id_to_local_id = std::mem::take(&mut self.node_id_to_local_id);
let current_trait_map = std::mem::take(&mut self.trait_map);
let current_owner =
std::mem::replace(&mut self.current_hir_id_owner, hir::OwnerId { def_id });
let current_owner = std::mem::replace(&mut self.current_hir_id_owner, owner_id);
let current_local_counter =
std::mem::replace(&mut self.item_local_id_counter, hir::ItemLocalId::new(1));
let current_impl_trait_defs = std::mem::take(&mut self.impl_trait_defs);
@@ -577,7 +581,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}

let item = f(self);
debug_assert_eq!(def_id, item.def_id().def_id);
debug_assert_eq!(owner_id, item.def_id());
// `f` should have consumed all the elements in these vectors when constructing `item`.
debug_assert!(self.impl_trait_defs.is_empty());
debug_assert!(self.impl_trait_bounds.is_empty());
@@ -598,8 +602,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.impl_trait_defs = current_impl_trait_defs;
self.impl_trait_bounds = current_impl_trait_bounds;

debug_assert!(!self.children.iter().any(|(id, _)| id == &def_id));
self.children.push((def_id, hir::MaybeOwner::Owner(info)));
debug_assert!(!self.children.iter().any(|(id, _)| id == &owner_id.def_id));
self.children.push((owner_id.def_id, hir::MaybeOwner::Owner(info)));
}

fn make_owner_info(&mut self, node: hir::OwnerNode<'hir>) -> &'hir hir::OwnerInfo<'hir> {
Loading