Skip to content

Commit ff9a9ea

Browse files
committed
Auto merge of #138995 - oli-obk:split-resolver, r=petrochenkov
Split the node_id_to_def_id table into a per-owner table *[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/pull/138995)* My goal is to split all the resolver tables that get passed to act lowering into per-owner tables, so that all information that ast lowering needs from the resolver is separated by owners. This should allow us to fully split ast lowering to have one query invocation per owner that steal the individual resolver results for each owner. part of rust-lang/rust-project-goals#620
2 parents 8008927 + 3a2ae6a commit ff9a9ea

18 files changed

Lines changed: 434 additions & 253 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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<'hir> ItemLowerer<'_, 'hir> {
9898
match node {
9999
AstOwner::NonOwner => {}
100100
AstOwner::Crate(c) => {
101-
assert_eq!(self.resolver.local_def_id(CRATE_NODE_ID), CRATE_DEF_ID);
101+
assert_eq!(self.resolver.owner_def_id(CRATE_NODE_ID), CRATE_DEF_ID);
102102
self.with_lctx(CRATE_NODE_ID, |lctx| {
103103
let module = lctx.lower_mod(&c.items, &c.spans);
104104
// FIXME(jdonszelman): is dummy span ever a problem here?
@@ -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: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use rustc_index::{Idx, IndexSlice, IndexVec};
6161
use rustc_macros::extension;
6262
use rustc_middle::hir::{self as mid_hir};
6363
use rustc_middle::span_bug;
64-
use rustc_middle::ty::{DelegationInfo, ResolverAstLowering, TyCtxt};
64+
use rustc_middle::ty::{DelegationInfo, PerOwnerResolverData, ResolverAstLowering, TyCtxt};
6565
use rustc_session::errors::add_feature_diagnostics;
6666
use rustc_span::symbol::{Ident, Symbol, kw, sym};
6767
use rustc_span::{DUMMY_SP, DesugaringKind, Span};
@@ -126,6 +126,7 @@ struct LoweringContext<'a, 'hir> {
126126
is_in_dyn_type: bool,
127127

128128
current_hir_id_owner: hir::OwnerId,
129+
owner: &'a PerOwnerResolverData,
129130
item_local_id_counter: hir::ItemLocalId,
130131
trait_map: ItemLocalMap<&'hir [TraitCandidate<'hir>]>,
131132

@@ -173,6 +174,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
173174
tcx,
174175
resolver,
175176
current_disambiguator: Default::default(),
177+
owner: &resolver.owners[&CRATE_NODE_ID],
176178
arena: tcx.hir_arena,
177179

178180
// HirId handling.
@@ -316,12 +318,8 @@ impl<'tcx> ResolverAstLowering<'tcx> {
316318
self.delegation_infos.get(&id)
317319
}
318320

319-
fn opt_local_def_id(&self, id: NodeId) -> Option<LocalDefId> {
320-
self.node_id_to_def_id.get(&id).copied()
321-
}
322-
323-
fn local_def_id(&self, id: NodeId) -> LocalDefId {
324-
self.opt_local_def_id(id).expect("must have def_id")
321+
fn owner_def_id(&self, id: NodeId) -> LocalDefId {
322+
self.owners[&id].def_id
325323
}
326324

327325
fn lifetime_elision_allowed(&self, id: NodeId) -> bool {
@@ -489,20 +487,20 @@ fn index_crate<'a, 'b>(
489487
}
490488

491489
fn visit_item(&mut self, item: &'a ast::Item) {
492-
let def_id = self.resolver.local_def_id(item.id);
490+
let def_id = self.resolver.owner_def_id(item.id);
493491
*self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) = AstOwner::Item(item);
494492
visit::walk_item(self, item)
495493
}
496494

497495
fn visit_assoc_item(&mut self, item: &'a ast::AssocItem, ctxt: visit::AssocCtxt) {
498-
let def_id = self.resolver.local_def_id(item.id);
496+
let def_id = self.resolver.owner_def_id(item.id);
499497
*self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) =
500498
AstOwner::AssocItem(item, ctxt);
501499
visit::walk_assoc_item(self, item, ctxt);
502500
}
503501

504502
fn visit_foreign_item(&mut self, item: &'a ast::ForeignItem) {
505-
let def_id = self.resolver.local_def_id(item.id);
503+
let def_id = self.resolver.owner_def_id(item.id);
506504
*self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) =
507505
AstOwner::ForeignItem(item);
508506
visit::walk_item(self, item);
@@ -668,12 +666,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
668666
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
669667
self.node_id_to_def_id
670668
.get(&node)
671-
.or_else(|| self.resolver.node_id_to_def_id.get(&node))
669+
.or_else(|| self.owner.node_id_to_def_id.get(&node))
672670
.copied()
673671
}
674672

675673
fn local_def_id(&self, node: NodeId) -> LocalDefId {
676-
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{node:?}`"))
674+
self.opt_local_def_id(node).unwrap_or_else(|| {
675+
self.resolver.owners.items().any(|(id, items)| {
676+
items.node_id_to_def_id.items().any(|(node_id, def_id)| {
677+
if *node_id == node {
678+
let actual_owner = items.node_id_to_def_id.get(id);
679+
panic!("{def_id:?} ({node_id}) was found in {actual_owner:?} ({id})",)
680+
}
681+
false
682+
})
683+
});
684+
panic!("no entry for node id: `{node:?}`");
685+
})
677686
}
678687

679688
fn get_partial_res(&self, id: NodeId) -> Option<PartialRes> {
@@ -685,7 +694,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
685694

686695
/// Given the id of an owner node in the AST, returns the corresponding `OwnerId`.
687696
fn owner_id(&self, node: NodeId) -> hir::OwnerId {
688-
hir::OwnerId { def_id: self.local_def_id(node) }
697+
hir::OwnerId { def_id: self.resolver.owners[&node].def_id }
689698
}
690699

691700
/// Freshen the `LoweringContext` and ready it to lower a nested item.
@@ -710,6 +719,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
710719
.unwrap_or_else(|| PerParentDisambiguatorState::new(def_id));
711720

712721
let disambiguator = std::mem::replace(&mut self.current_disambiguator, new_disambig);
722+
let current_ast_owner = std::mem::replace(&mut self.owner, &self.resolver.owners[&owner]);
713723
let current_attrs = std::mem::take(&mut self.attrs);
714724
let current_bodies = std::mem::take(&mut self.bodies);
715725
let current_define_opaque = std::mem::take(&mut self.define_opaque);
@@ -745,6 +755,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
745755
let info = self.make_owner_info(item);
746756

747757
self.current_disambiguator = disambiguator;
758+
self.owner = current_ast_owner;
748759
self.attrs = current_attrs;
749760
self.bodies = current_bodies;
750761
self.define_opaque = current_define_opaque;
@@ -1793,27 +1804,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
17931804

17941805
let output = match coro {
17951806
Some(coro) => {
1796-
let fn_def_id = self.local_def_id(fn_node_id);
1807+
let fn_def_id = self.owner.def_id;
17971808
self.lower_coroutine_fn_ret_ty(&decl.output, fn_def_id, coro, kind)
17981809
}
17991810
None => match &decl.output {
18001811
FnRetTy::Ty(ty) => {
18011812
let itctx = match kind {
18021813
FnDeclKind::Fn | FnDeclKind::Inherent => ImplTraitContext::OpaqueTy {
18031814
origin: hir::OpaqueTyOrigin::FnReturn {
1804-
parent: self.local_def_id(fn_node_id),
1815+
parent: self.owner.def_id,
18051816
in_trait_or_impl: None,
18061817
},
18071818
},
18081819
FnDeclKind::Trait => ImplTraitContext::OpaqueTy {
18091820
origin: hir::OpaqueTyOrigin::FnReturn {
1810-
parent: self.local_def_id(fn_node_id),
1821+
parent: self.owner.def_id,
18111822
in_trait_or_impl: Some(hir::RpitContext::Trait),
18121823
},
18131824
},
18141825
FnDeclKind::Impl => ImplTraitContext::OpaqueTy {
18151826
origin: hir::OpaqueTyOrigin::FnReturn {
1816-
parent: self.local_def_id(fn_node_id),
1827+
parent: self.owner.def_id,
18171828
in_trait_or_impl: Some(hir::RpitContext::TraitImpl),
18181829
},
18191830
},

compiler/rustc_data_structures/src/unord.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ use crate::stable_hash::{
3434
pub struct UnordItems<T, I: Iterator<Item = T>>(I);
3535

3636
impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
37+
#[inline]
38+
pub fn new(iter: I) -> UnordItems<T, I> {
39+
UnordItems(iter)
40+
}
41+
3742
#[inline]
3843
pub fn map<U, F: Fn(T) -> U>(self, f: F) -> UnordItems<U, impl Iterator<Item = U>> {
3944
UnordItems(self.0.map(f))
@@ -62,6 +67,14 @@ impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
6267
UnordItems(self.0.filter_map(f))
6368
}
6469

70+
#[inline]
71+
pub fn chain(
72+
self,
73+
other: UnordItems<T, impl Iterator<Item = T>>,
74+
) -> UnordItems<T, impl Iterator<Item = T>> {
75+
UnordItems(self.0.chain(other.0))
76+
}
77+
6578
#[inline]
6679
pub fn max(self) -> Option<T>
6780
where
@@ -102,10 +115,10 @@ impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
102115
#[inline]
103116
pub fn flat_map<U, F, O>(self, f: F) -> UnordItems<O, impl Iterator<Item = O>>
104117
where
105-
U: IntoIterator<Item = O>,
106-
F: Fn(T) -> U,
118+
U: Iterator<Item = O>,
119+
F: Fn(T) -> UnordItems<O, U>,
107120
{
108-
UnordItems(self.0.flat_map(f))
121+
UnordItems(self.0.flat_map(move |x| f(x).0))
109122
}
110123

111124
pub fn collect<C: From<UnordItems<T, I>>>(self) -> C {

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,21 @@ pub struct ResolverGlobalCtxt {
198198
pub stripped_cfg_items: Vec<StrippedCfgItem>,
199199
}
200200

201+
#[derive(Debug)]
202+
pub struct PerOwnerResolverData {
203+
pub node_id_to_def_id: NodeMap<LocalDefId>,
204+
/// The id of the owner
205+
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,
208+
}
209+
210+
impl PerOwnerResolverData {
211+
pub fn new(id: ast::NodeId, def_id: LocalDefId) -> PerOwnerResolverData {
212+
PerOwnerResolverData { node_id_to_def_id: Default::default(), id, def_id }
213+
}
214+
}
215+
201216
/// Resolutions that should only be used for lowering.
202217
/// This struct is meant to be consumed by lowering.
203218
#[derive(Debug)]
@@ -215,7 +230,7 @@ pub struct ResolverAstLowering<'tcx> {
215230

216231
pub next_node_id: ast::NodeId,
217232

218-
pub node_id_to_def_id: NodeMap<LocalDefId>,
233+
pub owners: NodeMap<PerOwnerResolverData>,
219234

220235
pub trait_map: NodeMap<&'tcx [hir::TraitCandidate<'tcx>]>,
221236
/// List functions and methods for which lifetime elision was successful.

compiler/rustc_passes/src/lang_items.rs

Lines changed: 3 additions & 9 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.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(),
@@ -309,7 +309,7 @@ impl<'ast, 'tcx> visit::Visitor<'ast> for LanguageItemCollector<'ast, 'tcx> {
309309
fn visit_variant(&mut self, variant: &'ast ast::Variant) {
310310
self.check_for_lang(
311311
Target::Variant,
312-
self.resolver.node_id_to_def_id[&variant.id],
312+
self.resolver.owners[&self.parent_item.unwrap().id].node_id_to_def_id[&variant.id],
313313
&variant.attrs,
314314
variant.span,
315315
None,
@@ -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.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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -743,12 +743,13 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
743743
}
744744
ast::UseTreeKind::Nested { ref items, .. } => {
745745
for &(ref tree, id) in items {
746-
let feed = self.create_def(id, None, DefKind::Use, use_tree.span());
747-
self.build_reduced_graph_for_use_tree(
748-
// This particular use tree
749-
tree, id, &prefix, true, false, // The whole `use` item
750-
item, vis, root_span, feed,
751-
);
746+
self.with_owner(id, None, DefKind::Use, use_tree.span(), |this, feed| {
747+
this.build_reduced_graph_for_use_tree(
748+
// This particular use tree
749+
tree, id, &prefix, true, false, // The whole `use` item
750+
item, vis, root_span, feed,
751+
)
752+
});
752753
}
753754

754755
// Empty groups `a::b::{}` are turned into synthetic `self` imports

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
3131
use rustc_data_structures::unord::UnordSet;
3232
use rustc_errors::{DiagArgValue, Diagnostic, MultiSpan};
3333
use rustc_hir::def::{DefKind, Res};
34+
use rustc_hir::def_id::LocalDefId;
3435
use rustc_session::lint::builtin::{
3536
MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES, UNUSED_IMPORTS, UNUSED_QUALIFICATIONS,
3637
};
@@ -81,9 +82,8 @@ struct ExternCrateToLint {
8182
impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> {
8283
// We have information about whether `use` (import) items are actually
8384
// used now. If an import is not used at all, we signal a lint error.
84-
fn check_import(&mut self, id: ast::NodeId) {
85+
fn check_import(&mut self, id: ast::NodeId, def_id: LocalDefId) {
8586
let used = self.r.used_imports.contains(&id);
86-
let def_id = self.r.local_def_id(id);
8787
if !used {
8888
if self.r.maybe_unused_trait_imports.contains(&def_id) {
8989
// Check later.
@@ -102,7 +102,8 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> {
102102
}
103103

104104
fn check_use_tree(&mut self, use_tree: &'a ast::UseTree, id: ast::NodeId) {
105-
if self.r.effective_visibilities.is_exported(self.r.local_def_id(id)) {
105+
let def_id = self.r.owner_def_id(id);
106+
if self.r.effective_visibilities.is_exported(def_id) {
106107
self.check_import_as_underscore(use_tree, id);
107108
return;
108109
}
@@ -112,7 +113,7 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> {
112113
self.unused_import(self.base_id).add(id);
113114
}
114115
} else {
115-
self.check_import(id);
116+
self.check_import(id, def_id);
116117
}
117118
}
118119

@@ -212,7 +213,7 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> {
212213

213214
let module = self
214215
.r
215-
.get_nearest_non_block_module(self.r.local_def_id(extern_crate.id).to_def_id());
216+
.get_nearest_non_block_module(self.r.owner_def_id(extern_crate.id).to_def_id());
216217
if module.no_implicit_prelude {
217218
// If the module has `no_implicit_prelude`, then we don't suggest
218219
// replacing the extern crate with a use, as it would not be
@@ -494,7 +495,7 @@ impl Resolver<'_, '_> {
494495
None
495496
} else {
496497
let parent_module = visitor.r.get_nearest_non_block_module(
497-
visitor.r.local_def_id(unused.use_tree_id).to_def_id(),
498+
visitor.r.owner_def_id(unused.use_tree_id).to_def_id(),
498499
);
499500
match module_to_string(parent_module) {
500501
Some(module)

0 commit comments

Comments
 (0)