Skip to content

Commit f9e62d1

Browse files
committed
resolve: Merge NameBindingKind::Module into NameBindingKind::Res
1 parent 040e2f8 commit f9e62d1

File tree

11 files changed

+61
-111
lines changed

11 files changed

+61
-111
lines changed

compiler/rustc_hir/src/def.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ impl DefKind {
295295
}
296296
}
297297

298+
/// This is a "module" in name resolution sense.
299+
#[inline]
300+
pub fn is_module_like(self) -> bool {
301+
matches!(self, DefKind::Mod | DefKind::Enum | DefKind::Trait)
302+
}
303+
298304
#[inline]
299305
pub fn is_fn_like(self) -> bool {
300306
matches!(
@@ -720,6 +726,15 @@ impl<Id> Res<Id> {
720726
}
721727
}
722728

729+
/// If this is a "module" in name resolution sense, return its `DefId`.
730+
#[inline]
731+
pub fn module_like_def_id(&self) -> Option<DefId> {
732+
match self {
733+
Res::Def(def_kind, def_id) if def_kind.is_module_like() => Some(*def_id),
734+
_ => None,
735+
}
736+
}
737+
723738
/// A human readable name for the res kind ("function", "module", etc.).
724739
pub fn descr(&self) -> &'static str {
725740
match *self {

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::mem;
33
use std::sync::Arc;
44

55
use rustc_attr_data_structures::Deprecation;
6-
use rustc_hir::def::{CtorKind, DefKind, Res};
6+
use rustc_hir::def::{CtorKind, DefKind};
77
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE};
88
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
99
use rustc_middle::arena::ArenaAllocatable;
@@ -510,10 +510,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
510510
}
511511
Entry::Vacant(entry) => {
512512
entry.insert(parent);
513-
if matches!(
514-
child.res,
515-
Res::Def(DefKind::Mod | DefKind::Enum | DefKind::Trait, _)
516-
) {
513+
if child.res.module_like_def_id().is_some() {
517514
bfs_queue.push_back(def_id);
518515
}
519516
}

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3453,9 +3453,7 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
34533453
collect_fn(&child.ident, ns, def_id);
34543454
}
34553455

