Skip to content

Commit b318f1f

Browse files
Rollup merge of rust-lang#156406 - petrochenkov:modata2, r=nnethercote
resolve: Module-related refactorings Extracted parts of rust-lang#156362 that don't require splitting `(Local,Extern)ModuleData` into separate data structures. - Some `expect_local` assertions are added - Methods that need to exist on all of `Module` and `(Local,Extern)Module` are implemented for `ModuleData` - Methods that need to exist on `ModuleKind` are moved to `ModuleKind` - Some unnecessary complicated logic using `graph_root` is simplified - `glob_importers` are filled and used only for local modules. - Some unnecessary logic is skipped for extern modules in `resolve_ident_in_module_non_globs_unadjusted` - Module construction functions are cleaned up - `module_to_string` is simplified r? @nnethercote
2 parents 0bae0be + a07cc54 commit b318f1f

8 files changed

Lines changed: 164 additions & 143 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
253253
match vis.kind {
254254
ast::VisibilityKind::Public => Ok(Visibility::Public),
255255
ast::VisibilityKind::Inherited => {
256-
Ok(match parent_scope.module.kind {
256+
Ok(match parent_scope.module.expect_local().kind {
257257
// Any inherited visibility resolved directly inside an enum or trait
258258
// (i.e. variants, fields, and trait items) inherits from the visibility
259259
// of the enum or trait.
@@ -535,7 +535,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
535535
root_id: NodeId,
536536
vis: Visibility,
537537
) {
538-
let current_module = self.parent_scope.module;
538+
let current_module = self.parent_scope.module.expect_local();
539539
let import = self.r.arenas.alloc_import(ImportData {
540540
kind,
541541
parent_scope: self.parent_scope,
@@ -560,7 +560,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
560560
if target.name != kw::Underscore {
561561
self.r.per_ns(|this, ns| {
562562
let key = BindingKey::new(IdentKey::new(target), ns);
563-
this.resolution_or_default(current_module, key, target.span)
563+
this.resolution_or_default(current_module.to_module(), key, target.span)
564564
.borrow_mut(this)
565565
.single_imports
566566
.insert(import);
@@ -1136,7 +1136,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
11361136
if let Some(Attribute::Parsed(AttributeKind::MacroUse { span, arguments })) =
11371137
AttributeParser::parse_limited(self.r.tcx.sess, &item.attrs, &[sym::macro_use])
11381138
{
1139-
if self.parent_scope.module.parent.is_some() {
1139+
if self.parent_scope.module.expect_local().parent.is_some() {
11401140
self.r
11411141
.dcx()
11421142
.emit_err(errors::ExternCrateLoadingMacroNotAtCrateRoot { span: item.span });
@@ -1247,7 +1247,8 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
12471247
/// directly into its parent scope's module.
12481248
pub(crate) fn visit_invoc_in_module(&mut self, id: NodeId) -> MacroRulesScopeRef<'ra> {
12491249
let invoc_id = self.visit_invoc(id);
1250-
self.parent_scope.module.unexpanded_invocations.borrow_mut(self.r).insert(invoc_id);
1250+
let module = self.parent_scope.module.expect_local();
1251+
module.unexpanded_invocations.borrow_mut(self.r).insert(invoc_id);
12511252
self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Invocation(invoc_id))
12521253
}
12531254

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 22 additions & 19 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),
@@ -2597,7 +2597,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
25972597
let module = module.to_module();
25982598
current_module.is_ancestor_of(module) && current_module != module
25992599
})
2600-
.flat_map(|(_, module)| module.kind.name()),
2600+
.flat_map(|(_, module)| module.name()),
26012601
)
26022602
.chain(
26032603
self.extern_module_map
@@ -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+
.flat_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
@@ -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,23 @@ 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+
let m = r.expect_module(parent_module);
3295+
if m.is_local() {
3296+
for importer in m.glob_importers.borrow().iter() {
3297+
if let Some(next_parent_module) = importer.parent_scope.module.opt_def_id()
33043298
{
3305-
res = true;
3306-
break;
3299+
if next_parent_module == module
3300+
|| comes_from_same_module_for_glob(
3301+
r,
3302+
next_parent_module,
3303+
module,
3304+
visited,
3305+
)
3306+
{
3307+
res = true;
3308+
break;
3309+
}
33073310
}
33083311
}
33093312
}

compiler/rustc_resolve/src/ident.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -643,10 +643,7 @@ 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, _) if !module.is_local() => {
650647
// Fast path: external module decoding only creates non-glob declarations.
651648
Err(Determined)
652649
}
@@ -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 => {
@@ -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+
if !module.is_local() {
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: 3 additions & 1 deletion
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 module.is_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.

compiler/rustc_resolve/src/late.rs

Lines changed: 1 addition & 1 deletion
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,

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)