Skip to content

Commit babe2c0

Browse files
committed
resolve: Merge NameBindingKind::Module into NameBindingKind::Res
1 parent 3014e79 commit babe2c0

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
@@ -3454,9 +3454,7 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
34543454
collect_fn(&child.ident, ns, def_id);
34553455
}
34563456

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

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)
@@ -223,12 +208,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
223208
let expansion = parent_scope.expansion;
224209
// Record primary definitions.
225210
match res {
226-
Res::Def(DefKind::Mod | DefKind::Enum | DefKind::Trait, def_id) => {
227-
let module = self.expect_module(def_id);
228-
self.define(parent, ident, TypeNS, (module, vis, span, expansion));
229-
}
230211
Res::Def(
231-
DefKind::Struct
212+
DefKind::Mod
213+
| DefKind::Enum
214+
| DefKind::Trait
215+
| DefKind::Struct
232216
| DefKind::Union
233217
| DefKind::Variant
234218
| DefKind::TyAlias
@@ -766,22 +750,19 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
766750
}
767751

768752
ItemKind::Mod(_, ident, ref mod_kind) => {
769-
let module = self.r.new_module(
753+
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
754+
755+
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
756+
self.r.mods_with_parse_errors.insert(def_id);
757+
}
758+
self.parent_scope.module = self.r.new_module(
770759
Some(parent),
771760
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
772761
expansion.to_expn_id(),
773762
item.span,
774763
parent.no_implicit_prelude
775764
|| ast::attr::contains_name(&item.attrs, sym::no_implicit_prelude),
776765
);
777-
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
778-
779-
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
780-
self.r.mods_with_parse_errors.insert(def_id);
781-
}
782-
783-
// Descend into the module.
784-
self.parent_scope.module = module;
785766
}
786767

787768
// These items live in the value namespace.
@@ -804,15 +785,15 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
804785
}
805786

806787
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => {
807-
let module = self.r.new_module(
788+
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
789+
790+
self.parent_scope.module = self.r.new_module(
808791
Some(parent),
809792
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
810793
expansion.to_expn_id(),
811794
item.span,
812795
parent.no_implicit_prelude,
813796
);
814-
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
815-
self.parent_scope.module = module;
816797
}
817798

818799
// These items live in both the type and value namespaces.
@@ -920,8 +901,9 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
920901
}
921902
.map(|module| {
922903
let used = self.process_macro_use_imports(item, module);
904+
let res = module.res().unwrap();
923905
let vis = ty::Visibility::<LocalDefId>::Public;
924-
let binding = (module, vis, sp, expansion).to_name_binding(self.r.arenas);
906+
let binding = (res, vis, sp, expansion).to_name_binding(self.r.arenas);
925907
(used, Some(ModuleOrUniformRoot::Module(module)), binding)
926908
})
927909
.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
@@ -232,12 +232,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
232232
return;
233233
}
234234

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

@@ -1319,7 +1319,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13191319
}
13201320

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

13401340
if !is_extern_crate_that_also_appears_in_prelude || alias_import {
13411341
// add the module to the lookup
1342-
if seen_modules.insert(module.def_id()) {
1342+
if seen_modules.insert(def_id) {
13431343
if via_import { &mut worklist_via_import } else { &mut worklist }.push(
13441344
(
1345-
module,
1345+
this.expect_module(def_id),
13461346
path_segments,
13471347
child_accessible,
13481348
child_doc_visible,
1349-
is_stable && this.is_stable(module.def_id(), name_binding.span),
1349+
is_stable && this.is_stable(def_id, name_binding.span),
13501350
),
13511351
);
13521352
}
@@ -2094,7 +2094,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20942094
true, // re-export
20952095
));
20962096
}
2097-
NameBindingKind::Res(_) | NameBindingKind::Module(_) => {}
2097+
NameBindingKind::Res(_) => {}
20982098
}
20992099
let first = binding == first_binding;
21002100
let def_span = self.tcx.sess.source_map().guess_head_span(binding.span);
@@ -2306,25 +2306,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23062306
.ok()
23072307
};
23082308
if let Some(binding) = binding {
2309-
let mut found = |what| {
2310-
msg = format!(
2311-
"expected {}, found {} `{}` in {}",
2312-
ns.descr(),
2313-
what,
2314-
ident,
2315-
parent
2316-
)
2317-
};
2318-
if binding.module().is_some() {
2319-
found("module")
2320-
} else {
2321-
match binding.res() {
2322-
// Avoid using TyCtxt::def_kind_descr in the resolver, because it
2323-
// indirectly *calls* the resolver, and would cause a query cycle.
2324-
Res::Def(kind, id) => found(kind.descr(id)),
2325-
_ => found(ns_to_try.descr()),
2326-
}
2327-
}
2309+
msg = format!(
2310+
"expected {}, found {} `{ident}` in {parent}",
2311+
ns.descr(),
2312+
binding.res().descr(),
2313+
);
23282314
};
23292315
}
23302316
(msg, None)

compiler/rustc_resolve/src/ident.rs

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