3456-
if matches!(defkind, DefKind::Mod | DefKind::Enum | DefKind::Trait)
3457-
&& seen_defs.insert(def_id)
3458-
{
3456+
if defkind.is_module_like() && seen_defs.insert(def_id) {
34593457
queue.push(def_id);
34603458
}
34613459
}

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,6 @@ use crate::{
4040

4141
type Res = def::Res<NodeId>;
4242

43-
impl<'ra, Id: Into<DefId>> ToNameBinding<'ra>
44-
for (Module<'ra>, ty::Visibility<Id>, Span, LocalExpnId)
45-
{
46-
fn to_name_binding(self, arenas: &'ra ResolverArenas<'ra>) -> NameBinding<'ra> {
47-
arenas.alloc_name_binding(NameBindingData {
48-
kind: NameBindingKind::Module(self.0),
49-
ambiguity: None,
50-
warn_ambiguity: false,
51-
vis: self.1.to_def_id(),
52-
span: self.2,
53-
expansion: self.3,
54-
})
55-
}
56-
}
57-
5843
impl<'ra, Id: Into<DefId>> ToNameBinding<'ra> for (Res, ty::Visibility<Id>, Span, LocalExpnId) {
5944
fn to_name_binding(self, arenas: &'ra ResolverArenas<'ra>) -> NameBinding<'ra> {
6045
arenas.alloc_name_binding(NameBindingData {
@@ -122,7 +107,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
122107
if !def_id.is_local() {
123108
// Query `def_kind` is not used because query system overhead is too expensive here.
124109
let def_kind = self.cstore().def_kind_untracked(def_id);
125-
if let DefKind::Mod | DefKind::Enum | DefKind::Trait = def_kind {
110+
if def_kind.is_module_like() {
126111
let parent = self
127112
.tcx
128113
.opt_parent(def_id)
@@ -222,12 +207,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
222207
let expansion = parent_scope.expansion;
223208
// Record primary definitions.
224209
match res {
225-
Res::Def(DefKind::Mod | DefKind::Enum | DefKind::Trait, def_id) => {
226-
let module = self.expect_module(def_id);
227-
self.define(parent, ident, TypeNS, (module, vis, span, expansion));
228-
}
229210
Res::Def(
230-
DefKind::Struct
211+
DefKind::Mod
212+
| DefKind::Enum
213+
| DefKind::Trait
214+
| DefKind::Struct
231215
| DefKind::Union
232216
| DefKind::Variant
233217
| DefKind::TyAlias
@@ -774,22 +758,19 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
774758
}
775759

776760
ItemKind::Mod(_, ident, ref mod_kind) => {
777-
let module = self.r.new_module(
761+
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
762+
763+
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
764+
self.r.mods_with_parse_errors.insert(def_id);
765+
}
766+
self.parent_scope.module = self.r.new_module(
778767
Some(parent),
779768
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
780769
expansion.to_expn_id(),
781770
item.span,
782771
parent.no_implicit_prelude
783772
|| ast::attr::contains_name(&item.attrs, sym::no_implicit_prelude),
784773
);
785-
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
786-
787-
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
788-
self.r.mods_with_parse_errors.insert(def_id);
789-
}
790-
791-
// Descend into the module.
792-
self.parent_scope.module = module;
793774
}
794775

795776
// These items live in the value namespace.
@@ -812,15 +793,15 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
812793
}
813794

814795
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => {
815-
let module = self.r.new_module(
796+
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
797+
798+
self.parent_scope.module = self.r.new_module(
816799
Some(parent),
817800
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
818801
expansion.to_expn_id(),
819802
item.span,
820803
parent.no_implicit_prelude,
821804
);
822-
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
823-
self.parent_scope.module = module;
824805
}
825806

826807
// These items live in both the type and value namespaces.
@@ -928,8 +909,9 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
928909
}
929910
.map(|module| {
930911
let used = self.process_macro_use_imports(item, module);
912+
let res = module.res().unwrap();
931913
let vis = ty::Visibility::<LocalDefId>::Public;
932-
let binding = (module, vis, sp, expansion).to_name_binding(self.r.arenas);
914+
let binding = (res, vis, sp, expansion).to_name_binding(self.r.arenas);
933915
(used, Some(ModuleOrUniformRoot::Module(module)), binding)
934916
})
935917
.unwrap_or((true, None, self.r.dummy_binding));

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
233233
return;
234234
}
235235

236-
let old_kind = match (ns, old_binding.module()) {
236+
let old_kind = match (ns, old_binding.res()) {
237237
(ValueNS, _) => "value",
238238
(MacroNS, _) => "macro",
239239
(TypeNS, _) if old_binding.is_extern_crate() => "extern crate",
240-
(TypeNS, Some(module)) if module.is_normal() => "module",
241-
(TypeNS, Some(module)) if module.is_trait() => "trait",
240+
(TypeNS, Res::Def(DefKind::Mod, _)) => "module",
241+
(TypeNS, Res::Def(DefKind::Trait, _)) => "trait",
242242
(TypeNS, _) => "type",
243243
};
244244

@@ -1320,7 +1320,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13201320
}
13211321

13221322
// collect submodules to explore
1323-
if let Some(module) = name_binding.module() {
1323+
if let Some(def_id) = name_binding.res().module_like_def_id() {
13241324
// form the path
13251325
let mut path_segments = path_segments.clone();
13261326
path_segments.push(ast::PathSegment::from_ident(ident));
@@ -1340,14 +1340,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13401340

13411341
if !is_extern_crate_that_also_appears_in_prelude || alias_import {
13421342
// add the module to the lookup
1343-
if seen_modules.insert(module.def_id()) {
1343+
if seen_modules.insert(def_id) {
13441344
if via_import { &mut worklist_via_import } else { &mut worklist }.push(
13451345
(
1346-
module,
1346+
this.expect_module(def_id),
13471347
path_segments,
13481348
child_accessible,
13491349
child_doc_visible,
1350-
is_stable && this.is_stable(module.def_id(), name_binding.span),
1350+
is_stable && this.is_stable(def_id, name_binding.span),
13511351
),
13521352
);
13531353
}
@@ -2090,7 +2090,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20902090
true, // re-export
20912091
));
20922092
}
2093-
NameBindingKind::Res(_) | NameBindingKind::Module(_) => {}
2093+
NameBindingKind::Res(_) => {}
20942094
}
20952095
let first = binding == first_binding;
20962096
let def_span = self.tcx.sess.source_map().guess_head_span(binding.span);
@@ -2302,25 +2302,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23022302
.ok()
23032303
};
23042304
if let Some(binding) = binding {
2305-
let mut found = |what| {
2306-
msg = format!(
2307-
"expected {}, found {} `{}` in {}",
2308-
ns.descr(),
2309-
what,
2310-
ident,
2311-
parent
2312-
)
2313-
};
2314-
if binding.module().is_some() {
2315-
found("module")
2316-
} else {
2317-
match binding.res() {
2318-
// Avoid using TyCtxt::def_kind_descr in the resolver, because it
2319-
// indirectly *calls* the resolver, and would cause a query cycle.
2320-
Res::Def(kind, id) => found(kind.descr(id)),
2321-
_ => found(ns_to_try.descr()),
2322-
}
2323-
}
2305+
msg = format!(
2306+
"expected {}, found {} `{ident}` in {parent}",
2307+
ns.descr(),
2308+
binding.res().descr(),
2309+
);
23242310
};
23252311
}
23262312
(msg, None)

compiler/rustc_resolve/src/ident.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,11 +1613,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16131613
}
16141614

16151615
let maybe_assoc = opt_ns != Some(MacroNS) && PathSource::Type.is_expected(res);
1616-
if let Some(next_module) = binding.module() {
1617-
if self.mods_with_parse_errors.contains(&next_module.def_id()) {
1616+
if let Some(def_id) = binding.res().module_like_def_id() {
1617+
if self.mods_with_parse_errors.contains(&def_id) {
16181618
module_had_parse_errors = true;
16191619
}
1620-
module = Some(ModuleOrUniformRoot::Module(next_module));
1620+
module = Some(ModuleOrUniformRoot::Module(self.expect_module(def_id)));
16211621
record_segment_res(self, res);
16221622
} else if res == Res::ToolMod && !is_last && opt_ns.is_some() {
16231623
if binding.is_import() {

compiler/rustc_resolve/src/imports.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
663663
NameBindingKind::Res(res) => {
664664
Some(self.def_id_to_node_id(res.def_id().expect_local()))
665665
}
666-
NameBindingKind::Module(module) => {
667-
Some(self.def_id_to_node_id(module.def_id().expect_local()))
668-
}
669666
NameBindingKind::Import { import, .. } => import.id(),
670667
};
671668
if let Some(binding_id) = binding_id {

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,18 +2644,17 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26442644
if result.is_some() || !name_binding.vis.is_visible_locally() {
26452645
return;
26462646
}
2647-
if let Some(module) = name_binding.module() {
2647+
if let Some(module_def_id) = name_binding.res().module_like_def_id() {
26482648
// form the path
26492649
let mut path_segments = path_segments.clone();
26502650
path_segments.push(ast::PathSegment::from_ident(ident));
2651-
let module_def_id = module.def_id();
26522651
let doc_visible = doc_visible
26532652
&& (module_def_id.is_local() || !r.tcx.is_doc_hidden(module_def_id));
26542653
if module_def_id == def_id {
26552654
let path =
26562655
Path { span: name_binding.span, segments: path_segments, tokens: None };
26572656
result = Some((
2658-
module,
2657+
r.expect_module(module_def_id),
26592658
ImportSuggestion {
26602659
did: Some(def_id),
26612660
descr: "module",
@@ -2670,6 +2669,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26702669
} else {
26712670
// add the module to the lookup
26722671
if seen_modules.insert(module_def_id) {
2672+
let module = r.expect_module(module_def_id);
26732673
worklist.push((module, path_segments, doc_visible));
26742674
}
26752675
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,6 @@ impl<'ra> Module<'ra> {
674674
}
675675
}
676676

677-
// Public for rustdoc.
678677
fn def_id(self) -> DefId {
679678
self.opt_def_id().expect("`ModuleData::def_id` is called on a block module")
680679
}
@@ -782,7 +781,6 @@ impl<'ra> ToNameBinding<'ra> for NameBinding<'ra> {
782781
#[derive(Clone, Copy, Debug)]
783782
enum NameBindingKind<'ra> {
784783
Res(Res),
785-
Module(Module<'ra>),
786784
Import { binding: NameBinding<'ra>, import: Import<'ra> },
787785
}
788786

@@ -875,18 +873,9 @@ struct AmbiguityError<'ra> {
875873
}
876874

877875
impl<'ra> NameBindingData<'ra> {
878-
fn module(&self) -> Option<Module<'ra>> {
879-
match self.kind {
880-
NameBindingKind::Module(module) => Some(module),
881-
NameBindingKind::Import { binding, .. } => binding.module(),
882-
_ => None,
883-
}
884-
}
885-
886876
fn res(&self) -> Res {
887877
match self.kind {
888878
NameBindingKind::Res(res) => res,
889-
NameBindingKind::Module(module) => module.res().unwrap(),
890879
NameBindingKind::Import { binding, .. } => binding.res(),
891880
}
892881
}
@@ -914,7 +903,7 @@ impl<'ra> NameBindingData<'ra> {
914903
DefKind::Variant | DefKind::Ctor(CtorOf::Variant, ..),
915904
_,
916905
)) => true,
917-
NameBindingKind::Res(..) | NameBindingKind::Module(..) => false,
906+
NameBindingKind::Res(..) => false,
918907
}
919908
}
920909

@@ -923,11 +912,7 @@ impl<'ra> NameBindingData<'ra> {
923912
NameBindingKind::Import { import, .. } => {
924913
matches!(import.kind, ImportKind::ExternCrate { .. })
925914
}
926-
NameBindingKind::Module(module)
927-
if let ModuleKind::Def(DefKind::Mod, def_id, _) = module.kind =>
928-
{
929-
def_id.is_crate_root()
930-
}
915+
NameBindingKind::Res(Res::Def(_, def_id)) => def_id.is_crate_root(),
931916
_ => false,
932917
}
933918
}
@@ -1269,7 +1254,8 @@ impl<'ra> ResolverArenas<'ra> {
12691254
if let Some(def_id) = def_id {
12701255
module_map.insert(def_id, module);
12711256
let vis = ty::Visibility::<DefId>::Public;
1272-
let binding = (module, vis, module.span, LocalExpnId::ROOT).to_name_binding(self);
1257+
let res = module.res().unwrap();
1258+
let binding = (res, vis, module.span, LocalExpnId::ROOT).to_name_binding(self);
12731259
module_self_bindings.insert(module, binding);
12741260
}
12751261
module
@@ -1817,7 +1803,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18171803
module.ensure_traits(self);
18181804
let traits = module.traits.borrow();
18191805
for (trait_name, trait_binding) in traits.as_ref().unwrap().iter() {
1820-
if self.trait_may_have_item(trait_binding.module(), assoc_item) {
1806+
let trait_module = self.get_module(trait_binding.res().def_id());
1807+
if self.trait_may_have_item(trait_module, assoc_item) {
18211808
let def_id = trait_binding.res().def_id();
18221809
let import_ids = self.find_transitive_imports(&trait_binding.kind, *trait_name);
18231810
found_traits.push(TraitCandidate { def_id, import_ids });
@@ -2153,9 +2140,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21532140
} else {
21542141
self.crate_loader(|c| c.maybe_process_path_extern(ident.name))?
21552142
};
2156-
let crate_root = self.expect_module(crate_id.as_def_id());
2143+
let res = Res::Def(DefKind::Mod, crate_id.as_def_id());
21572144
let vis = ty::Visibility::<DefId>::Public;
2158-
(crate_root, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas)
2145+
(res, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas)
21592146
})
21602147
});
21612148

0 commit comments

Comments
 (0)