Skip to content

Commit 9c3c4b9

Browse files
committed
resolve: Move kind to (Local,Extern)ModuleData
1 parent ef2a512 commit 9c3c4b9

8 files changed

Lines changed: 140 additions & 119 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 7 additions & 11 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)

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 9 additions & 9 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
@@ -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}")

compiler/rustc_resolve/src/ident.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ 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 {
250+
if let ModuleKind::Block = module.kind() {
251251
return Some((module.parent().unwrap().nearest_item_scope(), None));
252252
}
253253

@@ -643,24 +643,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
643643
Err(ControlFlow::Break(..)) => return decl,
644644
}
645645
}
646-
Scope::ModuleGlobs(module, _) if !module.kind.is_local() => {
646+
Scope::ModuleGlobs(Module::Extern(_), _) => {
647647
// Fast path: external module decoding only creates non-glob declarations.
648648
Err(Determined)
649649
}
650-
Scope::ModuleGlobs(module, derive_fallback_lint_id) => {
650+
Scope::ModuleGlobs(Module::Local(module), derive_fallback_lint_id) => {
651651
let (adjusted_parent_scope, adjusted_finalize) = if matches!(
652652
scope_set,
653653
ScopeSet::Module(..) | ScopeSet::ModuleAndExternPrelude(..)
654654
) {
655655
(parent_scope, finalize)
656656
} else {
657657
(
658-
&ParentScope { module, ..*parent_scope },
658+
&ParentScope { module: module.to_module(), ..*parent_scope },
659659
finalize.map(|f| Finalize { used: Used::Scope, ..f }),
660660
)
661661
};
662662
let binding = self.reborrow().resolve_ident_in_module_globs_unadjusted(
663-
module.expect_local(),
663+
module,
664664
ident,
665665
orig_ident_span,
666666
ns,

compiler/rustc_resolve/src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18461846
// Since import resolution is finished, globs will not define any more names.
18471847
*module.globs.borrow_mut(self) = Vec::new();
18481848

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

18511851
let mut children = Vec::new();
18521852
let mut ambig_children = Vec::new();

compiler/rustc_resolve/src/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)