Skip to content

Commit 3757104

Browse files
authored
Rollup merge of #138895 - oli-obk:dedup-owner-id-creation, r=compiler-errors
Add a helper for building an owner id in ast lowering Just some deduplication of owner-id creations. Will also help me later split up ast lowering into per-owner queries, as it won't be possible anymore to go from a NodeId to a DefId of an owner without doing extra work to check whether we have an owner id. So I'd just do that in the new `owner_id` function and keep the `local_def_id` function free of that logic
2 parents b2629be + 67e0b89 commit 3757104

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

compiler/rustc_ast_lowering/src/item.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
132132
}
133133

134134
pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
135-
let mut node_ids =
136-
smallvec![hir::ItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } }];
135+
let mut node_ids = smallvec![hir::ItemId { owner_id: self.owner_id(i.id) }];
137136
if let ItemKind::Use(use_tree) = &i.kind {
138137
self.lower_item_id_use_tree(use_tree, &mut node_ids);
139138
}
@@ -144,9 +143,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
144143
match &tree.kind {
145144
UseTreeKind::Nested { items, .. } => {
146145
for &(ref nested, id) in items {
147-
vec.push(hir::ItemId {
148-
owner_id: hir::OwnerId { def_id: self.local_def_id(id) },
149-
});
146+
vec.push(hir::ItemId { owner_id: self.owner_id(id) });
150147
self.lower_item_id_use_tree(nested, vec);
151148
}
152149
}
@@ -585,7 +582,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
585582

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

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

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

711708
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef {
712709
hir::ForeignItemRef {
713-
id: hir::ForeignItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } },
710+
id: hir::ForeignItemId { owner_id: self.owner_id(i.id) },
714711
ident: self.lower_ident(i.ident),
715712
span: self.lower_span(i.span),
716713
}
@@ -931,7 +928,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
931928
panic!("macros should have been expanded by now")
932929
}
933930
};
934-
let id = hir::TraitItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } };
931+
let id = hir::TraitItemId { owner_id: self.owner_id(i.id) };
935932
hir::TraitItemRef {
936933
id,
937934
ident: self.lower_ident(i.ident),
@@ -1046,7 +1043,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10461043

10471044
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
10481045
hir::ImplItemRef {
1049-
id: hir::ImplItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } },
1046+
id: hir::ImplItemId { owner_id: self.owner_id(i.id) },
10501047
ident: self.lower_ident(i.ident),
10511048
span: self.lower_span(i.span),
10521049
kind: match &i.kind {

compiler/rustc_ast_lowering/src/lib.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
536536
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{node:?}`"))
537537
}
538538

539+
/// Given the id of an owner node in the AST, returns the corresponding `OwnerId`.
540+
fn owner_id(&self, node: NodeId) -> hir::OwnerId {
541+
hir::OwnerId { def_id: self.local_def_id(node) }
542+
}
543+
539544
/// Freshen the `LoweringContext` and ready it to lower a nested item.
540545
/// The lowered item is registered into `self.children`.
541546
///
@@ -547,7 +552,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
547552
owner: NodeId,
548553
f: impl FnOnce(&mut Self) -> hir::OwnerNode<'hir>,
549554
) {
550-
let def_id = self.local_def_id(owner);
555+
let owner_id = self.owner_id(owner);
551556

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

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

601-
debug_assert!(!self.children.iter().any(|(id, _)| id == &def_id));
602-
self.children.push((def_id, hir::MaybeOwner::Owner(info)));
605+
debug_assert!(!self.children.iter().any(|(id, _)| id == &owner_id.def_id));
606+
self.children.push((owner_id.def_id, hir::MaybeOwner::Owner(info)));
603607
}
604608

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

0 commit comments

Comments
 (0)