Skip to content

Commit 532e78b

Browse files
committed
WIP
1 parent 2aabf3c commit 532e78b

5 files changed

Lines changed: 69 additions & 69 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 7 additions & 13 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,7 @@ 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+
ModuleKind::Extern(def_kind, def_id, self.tcx.item_name(def_id)),
177171
expn_id,
178172
self.def_span(def_id),
179173
// FIXME: Account for `#[no_implicit_prelude]` attributes.
@@ -253,11 +247,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
253247
match vis.kind {
254248
ast::VisibilityKind::Public => Ok(Visibility::Public),
255249
ast::VisibilityKind::Inherited => {
256-
Ok(match parent_scope.module.expect_local().kind {
250+
Ok(match parent_scope.module.expect_local().def() {
257251
// Any inherited visibility resolved directly inside an enum or trait
258252
// (i.e. variants, fields, and trait items) inherits from the visibility
259253
// of the enum or trait.
260-
ModuleKind::Def(DefKind::Enum | DefKind::Trait, def_id, _, _) => {
254+
Some((DefKind::Enum | DefKind::Trait, def_id)) => {
261255
self.tcx.visibility(def_id).expect_local()
262256
}
263257
// Otherwise, the visibility is restricted to the nearest parent `mod` item.
@@ -856,7 +850,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
856850
}
857851
let module = self.r.new_local_module(
858852
Some(parent),
859-
ModuleKind::Def(def_kind, def_id, item.id, Some(ident.name)),
853+
ModuleKind::Local(def_kind, local_def_id, item.id, Some(ident.name)),
860854
expansion.to_expn_id(),
861855
item.span,
862856
parent.no_implicit_prelude
@@ -890,7 +884,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
890884

891885
let module = self.r.new_local_module(
892886
Some(parent),
893-
ModuleKind::Def(def_kind, def_id, item.id, Some(ident.name)),
887+
ModuleKind::Local(def_kind, local_def_id, item.id, Some(ident.name)),
894888
expansion.to_expn_id(),
895889
item.span,
896890
parent.no_implicit_prelude,

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ use crate::late::{DiagMetadata, PatternSource, Rib};
5151
use crate::{
5252
AmbiguityError, AmbiguityKind, AmbiguityWarning, BindingError, BindingKey, Decl, DeclKind,
5353
DelayedVisResolutionError, Finalize, ForwardGenericParamBanReason, HasGenericParams, IdentKey,
54-
LateDecl, MacroRulesScope, Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult,
55-
PrivacyError, Res, ResolutionError, Resolver, Scope, ScopeSet, Segment, UseError, Used,
56-
VisResolutionError, errors as errs, path_names_to_string,
54+
LateDecl, MacroRulesScope, Module, ModuleOrUniformRoot, ParentScope, PathResult, PrivacyError,
55+
Res, ResolutionError, Resolver, Scope, ScopeSet, Segment, UseError, Used, VisResolutionError,
56+
errors as errs, path_names_to_string,
5757
};
5858

5959
/// A vector of spans and replacements, a message and applicability.
@@ -240,11 +240,11 @@ 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().expect_local().kind {
243+
let container = match old_binding.parent_module.unwrap().expect_local().def() {
244244
// Avoid using TyCtxt::def_kind_descr in the resolver, because it
245245
// indirectly *calls* the resolver, and would cause a query cycle.
246-
ModuleKind::Def(kind, def_id, _, _) => kind.descr(def_id),
247-
ModuleKind::Block => "block",
246+
Some((def_kind, def_id)) => def_kind.descr(def_id),
247+
None => "block",
248248
};
249249

250250
let (name, span) =
@@ -1765,7 +1765,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17651765
}
17661766

17671767
if ident.name == kw::Default
1768-
&& let ModuleKind::Def(DefKind::Enum, def_id, _, _) = parent_scope.module.kind
1768+
&& let Some((DefKind::Enum, def_id)) = parent_scope.module.def()
17691769
{
17701770
let span = self.def_span(def_id);
17711771
let source_map = self.tcx.sess.source_map();
@@ -1893,19 +1893,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18931893
missing a `derive` attribute",
18941894
ident.name,
18951895
);
1896-
let sugg_span =
1897-
if let ModuleKind::Def(DefKind::Enum, id, _, _) = parent_scope.module.kind {
1898-
let span = self.def_span(id);
1899-
if span.from_expansion() {
1900-
None
1901-
} else {
1902-
// For enum variants sugg_span is empty but we can get the enum's Span.
1903-
Some(span.shrink_to_lo())
1904-
}
1896+
let sugg_span = if let Some((DefKind::Enum, id)) = parent_scope.module.def() {
1897+
let span = self.def_span(id);
1898+
if span.from_expansion() {
1899+
None
19051900
} else {
1906-
// For items this `Span` will be populated, everything else it'll be None.
1907-
sugg_span
1908-
};
1901+
// For enum variants sugg_span is empty but we can get the enum's Span.
1902+
Some(span.shrink_to_lo())
1903+
}
1904+
} else {
1905+
// For items this `Span` will be populated, everything else it'll be None.
1906+
sugg_span
1907+
};
19091908
match sugg_span {
19101909
Some(span) => {
19111910
err.span_suggestion_verbose(

compiler/rustc_resolve/src/late.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,8 +1941,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
19411941
&& let Some((module, _)) = &self.current_trait_ref
19421942
&& let Some(ty) = &self.diag_metadata.current_self_type
19431943
&& Some(true) == self.diag_metadata.in_non_gat_assoc_type
1944-
&& let crate::ModuleKind::Def(DefKind::Trait, trait_id, _, _) =
1945-
module.kind
1944+
&& let Some((DefKind::Trait, trait_id)) = module.def()
19461945
{
19471946
if def_id_matches_path(
19481947
self.r.tcx,

compiler/rustc_resolve/src/lib.rs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ enum ModuleKind {
533533
/// }
534534
/// ```
535535
Block,
536-
/// Any module with a name.
536+
/// A local non-block module.
537537
///
538538
/// This could be:
539539
///
@@ -542,13 +542,16 @@ enum ModuleKind {
542542
/// The crate root will have `None` for the symbol.
543543
/// * A trait or an enum (it implicitly contains associated types, methods and variant
544544
/// constructors).
545-
Def(DefKind, DefId, NodeId, Option<Symbol>),
545+
Local(DefKind, LocalDefId, NodeId, Option<Symbol>),
546+
/// An external module, non-block by definition.
547+
Extern(DefKind, DefId, Symbol),
546548
}
547549

548550
impl ModuleKind {
549551
fn opt_def_id(&self) -> Option<DefId> {
550552
match self {
551-
ModuleKind::Def(_, def_id, _, _) => Some(*def_id),
553+
ModuleKind::Local(_, def_id, _, _) => Some(def_id.to_def_id()),
554+
ModuleKind::Extern(_, def_id, _) => Some(*def_id),
552555
_ => None,
553556
}
554557
}
@@ -557,12 +560,17 @@ impl ModuleKind {
557560
self.opt_def_id().expect("`Module::def_id` is called on a block module")
558561
}
559562

560-
fn is_local(&self) -> bool {
563+
fn def(&self) -> Option<(DefKind, DefId)> {
561564
match self {
562-
ModuleKind::Def(_, def_id, ..) => def_id.is_local(),
563-
ModuleKind::Block => true,
565+
ModuleKind::Local(def_kind, def_id, _, _) => Some((*def_kind, def_id.to_def_id())),
566+
ModuleKind::Extern(def_kind, def_id, _) => Some((*def_kind, *def_id)),
567+
ModuleKind::Block => None,
564568
}
565569
}
570+
571+
fn is_local(&self) -> bool {
572+
matches!(self, ModuleKind::Local(..) | ModuleKind::Block)
573+
}
566574
}
567575

568576
/// Combination of a symbol and its macros 2.0 normalized hygiene context.
@@ -729,13 +737,10 @@ impl<'ra> ModuleData<'ra> {
729737
arenas: &'ra ResolverArenas<'ra>,
730738
) -> Self {
731739
let is_foreign = !kind.is_local();
732-
let self_decl = match kind {
733-
ModuleKind::Def(def_kind, def_id, ..) => {
734-
let expn_id = expansion.as_local().unwrap_or(LocalExpnId::ROOT);
735-
Some(arenas.new_def_decl(Res::Def(def_kind, def_id), vis, span, expn_id, parent))
736-
}
737-
ModuleKind::Block => None,
738-
};
740+
let self_decl = kind.def().map(|(def_kind, def_id)| {
741+
let expn_id = expansion.as_local().unwrap_or(LocalExpnId::ROOT);
742+
arenas.new_def_decl(Res::Def(def_kind, def_id), vis, span, expn_id, parent)
743+
});
739744
ModuleData {
740745
parent,
741746
kind,
@@ -757,7 +762,8 @@ impl<'ra> ModuleData<'ra> {
757762
fn name(&self) -> Option<Symbol> {
758763
match self.kind {
759764
ModuleKind::Block => None,
760-
ModuleKind::Def(.., name) => name,
765+
ModuleKind::Local(.., name) => name,
766+
ModuleKind::Extern(.., name) => Some(name),
761767
}
762768
}
763769

@@ -769,6 +775,10 @@ impl<'ra> ModuleData<'ra> {
769775
self.kind.def_id()
770776
}
771777

778+
fn def(&self) -> Option<(DefKind, DefId)> {
779+
self.kind.def()
780+
}
781+
772782
fn is_local(&self) -> bool {
773783
self.kind.is_local()
774784
}
@@ -779,14 +789,15 @@ impl<'ra> ModuleData<'ra> {
779789

780790
fn res(&self) -> Option<Res> {
781791
match self.kind {
782-
ModuleKind::Def(kind, def_id, _, _) => Some(Res::Def(kind, def_id)),
792+
ModuleKind::Local(kind, def_id, _, _) => Some(Res::Def(kind, def_id.to_def_id())),
793+
ModuleKind::Extern(kind, def_id, _) => Some(Res::Def(kind, def_id)),
783794
_ => None,
784795
}
785796
}
786797

787798
fn def_kind(&self) -> Option<DefKind> {
788799
match self.kind {
789-
ModuleKind::Def(def_kind, ..) => Some(def_kind),
800+
ModuleKind::Local(def_kind, ..) | ModuleKind::Extern(def_kind, ..) => Some(def_kind),
790801
ModuleKind::Block => None,
791802
}
792803
}
@@ -863,7 +874,8 @@ impl<'ra> Module<'ra> {
863874
/// This may be the crate root.
864875
fn nearest_parent_mod(self) -> DefId {
865876
match self.kind {
866-
ModuleKind::Def(DefKind::Mod, def_id, _, _) => def_id,
877+
ModuleKind::Local(DefKind::Mod, def_id, _, _) => def_id.to_def_id(),
878+
ModuleKind::Extern(DefKind::Mod, def_id, _) => def_id,
867879
_ => self.parent.expect("non-root module without parent").nearest_parent_mod(),
868880
}
869881
}
@@ -872,7 +884,8 @@ impl<'ra> Module<'ra> {
872884
/// This may be the crate root.
873885
fn nearest_parent_mod_node_id(self) -> NodeId {
874886
match self.kind {
875-
ModuleKind::Def(DefKind::Mod, _, node_id, _) => node_id,
887+
ModuleKind::Local(DefKind::Mod, _, node_id, _) => node_id,
888+
ModuleKind::Extern(..) => ast::DUMMY_NODE_ID,
876889
_ => self.parent.expect("non-root module without parent").nearest_parent_mod_node_id(),
877890
}
878891
}
@@ -891,20 +904,16 @@ impl<'ra> Module<'ra> {
891904
#[track_caller]
892905
fn expect_local(self) -> LocalModule<'ra> {
893906
match self.kind {
894-
ModuleKind::Def(_, def_id, _, _) if !def_id.is_local() => {
895-
span_bug!(self.span, "unexpected extern module: {self:?}")
896-
}
897-
ModuleKind::Def(..) | ModuleKind::Block => LocalModule(self.0),
907+
ModuleKind::Local(..) | ModuleKind::Block => LocalModule(self.0),
908+
_ => span_bug!(self.span, "unexpected extern module: {self:?}"),
898909
}
899910
}
900911

901912
#[track_caller]
902913
fn expect_extern(self) -> ExternModule<'ra> {
903914
match self.kind {
904-
ModuleKind::Def(_, def_id, _, _) if !def_id.is_local() => ExternModule(self.0),
905-
ModuleKind::Def(..) | ModuleKind::Block => {
906-
span_bug!(self.span, "unexpected local module: {self:?}")
907-
}
915+
ModuleKind::Extern(..) => ExternModule(self.0),
916+
_ => span_bug!(self.span, "unexpected local module: {self:?}"),
908917
}
909918
}
910919
}
@@ -1757,10 +1766,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17571766
current_crate_outer_attr_insert_span: Span,
17581767
arenas: &'ra ResolverArenas<'ra>,
17591768
) -> Resolver<'ra, 'tcx> {
1760-
let root_def_id = CRATE_DEF_ID.to_def_id();
17611769
let graph_root = LocalModule::new(
17621770
None,
1763-
ModuleKind::Def(DefKind::Mod, root_def_id, CRATE_NODE_ID, None),
1771+
ModuleKind::Local(DefKind::Mod, CRATE_DEF_ID, CRATE_NODE_ID, None),
17641772
Visibility::Public,
17651773
ExpnId::root(),
17661774
crate_span,
@@ -1771,7 +1779,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17711779
let local_module_map = FxIndexMap::from_iter([(CRATE_DEF_ID, graph_root)]);
17721780
let empty_module = LocalModule::new(
17731781
None,
1774-
ModuleKind::Def(DefKind::Mod, root_def_id, CRATE_NODE_ID, None),
1782+
ModuleKind::Local(DefKind::Mod, CRATE_DEF_ID, CRATE_NODE_ID, None),
17751783
Visibility::Public,
17761784
ExpnId::root(),
17771785
DUMMY_SP,

compiler/rustc_resolve/src/macros.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ use crate::hygiene::Macros20NormalizedSyntaxContext;
4343
use crate::imports::Import;
4444
use crate::{
4545
BindingKey, CacheCell, CmResolver, Decl, DeclKind, DeriveData, Determinacy, Finalize, IdentKey,
46-
InvocationParent, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, Res,
47-
ResolutionError, Resolver, ScopeSet, Segment, Used,
46+
InvocationParent, ModuleOrUniformRoot, ParentScope, PathResult, Res, ResolutionError, Resolver,
47+
ScopeSet, Segment, Used,
4848
};
4949

5050
/// Name declaration produced by a `macro_rules` item definition.
@@ -1181,15 +1181,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11811181
// Silence `unused_imports` on the fallback import as well.
11821182
self.get_mut().record_use(ident, fallback_binding, Used::Other);
11831183
} else {
1184-
let location = match parent_scope.module.kind {
1185-
ModuleKind::Def(kind, def_id, _, name) => {
1186-
if let Some(name) = name {
1184+
let location = match parent_scope.module.def() {
1185+
Some((kind, def_id)) => {
1186+
if let Some(name) = parent_scope.module.name() {
11871187
format!("{} `{name}`", kind.descr(def_id))
11881188
} else {
11891189
"the crate root".to_string()
11901190
}
11911191
}
1192-
ModuleKind::Block => "this scope".to_string(),
1192+
None => "this scope".to_string(),
11931193
};
11941194
self.tcx.sess.psess.buffer_lint(
11951195
OUT_OF_SCOPE_MACRO_CALLS,

0 commit comments

Comments
 (0)