Skip to content

Commit b693dc1

Browse files
authored
Unrolled build for #155242
Rollup merge of #155242 - petrochenkov:modmodmod, r=mu001999 resolve: Introduce `(Local,Extern)Module` newtypes for local and external modules respectively Right now both `LocalModule` and `ExternModule` refer to the same `ModuleData`, but the module data for local and extern modules can potentially be made quite different, and we can specialize name lookup for both cases as an optimization. Declaration creation for local and external modules was already specialized as a part of the parallel import resolution work (see `define_local` and `define_extern`, #145108 and previous PRs), because they have different properties with regards to ownership and synchronization.
2 parents f29256d + 61bc404 commit b693dc1

9 files changed

Lines changed: 197 additions & 109 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ use crate::imports::{ImportData, ImportKind, OnUnknownData};
3737
use crate::macros::{MacroRulesDecl, MacroRulesScope, MacroRulesScopeRef};
3838
use crate::ref_mut::CmCell;
3939
use crate::{
40-
BindingKey, Decl, DeclData, DeclKind, ExternPreludeEntry, Finalize, IdentKey, MacroData,
41-
Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, Res, ResolutionError,
42-
Resolver, Segment, Used, VisResolutionError, errors,
40+
BindingKey, Decl, DeclData, DeclKind, ExternModule, ExternPreludeEntry, Finalize, IdentKey,
41+
LocalModule, MacroData, Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, Res,
42+
ResolutionError, Resolver, Segment, Used, VisResolutionError, errors,
4343
};
4444

4545
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
@@ -62,23 +62,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
6262
/// Create a name definition from the given components, and put it into the local module.
6363
fn define_local(
6464
&mut self,
65-
parent: Module<'ra>,
65+
parent: LocalModule<'ra>,
6666
orig_ident: Ident,
6767
ns: Namespace,
6868
res: Res,
6969
vis: Visibility,
7070
span: Span,
7171
expn_id: LocalExpnId,
7272
) {
73-
let decl = self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id, Some(parent));
73+
let decl =
74+
self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id, Some(parent.to_module()));
7475
let ident = IdentKey::new(orig_ident);
7576
self.plant_decl_into_local_module(ident, orig_ident.span, ns, decl);
7677
}
7778

7879
/// Create a name definition from the given components, and put it into the extern module.
7980
fn define_extern(
8081
&self,
81-
parent: Module<'ra>,
82+
parent: ExternModule<'ra>,
8283
ident: IdentKey,
8384
orig_ident_span: Span,
8485
ns: Namespace,
@@ -97,15 +98,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
9798
vis: CmCell::new(vis),
9899
span,
99100
expansion,
100-
parent_module: Some(parent),
101+
parent_module: Some(parent.to_module()),
101102
});
102103
// Even if underscore names cannot be looked up, we still need to add them to modules,
103104
// because they can be fetched by glob imports from those modules, and bring traits
104105
// into scope both directly and through glob imports.
105106
let key =
106107
BindingKey::new_disambiguated(ident, ns, || (child_index + 1).try_into().unwrap()); // 0 indicates no underscore
107108
if self
108-
.resolution_or_default(parent, key, orig_ident_span)
109+
.resolution_or_default(parent.to_module(), key, orig_ident_span)
109110
.borrow_mut_unchecked()
110111
.non_glob_decl
111112
.replace(decl)
@@ -149,30 +150,30 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
149150
/// returns `None`.
150151
pub(crate) fn get_module(&self, def_id: DefId) -> Option<Module<'ra>> {
151152
match def_id.as_local() {
152-
Some(local_def_id) => self.local_module_map.get(&local_def_id).copied(),
153+
Some(local_def_id) => self.local_module_map.get(&local_def_id).map(|m| m.to_module()),
153154
None => {
154155
if let module @ Some(..) = self.extern_module_map.borrow().get(&def_id) {
155-
return module.copied();
156+
return module.map(|m| m.to_module());
156157
}
157158

158159
// Query `def_kind` is not used because query system overhead is too expensive here.
159160
let def_kind = self.cstore().def_kind_untracked(self.tcx, def_id);
160161
if def_kind.is_module_like() {
161-
let parent = self
162-
.tcx
163-
.opt_parent(def_id)
164-
.map(|parent_id| self.get_nearest_non_block_module(parent_id));
162+
let parent = self.tcx.opt_parent(def_id).map(|parent_id| {
163+
self.get_nearest_non_block_module(parent_id).expect_extern()
164+
});
165165
// Query `expn_that_defined` is not used because
166166
// hashing spans in its result is expensive.
167167
let expn_id = self.cstore().expn_that_defined_untracked(self.tcx, def_id);
168-
return Some(self.new_extern_module(
168+
let module = self.new_extern_module(
169169
parent,
170170
ModuleKind::Def(def_kind, def_id, Some(self.tcx.item_name(def_id))),
171171
expn_id,
172172
self.def_span(def_id),
173173
// FIXME: Account for `#[no_implicit_prelude]` attributes.
174174
parent.is_some_and(|module| module.no_implicit_prelude),
175-
));
175+
);
176+
return Some(module.to_module());
176177
}
177178

178179
None
@@ -186,13 +187,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
186187
None => expn_id
187188
.as_local()
188189
.and_then(|expn_id| self.ast_transform_scopes.get(&expn_id).copied())
189-
.unwrap_or(self.graph_root),
190+
.unwrap_or(self.graph_root)
191+
.to_module(),
190192
}
191193
}
192194

