Skip to content

Commit 1b0ab9b

Browse files
committed
resolve: Merge NameBindingKind::Module into NameBindingKind::Res
1 parent 3c95364 commit 1b0ab9b

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
@@ -39,21 +39,6 @@ use crate::{
3939

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

42-
impl<'ra, Id: Into<DefId>> ToNameBinding<'ra>
43-
for (Module<'ra>, ty::Visibility<Id>, Span, LocalExpnId)
44-
{
45-
fn to_name_binding(self, arenas: &'ra ResolverArenas<'ra>) -> NameBinding<'ra> {
46-
arenas.alloc_name_binding(NameBindingData {
47-
kind: NameBindingKind::Module(self.0),
48-
ambiguity: None,
49-
warn_ambiguity: false,
50-
vis: self.1.to_def_id(),
51-
span: self.2,
52-
expansion: self.3,
53-
})
54-
}
55-
}
56-
5742
impl<'ra, Id: Into<DefId>> ToNameBinding<'ra> for (Res, ty::Visibility<Id>, Span, LocalExpnId) {
5843
fn to_name_binding(self, arenas: &'ra ResolverArenas<'ra>) -> NameBinding<'ra> {
5944
arenas.alloc_name_binding(NameBindingData {
@@ -121,7 +106,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
121106
if !def_id.is_local() {
122107
// Query `def_kind` is not used because query system overhead is too expensive here.
123108
let def_kind = self.cstore().def_kind_untracked(def_id);
124-
if let DefKind::Mod | DefKind::Enum | DefKind::Trait = def_kind {
109+
if def_kind.is_module_like() {
125110
let parent = self
126111
.tcx
127112
.opt_parent(def_id)
@@ -221,12 +206,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
221206
let expansion = parent_scope.expansion;
222207
// Record primary definitions.
223208
match res {
224-
Res::Def(DefKind::Mod | DefKind::Enum | DefKind::Trait, def_id) => {
225-
let module = self.expect_module(def_id);
226-
self.define(parent, ident, TypeNS, (module, vis, span, expansion));
227-
}
228209
Res::Def(
229-
DefKind::Struct
210+
DefKind::Mod
211+
| DefKind::Enum
212+
| DefKind::Trait
213+
| DefKind::Struct
230214
| DefKind::Union
231215
| DefKind::Variant
232216
| DefKind::TyAlias
@@ -773,22 +757,19 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
773757
}
774758

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

794775
// These items live in the value namespace.
@@ -811,15 +792,15 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
811792
}
812793

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

825806
// These items live in both the type and value namespaces.
@@ -927,8 +908,9 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
927908
}
928909
.map(|module| {
929910
let used = self.process_macro_use_imports(item, module);
911+
let res = module.res().unwrap();
930912
let vis = ty::Visibility::<LocalDefId>::Public;
931-
let binding = (module, vis, sp, expansion).to_name_binding(self.r.arenas);
913+
let binding = (res, vis, sp, expansion).to_name_binding(self.r.arenas);
932914
(used, Some(ModuleOrUniformRoot::Module(module)), binding)
933915
})
934916
.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
@@ -673,7 +673,6 @@ impl<'ra> Module<'ra> {
673673
}
674674
}
675675

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

@@ -874,18 +872,9 @@ struct AmbiguityError<'ra> {
874872
}
875873

876874
impl<'ra> NameBindingData<'ra> {
877-
fn module(&self) -> Option<Module<'ra>> {
878-
match self.kind {
879-
NameBindingKind::Module(module) => Some(module),
880-
NameBindingKind::Import { binding, .. } => binding.module(),
881-
_ => None,
882-
}
883-
}
884-
885875
fn res(&self) -> Res {
886876
match self.kind {
887877
NameBindingKind::Res(res) => res,
888-
NameBindingKind::Module(module) => module.res().unwrap(),
889878
NameBindingKind::Import { binding, .. } => binding.res(),
890879
}
891880
}
@@ -913,7 +902,7 @@ impl<'ra> NameBindingData<'ra> {
913902
DefKind::Variant | DefKind::Ctor(CtorOf::Variant, ..),
914903
_,
915904
)) => true,
916-
NameBindingKind::Res(..) | NameBindingKind::Module(..) => false,
905+
NameBindingKind::Res(..) => false,
917906
}
918907
}
919908

@@ -922,11 +911,7 @@ impl<'ra> NameBindingData<'ra> {
922911
NameBindingKind::Import { import, .. } => {
923912
matches!(import.kind, ImportKind::ExternCrate { .. })
924913
}
925-
NameBindingKind::Module(module)
926-
if let ModuleKind::Def(DefKind::Mod, def_id, _) = module.kind =>
927-
{
928-
def_id.is_crate_root()
929-
}
914+
NameBindingKind::Res(Res::Def(_, def_id)) => def_id.is_crate_root(),
930915
_ => false,
931916
}
932917
}
@@ -1268,7 +1253,8 @@ impl<'ra> ResolverArenas<'ra> {
12681253
if let Some(def_id) = def_id {
12691254
module_map.insert(def_id, module);
12701255
let vis = ty::Visibility::<DefId>::Public;
1271-
let binding = (module, vis, module.span, LocalExpnId::ROOT).to_name_binding(self);
1256+
let res = module.res().unwrap();
1257+
let binding = (res, vis, module.span, LocalExpnId::ROOT).to_name_binding(self);
12721258
module_self_bindings.insert(module, binding);
12731259
}
12741260
module
@@ -1816,7 +1802,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18161802
module.ensure_traits(self);
18171803
let traits = module.traits.borrow();
18181804
for (trait_name, trait_binding) in traits.as_ref().unwrap().iter() {
1819-
if self.trait_may_have_item(trait_binding.module(), assoc_item) {
1805+
let trait_module = self.get_module(trait_binding.res().def_id());
1806+
if self.trait_may_have_item(trait_module, assoc_item) {
18201807
let def_id = trait_binding.res().def_id();
18211808
let import_ids = self.find_transitive_imports(&trait_binding.kind, *trait_name);
18221809
found_traits.push(TraitCandidate { def_id, import_ids });
@@ -2152,9 +2139,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21522139
} else {
21532140
self.crate_loader(|c| c.maybe_process_path_extern(ident.name))?
21542141
};
2155-
let crate_root = self.expect_module(crate_id.as_def_id());
2142+
let res = Res::Def(DefKind::Mod, crate_id.as_def_id());
21562143
let vis = ty::Visibility::<DefId>::Public;
2157-
(crate_root, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas)
2144+
(res, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas)
21582145
})
21592146
});
21602147

0 commit comments

Comments
 (0)