16391639
let maybe_assoc = opt_ns != Some(MacroNS) && PathSource::Type.is_expected(res);
1640-
if let Some(next_module) = binding.module() {
1641-
if self.mods_with_parse_errors.contains(&next_module.def_id()) {
1640+
if let Some(def_id) = binding.res().module_like_def_id() {
1641+
if self.mods_with_parse_errors.contains(&def_id) {
16421642
module_had_parse_errors = true;
16431643
}
1644-
module = Some(ModuleOrUniformRoot::Module(next_module));
1644+
module = Some(ModuleOrUniformRoot::Module(self.expect_module(def_id)));
16451645
record_segment_res(self, res);
16461646
} else if res == Res::ToolMod && !is_last && opt_ns.is_some() {
16471647
if binding.is_import() {

compiler/rustc_resolve/src/imports.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
679679
NameBindingKind::Res(res) => {
680680
Some(self.def_id_to_node_id(res.def_id().expect_local()))
681681
}
682-
NameBindingKind::Module(module) => {
683-
Some(self.def_id_to_node_id(module.def_id().expect_local()))
684-
}
685682
NameBindingKind::Import { import, .. } => import.id(),
686683
};
687684
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
@@ -2656,18 +2656,17 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26562656
if result.is_some() || !name_binding.vis.is_visible_locally() {
26572657
return;
26582658
}
2659-
if let Some(module) = name_binding.module() {
2659+
if let Some(module_def_id) = name_binding.res().module_like_def_id() {
26602660
// form the path
26612661
let mut path_segments = path_segments.clone();
26622662
path_segments.push(ast::PathSegment::from_ident(ident));
2663-
let module_def_id = module.def_id();
26642663
let doc_visible = doc_visible
26652664
&& (module_def_id.is_local() || !r.tcx.is_doc_hidden(module_def_id));
26662665
if module_def_id == def_id {
26672666
let path =
26682667
Path { span: name_binding.span, segments: path_segments, tokens: None };
26692668
result = Some((
2670-
module,
2669+
r.expect_module(module_def_id),
26712670
ImportSuggestion {
26722671
did: Some(def_id),
26732672
descr: "module",
@@ -2682,6 +2681,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26822681
} else {
26832682
// add the module to the lookup
26842683
if seen_modules.insert(module_def_id) {
2684+
let module = r.expect_module(module_def_id);
26852685
worklist.push((module, path_segments, doc_visible));
26862686
}
26872687
}

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
}
@@ -921,7 +910,7 @@ impl<'ra> NameBindingData<'ra> {
921910
DefKind::Variant | DefKind::Ctor(CtorOf::Variant, ..),
922911
_,
923912
)) => true,
924-
NameBindingKind::Res(..) | NameBindingKind::Module(..) => false,
913+
NameBindingKind::Res(..) => false,
925914
}
926915
}
927916

@@ -930,11 +919,7 @@ impl<'ra> NameBindingData<'ra> {
930919
NameBindingKind::Import { import, .. } => {
931920
matches!(import.kind, ImportKind::ExternCrate { .. })
932921
}
933-
NameBindingKind::Module(module)
934-
if let ModuleKind::Def(DefKind::Mod, def_id, _) = module.kind =>
935-
{
936-
def_id.is_crate_root()
937-
}
922+
NameBindingKind::Res(Res::Def(_, def_id)) => def_id.is_crate_root(),
938923
_ => false,
939924
}
940925
}
@@ -1279,7 +1264,8 @@ impl<'ra> ResolverArenas<'ra> {
12791264
if let Some(def_id) = def_id {
12801265
module_map.insert(def_id, module);
12811266
let vis = ty::Visibility::<DefId>::Public;
1282-
let binding = (module, vis, module.span, LocalExpnId::ROOT).to_name_binding(self);
1267+
let res = module.res().unwrap();
1268+
let binding = (res, vis, module.span, LocalExpnId::ROOT).to_name_binding(self);
12831269
module_self_bindings.insert(module, binding);
12841270
}
12851271
module
@@ -1838,7 +1824,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18381824
module.ensure_traits(self);
18391825
let traits = module.traits.borrow();
18401826
for (trait_name, trait_binding) in traits.as_ref().unwrap().iter() {
1841-
if self.trait_may_have_item(trait_binding.module(), assoc_item) {
1827+
let trait_module = self.get_module(trait_binding.res().def_id());
1828+
if self.trait_may_have_item(trait_module, assoc_item) {
18421829
let def_id = trait_binding.res().def_id();
18431830
let import_ids = self.find_transitive_imports(&trait_binding.kind, *trait_name);
18441831
found_traits.push(TraitCandidate { def_id, import_ids });
@@ -2174,9 +2161,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21742161
} else {
21752162
self.crate_loader(|c| c.maybe_process_path_extern(ident.name))?
21762163
};
2177-
let crate_root = self.expect_module(crate_id.as_def_id());
2164+
let res = Res::Def(DefKind::Mod, crate_id.as_def_id());
21782165
let vis = ty::Visibility::<DefId>::Public;
2179-
(crate_root, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas)
2166+
(res, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas)
21802167
})
21812168
});
21822169

0 commit comments

Comments
 (0)