193195
pub(crate) fn macro_def_scope(&self, def_id: DefId) -> Module<'ra> {
194196
if let Some(id) = def_id.as_local() {
195-
self.local_macro_def_scopes[&id]
197+
self.local_macro_def_scopes[&id].to_module()
196198
} else {
197199
self.get_nearest_non_block_module(def_id)
198200
}
@@ -246,10 +248,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
246248
visitor.parent_scope.macro_rules
247249
}
248250

249-
pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) {
251+
pub(crate) fn build_reduced_graph_external(&self, module: ExternModule<'ra>) {
250252
let def_id = module.def_id();
251253
let children = self.tcx.module_children(def_id);
252-
let parent_scope = ParentScope::module(module, self.arenas);
254+
let parent_scope = ParentScope::module(module.to_module(), self.arenas);
253255
for (i, child) in children.iter().enumerate() {
254256
self.build_reduced_graph_for_external_crate_res(child, parent_scope, i, None)
255257
}
@@ -273,7 +275,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
273275
child_index: usize,
274276
ambig_child: Option<&ModChild>,
275277
) {
276-
let parent = parent_scope.module;
278+
let parent = parent_scope.module.expect_extern();
277279
let child_span = |this: &Self, reexport_chain: &[Reexport], res: def::Res<_>| {
278280
this.def_span(
279281
reexport_chain
@@ -291,7 +293,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
291293
let ModChild { ident: _, res, vis, ref reexport_chain } = *ambig_child;
292294
let span = child_span(self, reexport_chain, res);
293295
let res = res.expect_non_local();
294-
self.arenas.new_def_decl(res, vis, span, expansion, Some(parent))
296+
self.arenas.new_def_decl(res, vis, span, expansion, Some(parent.to_module()))
295297
});
296298

297299
// Record primary definitions.
@@ -801,7 +803,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
801803
adt_span: Span,
802804
) {
803805
let parent_scope = &self.parent_scope;
804-
let parent = parent_scope.module;
806+
let parent = parent_scope.module.expect_local();
805807
let expansion = parent_scope.expansion;
806808

807809
// Define a name in the type namespace if it is not anonymous.
@@ -817,7 +819,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
817819
/// Constructs the reduced graph for one item.
818820
fn build_reduced_graph_for_item(&mut self, item: &'a Item) {
819821
let parent_scope = &self.parent_scope;
820-
let parent = parent_scope.module;
822+
let parent = parent_scope.module.expect_local();
821823
let expansion = parent_scope.expansion;
822824
let sp = item.span;
823825
let vis = self.resolve_visibility(&item.vis);
@@ -862,14 +864,15 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
862864
{
863865
self.r.mods_with_parse_errors.insert(def_id);
864866
}
865-
self.parent_scope.module = self.r.new_local_module(
867+
let module = self.r.new_local_module(
866868
Some(parent),
867869
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
868870
expansion.to_expn_id(),
869871
item.span,
870872
parent.no_implicit_prelude
871873
|| ast::attr::contains_name(&item.attrs, sym::no_implicit_prelude),
872874
);
875+
self.parent_scope.module = module.to_module();
873876
}
874877

875878
// These items live in the value namespace.
@@ -895,13 +898,14 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
895898
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => {
896899
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
897900

898-
self.parent_scope.module = self.r.new_local_module(
901+
let module = self.r.new_local_module(
899902
Some(parent),
900903
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
901904
expansion.to_expn_id(),
902905
item.span,
903906
parent.no_implicit_prelude,
904907
);
908+
self.parent_scope.module = module.to_module();
905909
}
906910

907911
// These items live in both the type and value namespaces.
@@ -997,7 +1001,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
9971001
self.r.dcx().emit_err(errors::ExternCrateSelfRequiresRenaming { span: sp });
9981002
return;
9991003
} else if orig_name == Some(kw::SelfLower) {
1000-
Some(self.r.graph_root)
1004+
Some(self.r.graph_root.to_module())
10011005
} else {
10021006
let tcx = self.r.tcx;
10031007
let crate_id = self.r.cstore_mut().process_extern_crate(
@@ -1038,7 +1042,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10381042
self.r.potentially_unused_imports.push(import);
10391043
let import_decl = self.r.new_import_decl(decl, import);
10401044
let ident = IdentKey::new(orig_ident);
1041-
if ident.name != kw::Underscore && parent == self.r.graph_root {
1045+
if ident.name != kw::Underscore && parent == self.r.graph_root.to_module() {
10421046
// FIXME: this error is technically unnecessary now when extern prelude is split into
10431047
// two scopes, remove it with lang team approval.
10441048
if let Some(entry) = self.r.extern_prelude.get(&ident)
@@ -1083,15 +1087,15 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10831087
ForeignItemKind::TyAlias(..) => TypeNS,
10841088
ForeignItemKind::MacCall(..) => unreachable!(),
10851089
};
1086-
let parent = self.parent_scope.module;
1090+
let parent = self.parent_scope.module.expect_local();
10871091
let expansion = self.parent_scope.expansion;
10881092
let vis = self.resolve_visibility(&item.vis);
10891093
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
10901094
self.r.feed_visibility(feed, vis);
10911095
}
10921096

10931097
fn build_reduced_graph_for_block(&mut self, block: &Block) {
1094-
let parent = self.parent_scope.module;
1098+
let parent = self.parent_scope.module.expect_local();
10951099
let expansion = self.parent_scope.expansion;
10961100
if self.block_needs_anonymous_module(block) {
10971101
let module = self.r.new_local_module(
@@ -1102,7 +1106,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11021106
parent.no_implicit_prelude,
11031107
);
11041108
self.r.block_map.insert(block.id, module);
1105-
self.parent_scope.module = module; // Descend into the block.
1109+
self.parent_scope.module = module.to_module(); // Descend into the block.
11061110
}
11071111
}
11081112

@@ -1302,7 +1306,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13021306
_ => unreachable!(),
13031307
};
13041308

1305-
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
1309+
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module.expect_local());
13061310

13071311
if macro_rules {
13081312
let ident = IdentKey::new(orig_ident);
@@ -1325,7 +1329,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13251329
let import = self.r.arenas.alloc_import(ImportData {
13261330
kind: ImportKind::MacroExport,
13271331
root_id: item.id,
1328-
parent_scope: ParentScope { module: self.r.graph_root, ..parent_scope },
1332+
parent_scope: ParentScope {
1333+
module: self.r.graph_root.to_module(),
1334+
..parent_scope
1335+
},
13291336
imported_module: CmCell::new(None),
13301337
has_attributes: false,
13311338
use_span_with_attributes: span,
@@ -1356,7 +1363,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13561363
self.r.macro_rules_scopes.insert(def_id, scope);
13571364
scope
13581365
} else {
1359-
let module = parent_scope.module;
1366+
let module = parent_scope.module.expect_local();
13601367
let vis = match item.kind {
13611368
// Visibilities must not be resolved non-speculatively twice
13621369
// and we already resolved this one as a `fn` item visibility.
@@ -1504,7 +1511,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
15041511
}
15051512

15061513
if ctxt == AssocCtxt::Trait {
1507-
let parent = self.parent_scope.module;
1514+
let parent = self.parent_scope.module.expect_local();
15081515
let expansion = self.parent_scope.expansion;
15091516
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
15101517
} else if !matches!(&item.kind, AssocItemKind::Delegation(deleg) if deleg.from_glob)
@@ -1586,7 +1593,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
15861593
return;
15871594
}
15881595

1589-
let parent = self.parent_scope.module;
1596+
let parent = self.parent_scope.module.expect_local();
15901597
let expn_id = self.parent_scope.expansion;
15911598
let ident = variant.ident;
15921599

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ impl Resolver<'_, '_> {
545545
let unused_imports = visitor.unused_imports;
546546
let mut check_redundant_imports = FxIndexSet::default();
547547
for module in &self.local_modules {
548-
for (_key, resolution) in self.resolutions(*module).borrow().iter() {
548+
for (_key, resolution) in self.resolutions(module.to_module()).borrow().iter() {
549549
if let Some(decl) = resolution.borrow().best_decl()
550550
&& let DeclKind::Import { import, .. } = decl.kind
551551
&& let ImportKind::Single { id, .. } = import.kind

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15851585
lookup_ident,
15861586
namespace,
15871587
parent_scope,
1588-
self.graph_root,
1588+
self.graph_root.to_module(),
15891589
crate_path,
15901590
&filter_fn,
15911591
);
@@ -2074,7 +2074,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20742074

20752075
if kind != AmbiguityKind::GlobVsGlob {
20762076
if let Scope::ModuleNonGlobs(module, _) | Scope::ModuleGlobs(module, _) = scope {
2077-
if module == self.graph_root {
2077+
if module == self.graph_root.to_module() {
20782078
help_msgs.push(format!(
20792079
"use `crate::{ident}` to refer to this {thing} unambiguously"
20802080
));
@@ -2452,7 +2452,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24522452
self.local_module_map
24532453
.iter()
24542454
.filter(|(_, module)| {
2455-
current_module.is_ancestor_of(**module) && current_module != **module
2455+
let module = module.to_module();
2456+
current_module.is_ancestor_of(module) && current_module != module
24562457
})
24572458
.flat_map(|(_, module)| module.kind.name()),
24582459
)
@@ -2461,7 +2462,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24612462
.borrow()
24622463
.iter()
24632464
.filter(|(_, module)| {
2464-
current_module.is_ancestor_of(**module) && current_module != **module
2465+
let module = module.to_module();
2466+
current_module.is_ancestor_of(module) && current_module != module
24652467
})
24662468
.flat_map(|(_, module)| module.kind.name()),
24672469
)

compiler/rustc_resolve/src/ident.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ use crate::late::{
2424
use crate::macros::{MacroRulesScope, sub_namespace_match};
2525
use crate::{
2626
AmbiguityError, AmbiguityKind, AmbiguityWarning, BindingKey, CmResolver, Decl, DeclKind,
27-
Determinacy, Finalize, IdentKey, ImportKind, LateDecl, Module, ModuleKind, ModuleOrUniformRoot,
28-
ParentScope, PathResult, PrivacyError, Res, ResolutionError, Resolver, Scope, ScopeSet,
29-
Segment, Stage, Symbol, Used, errors,
27+
Determinacy, Finalize, IdentKey, ImportKind, LateDecl, LocalModule, Module, ModuleKind,
28+
ModuleOrUniformRoot, ParentScope, PathResult, PrivacyError, Res, ResolutionError, Resolver,
29+
Scope, ScopeSet, Segment, Stage, Symbol, Used, errors,
3030
};
3131

3232
#[derive(Copy, Clone)]
@@ -346,7 +346,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
346346
} else if let RibKind::Block(Some(module)) = rib.kind
347347
&& let Ok(binding) = self.cm().resolve_ident_in_scope_set(
348348
ident,
349-
ScopeSet::Module(ns, module),
349+
ScopeSet::Module(ns, module.to_module()),
350350
parent_scope,
351351
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
352352
ignore_decl,
@@ -357,7 +357,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
357357
return Some(LateDecl::Decl(binding));
358358
} else if let RibKind::Module(module) = rib.kind {
359359
// Encountered a module item, abandon ribs and look into that module and preludes.
360-
let parent_scope = &ParentScope { module, ..*parent_scope };
360+
let parent_scope = &ParentScope { module: module.to_module(), ..*parent_scope };
361361
let finalize = finalize.map(|f| Finalize { stage: Stage::Late, ..f });
362362
return self
363363
.cm()
@@ -658,7 +658,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
658658
)
659659
};
660660
let binding = self.reborrow().resolve_ident_in_module_globs_unadjusted(
661-
module,
661+
module.expect_local(),
662662
ident,
663663
orig_ident_span,
664664
ns,
@@ -1122,7 +1122,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11221122
/// Attempts to resolve `ident` in namespace `ns` of glob bindings in `module`.
11231123
fn resolve_ident_in_module_globs_unadjusted<'r>(
11241124
mut self: CmResolver<'r, 'ra, 'tcx>,
1125-
module: Module<'ra>,
1125+
module: LocalModule<'ra>,
11261126
ident: IdentKey,
11271127
orig_ident_span: Span,
11281128
ns: Namespace,
@@ -1137,7 +1137,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11371137
// doesn't need to be mutable. It will fail when there is a cycle of imports, and without
11381138
// the exclusive access infinite recursion will crash the compiler with stack overflow.
11391139
let resolution = &*self
1140-
.resolution_or_default(module, key, orig_ident_span)
1140+
.resolution_or_default(module.to_module(), key, orig_ident_span)
11411141
.try_borrow_mut_unchecked()
11421142
.map_err(|_| ControlFlow::Continue(Determined))?;
11431143

0 commit comments

Comments
 (0)