Skip to content

Commit 3a2ae6a

Browse files
committed
Stop putting owner's DefId into their own node_id_to_def_id map
Now they unfortunately land in their parent, which is not necessary
1 parent adb2b0f commit 3a2ae6a

12 files changed

Lines changed: 104 additions & 99 deletions

File tree

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
122122
let span = self.lower_span(delegation.path.segments.last().unwrap().ident.span);
123123

124124
// Delegation can be unresolved in illegal places such as function bodies in extern blocks (see #151356)
125-
let sig_id = if let Some(delegation_info) =
126-
self.resolver.delegation_info(self.local_def_id(item_id))
125+
let sig_id = if let Some(delegation_info) = self.resolver.delegation_info(self.owner.def_id)
127126
{
128127
self.get_sig_id(delegation_info.resolution_node, span)
129128
} else {
@@ -143,8 +142,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
143142

144143
let (param_count, c_variadic) = self.param_count(sig_id);
145144

146-
let mut generics =
147-
self.uplift_delegation_generics(delegation, sig_id, item_id, is_method);
145+
let mut generics = self.uplift_delegation_generics(delegation, sig_id, is_method);
148146

149147
let body_id = self.lower_delegation_body(
150148
delegation,

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
231231
&mut self,
232232
delegation: &Delegation,
233233
sig_id: DefId,
234-
item_id: NodeId,
235234
is_method: bool,
236235
) -> GenericsGenerationResults<'hir> {
237-
let delegation_parent_kind =
238-
self.tcx.def_kind(self.tcx.local_parent(self.local_def_id(item_id)));
236+
let delegation_parent_kind = self.tcx.def_kind(self.tcx.local_parent(self.owner.def_id));
239237

240238
let segments = &delegation.path.segments;
241239
let len = segments.len();

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
445445
ty,
446446
ImplTraitContext::OpaqueTy {
447447
origin: hir::OpaqueTyOrigin::TyAlias {
448-
parent: this.local_def_id(id),
448+
parent: this.owner.def_id,
449449
in_assoc_ty: false,
450450
},
451451
},
@@ -598,7 +598,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
598598
ItemKind::MacroDef(ident, MacroDef { body, macro_rules, eii_declaration: _ }) => {
599599
let ident = self.lower_ident(*ident);
600600
let body = Box::new(self.lower_delim_args(body));
601-
let def_id = self.local_def_id(id);
601+
let def_id = self.owner.def_id;
602602
let def_kind = self.tcx.def_kind(def_id);
603603
let DefKind::Macro(macro_kinds) = def_kind else {
604604
unreachable!(
@@ -1282,7 +1282,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12821282
ty,
12831283
ImplTraitContext::OpaqueTy {
12841284
origin: hir::OpaqueTyOrigin::TyAlias {
1285-
parent: this.local_def_id(i.id),
1285+
parent: this.owner.def_id,
12861286
in_assoc_ty: true,
12871287
},
12881288
},

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl<'tcx> ResolverAstLowering<'tcx> {
319319
}
320320

321321
fn owner_def_id(&self, id: NodeId) -> LocalDefId {
322-
self.owners[&id].node_id_to_def_id[&id]
322+
self.owners[&id].def_id
323323
}
324324

325325
fn lifetime_elision_allowed(&self, id: NodeId) -> bool {
@@ -694,7 +694,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
694694

695695
/// Given the id of an owner node in the AST, returns the corresponding `OwnerId`.
696696
fn owner_id(&self, node: NodeId) -> hir::OwnerId {
697-
hir::OwnerId { def_id: self.resolver.owners[&node].node_id_to_def_id[&node] }
697+
hir::OwnerId { def_id: self.resolver.owners[&node].def_id }
698698
}
699699

700700
/// Freshen the `LoweringContext` and ready it to lower a nested item.
@@ -1804,27 +1804,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
18041804

18051805
let output = match coro {
18061806
Some(coro) => {
1807-
let fn_def_id = self.local_def_id(fn_node_id);
1807+
let fn_def_id = self.owner.def_id;
18081808
self.lower_coroutine_fn_ret_ty(&decl.output, fn_def_id, coro, kind)
18091809
}
18101810
None => match &decl.output {
18111811
FnRetTy::Ty(ty) => {
18121812
let itctx = match kind {
18131813
FnDeclKind::Fn | FnDeclKind::Inherent => ImplTraitContext::OpaqueTy {
18141814
origin: hir::OpaqueTyOrigin::FnReturn {
1815-
parent: self.local_def_id(fn_node_id),
1815+
parent: self.owner.def_id,
18161816
in_trait_or_impl: None,
18171817
},
18181818
},
18191819
FnDeclKind::Trait => ImplTraitContext::OpaqueTy {
18201820
origin: hir::OpaqueTyOrigin::FnReturn {
1821-
parent: self.local_def_id(fn_node_id),
1821+
parent: self.owner.def_id,
18221822
in_trait_or_impl: Some(hir::RpitContext::Trait),
18231823
},
18241824
},
18251825
FnDeclKind::Impl => ImplTraitContext::OpaqueTy {
18261826
origin: hir::OpaqueTyOrigin::FnReturn {
1827-
parent: self.local_def_id(fn_node_id),
1827+
parent: self.owner.def_id,
18281828
in_trait_or_impl: Some(hir::RpitContext::TraitImpl),
18291829
},
18301830
},

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,13 @@ pub struct PerOwnerResolverData {
203203
pub node_id_to_def_id: NodeMap<LocalDefId>,
204204
/// The id of the owner
205205
pub id: ast::NodeId,
206+
/// The `DefId` of the owner, can't be found in `node_id_to_def_id`.
207+
pub def_id: LocalDefId,
206208
}
207209

208210
impl PerOwnerResolverData {
209-
pub fn new(id: ast::NodeId) -> PerOwnerResolverData {
210-
PerOwnerResolverData { node_id_to_def_id: Default::default(), id }
211+
pub fn new(id: ast::NodeId, def_id: LocalDefId) -> PerOwnerResolverData {
212+
PerOwnerResolverData { node_id_to_def_id: Default::default(), id, def_id }
211213
}
212214
}
213215

compiler/rustc_passes/src/lang_items.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl<'ast, 'tcx> visit::Visitor<'ast> for LanguageItemCollector<'ast, 'tcx> {
295295

296296
self.check_for_lang(
297297
target,
298-
self.resolver.owners[&i.id].node_id_to_def_id[&i.id],
298+
self.resolver.owners[&i.id].def_id,
299299
&i.attrs,
300300
i.span,
301301
i.opt_generics(),
@@ -346,13 +346,7 @@ impl<'ast, 'tcx> visit::Visitor<'ast> for LanguageItemCollector<'ast, 'tcx> {
346346
}
347347
};
348348

349-
self.check_for_lang(
350-
target,
351-
self.resolver.owners[&i.id].node_id_to_def_id[&i.id],
352-
&i.attrs,
353-
i.span,
354-
generics,
355-
);
349+
self.check_for_lang(target, self.resolver.owners[&i.id].def_id, &i.attrs, i.span, generics);
356350

357351
visit::walk_assoc_item(self, i, ctxt);
358352
}

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
743743
}
744744
ast::UseTreeKind::Nested { ref items, .. } => {
745745
for &(ref tree, id) in items {
746-
self.with_owner(id, |this| {
747-
let feed = this.create_def(id, None, DefKind::Use, use_tree.span());
746+
self.with_owner(id, None, DefKind::Use, use_tree.span(), |this, feed| {
748747
this.build_reduced_graph_for_use_tree(
749748
// This particular use tree
750749
tree, id, &prefix, true, false, // The whole `use` item

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub(crate) fn collect_definitions<'ra>(
3232
with_owner(resolver, invocation_parent.owner, |r| {
3333
let mut visitor = DefCollector { r, invocation_parent, parent_scope };
3434
fragment.visit_with(&mut visitor);
35-
3635
visitor.parent_scope.macro_rules
3736
})
3837
}
@@ -64,6 +63,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
6463
def_kind,
6564
self.parent_scope.expansion.to_expn_id(),
6665
span.with_parent(None),
66+
false,
6767
)
6868
}
6969

@@ -73,23 +73,33 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
7373
self.invocation_parent.parent_def = orig_parent_def;
7474
}
7575

76-
pub(super) fn with_owner<F: FnOnce(&mut Self)>(&mut self, owner: NodeId, f: F) {
76+
pub(super) fn with_owner<F: FnOnce(&mut Self, TyCtxtFeed<'tcx, LocalDefId>)>(
77+
&mut self,
78+
owner: NodeId,
79+
name: Option<Symbol>,
80+
def_kind: DefKind,
81+
span: Span,
82+
f: F,
83+
) {
7784
debug_assert_ne!(owner, DUMMY_NODE_ID);
78-
if owner == CRATE_NODE_ID {
79-
// Special case: we always have an invocation parent (set in `collect_definitions`)
80-
// of at least the crate root, even for visiting the crate root,
81-
// which would then remove the crate root from the tables
82-
// list twice and try to insert it twice afterwards.
83-
debug_assert_eq!(self.r.current_owner.id, CRATE_NODE_ID);
84-
return f(self);
85-
}
85+
debug_assert_ne!(owner, CRATE_NODE_ID);
8686
// We only get here if the owner didn't exist yet. After the owner has been created,
8787
// future invocations of `collect_definitions` will get the owner out of the `owners`
8888
// table.
89-
let tables = PerOwnerResolverData::new(owner);
89+
let parent_def = self.invocation_parent.parent_def;
90+
let feed = self.r.create_def(
91+
parent_def,
92+
owner,
93+
name,
94+
def_kind,
95+
self.parent_scope.expansion.to_expn_id(),
96+
span.with_parent(None),
97+
true,
98+
);
99+
let tables = PerOwnerResolverData::new(owner, feed.key());
90100

91101
let orig_invoc_owner = mem::replace(&mut self.invocation_parent.owner, owner);
92-
with_owner_tables(self, owner, tables, f);
102+
with_owner_tables(self, owner, tables, |this| f(this, feed));
93103
let old = mem::replace(&mut self.invocation_parent.owner, orig_invoc_owner);
94104
assert_eq!(old, owner);
95105
}
@@ -200,8 +210,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
200210
}
201211
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
202212
ItemKind::Use(_) => {
203-
return self.with_owner(i.id, |this| {
204-
let feed = this.create_def(i.id, None, DefKind::Use, i.span);
213+
return self.with_owner(i.id, None, DefKind::Use, i.span, |this, feed| {
205214
this.brg_visit_item(i, feed);
206215
});
207216
}
@@ -212,20 +221,23 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
212221
}
213222
ItemKind::DelegationMac(..) => unreachable!(),
214223
};
215-
self.with_owner(i.id, |this| {
216-
let feed =
217-
this.create_def(i.id, i.kind.ident().map(|ident| ident.name), def_kind, i.span);
218-
219-
if let Some(ext) = opt_syn_ext {
220-
this.r.local_macro_map.insert(feed.def_id(), self.r.arenas.alloc_macro(ext));
221-
}
224+
self.with_owner(
225+
i.id,
226+
i.kind.ident().map(|ident| ident.name),
227+
def_kind,
228+
i.span,
229+
|this, feed| {
230+
if let Some(ext) = opt_syn_ext {
231+
this.r.local_macro_map.insert(feed.def_id(), self.r.arenas.alloc_macro(ext));
232+
}
222233

223-
this.with_parent(feed.def_id(), |this| {
224-
this.with_impl_trait(ImplTraitContext::Existential, |this| {
225-
this.brg_visit_item(i, feed)
226-
})
227-
});
228-
});
234+
this.with_parent(feed.def_id(), |this| {
235+
this.with_impl_trait(ImplTraitContext::Existential, |this| {
236+
this.brg_visit_item(i, feed)
237+
})
238+
});
239+
},
240+
);
229241
}
230242

231243
fn visit_block(&mut self, block: &'a Block) {
@@ -316,9 +328,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
316328
}
317329
};
318330

319-
self.with_owner(fi.id, |this| {
320-
let def = this.create_def(fi.id, Some(ident.name), def_kind, fi.span);
321-
331+
self.with_owner(fi.id, Some(ident.name), def_kind, fi.span, |this, def| {
322332
this.with_parent(def.def_id(), |this| {
323333
this.build_reduced_graph_for_foreign_item(fi, ident, def);
324334
visit::walk_item(this, fi)
@@ -402,8 +412,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
402412
}
403413
};
404414

405-
self.with_owner(i.id, |this| {
406-
let feed = this.create_def(i.id, Some(ident.name), def_kind, i.span);
415+
self.with_owner(i.id, Some(ident.name), def_kind, i.span, |this, feed| {
407416
this.with_parent(feed.def_id(), |this| {
408417
this.brg_visit_assoc_item(i, ctxt, ident, ns, feed)
409418
});
@@ -604,11 +613,16 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
604613
} else {
605614
// Visit attributes after items for backward compatibility.
606615
// This way they can use `macro_rules` defined later.
607-
self.with_owner(CRATE_NODE_ID, |this| {
608-
visit::walk_list!(this, visit_item, &krate.items);
609-
visit::walk_list!(this, visit_attribute, &krate.attrs);
610-
this.contains_macro_use(&krate.attrs);
611-
})
616+
617+
// We always have an invocation parent (set in `collect_definitions`)
618+
// of at least the crate root, even for visiting the crate root,
619+
// which would then remove the crate root from the tables
620+
// list twice and try to insert it twice afterwards.
621+
debug_assert_eq!(self.r.current_owner.id, CRATE_NODE_ID);
622+
623+
visit::walk_list!(self, visit_item, &krate.items);
624+
visit::walk_list!(self, visit_attribute, &krate.attrs);
625+
self.contains_macro_use(&krate.attrs);
612626
}
613627
}
614628

0 commit comments

Comments
 (0)