Skip to content

Commit aa5ec85

Browse files
committed
Auto merge of #156362 - petrochenkov:modata, r=<try>
resolve: Introduce separate `(Local,Extern)ModuleData` structures for local and external modules respectively
2 parents 0490dd9 + 84650e8 commit aa5ec85

8 files changed

Lines changed: 350 additions & 255 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ use std::sync::Arc;
99

1010
use rustc_ast::visit::{self, AssocCtxt, Visitor, WalkItemKind};
1111
use rustc_ast::{
12-
self as ast, AssocItem, AssocItemKind, Block, ConstItem, DUMMY_NODE_ID, Delegation, Fn,
13-
ForeignItem, ForeignItemKind, Inline, Item, ItemKind, NodeId, StaticItem, StmtKind, TraitAlias,
14-
TyAlias,
12+
self as ast, AssocItem, AssocItemKind, Block, ConstItem, Delegation, Fn, ForeignItem,
13+
ForeignItemKind, Inline, Item, ItemKind, NodeId, StaticItem, StmtKind, TraitAlias, TyAlias,
1514
};
1615
use rustc_attr_parsing::AttributeParser;
1716
use rustc_expand::base::{ResolverExpand, SyntaxExtension, SyntaxExtensionKind};
@@ -168,12 +167,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
168167
let expn_id = self.cstore().expn_that_defined_untracked(self.tcx, def_id);
169168
let module = self.new_extern_module(
170169
parent,
171-
ModuleKind::Def(
172-
def_kind,
173-
def_id,
174-
DUMMY_NODE_ID,
175-
Some(self.tcx.item_name(def_id)),
176-
),
170+
def_kind,
171+
def_id,
172+
self.tcx.item_name(def_id),
177173
expn_id,
178174
self.def_span(def_id),
179175
// FIXME: Account for `#[no_implicit_prelude]` attributes.
@@ -253,7 +249,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
253249
match vis.kind {
254250
ast::VisibilityKind::Public => Ok(Visibility::Public),
255251
ast::VisibilityKind::Inherited => {
256-
Ok(match parent_scope.module.kind {
252+
Ok(match parent_scope.module.expect_local().kind {
257253
// Any inherited visibility resolved directly inside an enum or trait
258254
// (i.e. variants, fields, and trait items) inherits from the visibility
259255
// of the enum or trait.
@@ -350,7 +346,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
350346
}
351347

352348
pub(crate) fn build_reduced_graph_external(&self, module: ExternModule<'ra>) {
353-
let def_id = module.def_id();
349+
let def_id = module.def_id;
354350
let children = self.tcx.module_children(def_id);
355351
for (i, child) in children.iter().enumerate() {
356352
self.build_reduced_graph_for_external_crate_res(child, module, i, None)
@@ -535,7 +531,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
535531
root_id: NodeId,
536532
vis: Visibility,
537533
) {
538-
let current_module = self.parent_scope.module;
534+
let current_module = self.parent_scope.module.expect_local();
539535
let import = self.r.arenas.alloc_import(ImportData {
540536
kind,
541537
parent_scope: self.parent_scope,
@@ -560,7 +556,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
560556
if target.name != kw::Underscore {
561557
self.r.per_ns(|this, ns| {
562558
let key = BindingKey::new(IdentKey::new(target), ns);
563-
this.resolution_or_default(current_module, key, target.span)
559+
this.resolution_or_default(current_module.to_module(), key, target.span)
564560
.borrow_mut(this)
565561
.single_imports
566562
.insert(import);
@@ -1136,7 +1132,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
11361132
if let Some(Attribute::Parsed(AttributeKind::MacroUse { span, arguments })) =
11371133
AttributeParser::parse_limited(self.r.tcx.sess, &item.attrs, &[sym::macro_use])
11381134
{
1139-
if self.parent_scope.module.parent.is_some() {
1135+
if self.parent_scope.module.expect_local().parent.is_some() {
11401136
self.r
11411137
.dcx()
11421138
.emit_err(errors::ExternCrateLoadingMacroNotAtCrateRoot { span: item.span });
@@ -1247,7 +1243,8 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
12471243
/// directly into its parent scope's module.
12481244
pub(crate) fn visit_invoc_in_module(&mut self, id: NodeId) -> MacroRulesScopeRef<'ra> {
12491245
let invoc_id = self.visit_invoc(id);
1250-
self.parent_scope.module.unexpanded_invocations.borrow_mut(self.r).insert(invoc_id);
1246+
let module = self.parent_scope.module.expect_local();
1247+
module.unexpanded_invocations.borrow_mut(self.r).insert(invoc_id);
12511248
self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Invocation(invoc_id))
12521249
}
12531250

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
240240
return self.report_conflict(ident, ns, new_binding, old_binding);
241241
}
242242

243-
let container = match old_binding.parent_module.unwrap().kind {
243+
let container = match old_binding.parent_module.unwrap().expect_local().kind {
244244
// Avoid using TyCtxt::def_kind_descr in the resolver, because it
245245
// indirectly *calls* the resolver, and would cause a query cycle.
246246
ModuleKind::Def(kind, def_id, _, _) => kind.descr(def_id),
@@ -1757,15 +1757,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17571757
}
17581758
return;
17591759
}
1760-
if Some(parent_nearest) == scope.opt_def_id() {
1760+
if Some(parent_nearest) == scope.kind.opt_def_id() {
17611761
err.subdiagnostic(MacroDefinedLater { span: unused_ident.span });
17621762
err.subdiagnostic(MacroSuggMovePosition { span: ident.span, ident });
17631763
return;
17641764
}
17651765
}
17661766

17671767
if ident.name == kw::Default
1768-
&& let ModuleKind::Def(DefKind::Enum, def_id, _, _) = parent_scope.module.kind
1768+
&& let ModuleKind::Def(DefKind::Enum, def_id, _, _) = parent_scope.module.kind()
17691769
{
17701770
let span = self.def_span(def_id);
17711771
let source_map = self.tcx.sess.source_map();
@@ -1894,7 +1894,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18941894
ident.name,
18951895
);
18961896
let sugg_span =
1897-
if let ModuleKind::Def(DefKind::Enum, id, _, _) = parent_scope.module.kind {
1897+
if let ModuleKind::Def(DefKind::Enum, id, _, _) = parent_scope.module.kind() {
18981898
let span = self.def_span(id);
18991899
if span.from_expansion() {
19001900
None
@@ -2607,7 +2607,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
26072607
let module = module.to_module();
26082608
current_module.is_ancestor_of(module) && current_module != module
26092609
})
2610-
.flat_map(|(_, module)| module.kind.name()),
2610+
.map(|(_, module)| module.name),
26112611
)
26122612
.filter(|c| !c.to_string().is_empty())
26132613
.collect::<Vec<_>>();
@@ -2631,8 +2631,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
26312631
) -> (String, String, Option<Suggestion>) {
26322632
let is_last = failed_segment_idx == path.len() - 1;
26332633
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
2634-
let module_res = match module {
2635-
Some(ModuleOrUniformRoot::Module(module)) => module.res(),
2634+
let module_def_id = match module {
2635+
Some(ModuleOrUniformRoot::Module(module)) => module.opt_def_id(),
26362636
_ => None,
26372637
};
26382638
let scope = match &path[..failed_segment_idx] {
@@ -2647,7 +2647,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
26472647
};
26482648
let message = format!("cannot find `{ident}` in {scope}");
26492649

2650-
if module_res == self.graph_root.res() {
2650+
if module_def_id == Some(CRATE_DEF_ID.to_def_id()) {
26512651
let is_mod = |res| matches!(res, Res::Def(DefKind::Mod, _));
26522652
let mut candidates = self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod);
26532653
candidates
@@ -3128,7 +3128,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
31283128
return None;
31293129
};
31303130

3131-
while let Some(parent) = crate_module.parent {
3131+
while let Some(parent) = crate_module.parent() {
31323132
crate_module = parent;
31333133
}
31343134

@@ -3145,7 +3145,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
31453145
if !kinds.contains(MacroKinds::BANG) {
31463146
return None;
31473147
}
3148-
let module_name = crate_module.kind.name().unwrap_or(kw::Crate);
3148+
let module_name = crate_module.name().unwrap_or(kw::Crate);
31493149
let import_snippet = match import.kind {
31503150
ImportKind::Single { source, target, .. } if source != target => {
31513151
format!("{source} as {target}")
@@ -3290,20 +3290,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
32903290
return cached;
32913291
}
32923292
visited.insert(parent_module, false);
3293-
let m = r.expect_module(parent_module);
32943293
let mut res = false;
3295-
for importer in m.glob_importers.borrow().iter() {
3296-
if let Some(next_parent_module) = importer.parent_scope.module.opt_def_id() {
3297-
if next_parent_module == module
3298-
|| comes_from_same_module_for_glob(
3299-
r,
3300-
next_parent_module,
3301-
module,
3302-
visited,
3303-
)
3294+
if let Some(m) = r.expect_module(parent_module).as_local() {
3295+
for importer in m.glob_importers.borrow().iter() {
3296+
if let Some(next_parent_module) = importer.parent_scope.module.opt_def_id()
33043297
{
3305-
res = true;
3306-
break;
3298+
if next_parent_module == module
3299+
|| comes_from_same_module_for_glob(
3300+
r,
3301+
next_parent_module,
3302+
module,
3303+
visited,
3304+
)
3305+
{
3306+
res = true;
3307+
break;
3308+
}
33073309
}
33083310
}
33093311
}

compiler/rustc_resolve/src/ident.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
247247
return Some((self.expn_def_scope(expn_id), None));
248248
}
249249

250-
if let ModuleKind::Block = module.kind {
251-
return Some((module.parent.unwrap().nearest_item_scope(), None));
250+
if let ModuleKind::Block = module.kind() {
251+
return Some((module.parent().unwrap().nearest_item_scope(), None));
252252
}
253253

254254
// We need to support the next case under a deprecation warning
@@ -263,7 +263,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
263263
// ```
264264
// So we have to fall back to the module's parent during lexical resolution in this case.
265265
if derive_fallback_lint_id.is_some()
266-
&& let Some(parent) = module.parent
266+
&& let Some(parent) = module.parent()
267267
// Inner module is inside the macro
268268
&& module.expansion != parent.expansion
269269
// Parent module is outside of the macro
@@ -643,27 +643,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
643643
Err(ControlFlow::Break(..)) => return decl,
644644
}
645645
}
646-
Scope::ModuleGlobs(module, _)
647-
if let ModuleKind::Def(_, def_id, _, _) = module.kind
648-
&& !def_id.is_local() =>
649-
{
646+
Scope::ModuleGlobs(Module::Extern(_), _) => {
650647
// Fast path: external module decoding only creates non-glob declarations.
651648
Err(Determined)
652649
}
653-
Scope::ModuleGlobs(module, derive_fallback_lint_id) => {
650+
Scope::ModuleGlobs(Module::Local(module), derive_fallback_lint_id) => {
654651
let (adjusted_parent_scope, adjusted_finalize) = if matches!(
655652
scope_set,
656653
ScopeSet::Module(..) | ScopeSet::ModuleAndExternPrelude(..)
657654
) {
658655
(parent_scope, finalize)
659656
} else {
660657
(
661-
&ParentScope { module, ..*parent_scope },
658+
&ParentScope { module: module.to_module(), ..*parent_scope },
662659
finalize.map(|f| Finalize { used: Used::Scope, ..f }),
663660
)
664661
};
665662
let binding = self.reborrow().resolve_ident_in_module_globs_unadjusted(
666-
module.expect_local(),
663+
module,
667664
ident,
668665
orig_ident_span,
669666
ns,
@@ -699,9 +696,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
699696
}
700697
Scope::MacroUsePrelude => match self.macro_use_prelude.get(&ident.name).cloned() {
701698
Some(decl) => Ok(decl),
702-
None => Err(Determinacy::determined(
703-
self.graph_root.unexpanded_invocations.borrow().is_empty(),
704-
)),
699+
None => Err(Determinacy::determined(!self.graph_root.has_unexpanded_invocations())),
705700
},
706701
Scope::BuiltinAttrs => match self.builtin_attr_decls.get(&ident.name) {
707702
Some(decl) => Ok(*decl),
@@ -714,9 +709,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
714709
finalize.is_some(),
715710
) {
716711
Some(decl) => Ok(decl),
717-
None => Err(Determinacy::determined(
718-
self.graph_root.unexpanded_invocations.borrow().is_empty(),
719-
)),
712+
None => {
713+
Err(Determinacy::determined(!self.graph_root.has_unexpanded_invocations()))
714+
}
720715
}
721716
}
722717
Scope::ExternPreludeFlags => {
@@ -966,7 +961,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
966961
let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
967962
module
968963
.unwrap_or_else(|| self.resolve_self(&mut ctxt, parent_scope.module))
969-
.parent
964+
.parent()
970965
.map(|parent| self.resolve_self(&mut ctxt, parent))
971966
}
972967

@@ -1126,6 +1121,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11261121
return if accessible { Ok(binding) } else { Err(ControlFlow::Break(Determined)) };
11271122
}
11281123

1124+
// In extern modules everything is determined from the start.
1125+
let Some(module) = module.as_local() else {
1126+
return Err(ControlFlow::Continue(Determined));
1127+
};
1128+
11291129
// Check if one of single imports can still define the name, block if it can.
11301130
if self.reborrow().single_import_can_define_name(
11311131
&resolution,
@@ -1139,7 +1139,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11391139
}
11401140

11411141
// Check if one of unexpanded macros can still define the name.
1142-
if !module.unexpanded_invocations.borrow().is_empty() {
1142+
if module.has_unexpanded_invocations() {
11431143
return Err(ControlFlow::Continue(Undetermined));
11441144
}
11451145

@@ -1223,7 +1223,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12231223
// scopes we return `Undetermined` with `ControlFlow::Continue`.
12241224
// Check if one of unexpanded macros can still define the name,
12251225
// if it can then our "no resolution" result is not determined and can be invalidated.
1226-
if !module.unexpanded_invocations.borrow().is_empty() {
1226+
if module.has_unexpanded_invocations() {
12271227
return Err(ControlFlow::Continue(Undetermined));
12281228
}
12291229

compiler/rustc_resolve/src/imports.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17901790
}
17911791

17921792
// Add to module's glob_importers
1793-
module.glob_importers.borrow_mut_unchecked().push(import);
1793+
if let Some(module) = module.as_local() {
1794+
module.glob_importers.borrow_mut_unchecked().push(import);
1795+
}
17941796

17951797
// Ensure that `resolutions` isn't borrowed during `try_define`,
17961798
// since it might get updated via a glob cycle.
@@ -1844,7 +1846,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18441846
// Since import resolution is finished, globs will not define any more names.
18451847
*module.globs.borrow_mut(self) = Vec::new();
18461848

1847-
let Some(def_id) = module.opt_def_id() else { return };
1849+
let Some(def_id) = module.kind.opt_def_id() else { return };
18481850

18491851
let mut children = Vec::new();
18501852
let mut ambig_children = Vec::new();

compiler/rustc_resolve/src/late.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
14771477
// During late resolution we only track the module component of the parent scope,
14781478
// although it may be useful to track other components as well for diagnostics.
14791479
let graph_root = resolver.graph_root;
1480-
let parent_scope = ParentScope::module(graph_root.to_module(), resolver.arenas);
1480+
let parent_scope = ParentScope::module(graph_root, resolver.arenas);
14811481
let start_rib_kind = RibKind::Module(graph_root);
14821482
LateResolutionVisitor {
14831483
r: resolver,
@@ -1942,7 +1942,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
19421942
&& let Some(ty) = &self.diag_metadata.current_self_type
19431943
&& Some(true) == self.diag_metadata.in_non_gat_assoc_type
19441944
&& let crate::ModuleKind::Def(DefKind::Trait, trait_id, _, _) =
1945-
module.kind
1945+
module.kind()
19461946
{
19471947
if def_id_matches_path(
19481948
self.r.tcx,

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ use crate::late::{
3838
};
3939
use crate::ty::fast_reject::SimplifiedType;
4040
use crate::{
41-
Finalize, Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, PathSource, Res,
42-
Resolver, ScopeSet, Segment, errors, path_names_to_string,
41+
Finalize, Module, ModuleOrUniformRoot, ParentScope, PathResult, PathSource, Res, Resolver,
42+
ScopeSet, Segment, errors, path_names_to_string,
4343
};
4444

4545
/// A field or associated item from self type suggested in case of resolution failure.
@@ -1698,7 +1698,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
16981698
&& let TyKind::Path(_, self_ty_path) = &self_ty.kind
16991699
&& let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
17001700
self.resolve_path(&Segment::from_path(self_ty_path), Some(TypeNS), None, source)
1701-
&& let ModuleKind::Def(DefKind::Trait, ..) = module.kind
1701+
&& module.def_kind() == Some(DefKind::Trait)
17021702
&& trait_ref.path.span == span
17031703
&& let PathSource::Trait(_) = source
17041704
&& let Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)) = res
@@ -3014,12 +3014,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
30143014
fn find_module(&self, def_id: DefId) -> Option<(Module<'ra>, ImportSuggestion)> {
30153015
let mut result = None;
30163016
let mut seen_modules = FxHashSet::default();
3017-
let root_did = self.r.graph_root.def_id();
3018-
let mut worklist = vec![(
3019-
self.r.graph_root.to_module(),
3020-
ThinVec::new(),
3021-
root_did.is_local() || !self.r.tcx.is_doc_hidden(root_did),
3022-
)];
3017+
let mut worklist = vec![(self.r.graph_root.to_module(), ThinVec::new(), true)];
30233018

30243019
while let Some((in_module, path_segments, doc_visible)) = worklist.pop() {
30253020
// abort if the module is already found

0 commit comments

Comments
 (0)