Skip to content

Commit 8494efd

Browse files
petrochenkovLorrensP-2158466
authored andcommitted
resolve: Merge NameBindingKind::Module into NameBindingKind::Res
1 parent d2baa49 commit 8494efd

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
@@ -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)
@@ -224,12 +209,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
224209
let expansion = parent_scope.expansion;
225210
// Record primary definitions.
226211
match res {
227-
Res::Def(DefKind::Mod | DefKind::Enum | DefKind::Trait, def_id) => {
228-
let module = self.expect_module(def_id);
229-
self.define(parent, ident, TypeNS, (module, vis, span, expansion));
230-
}
231212
Res::Def(
232-
DefKind::Struct
213+
DefKind::Mod
214+
| DefKind::Enum
215+
| DefKind::Trait
216+
| DefKind::Struct
233217
| DefKind::Union
234218
| DefKind::Variant
235219
| DefKind::TyAlias
@@ -776,22 +760,19 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
776760
}
777761

778762
ItemKind::Mod(_, ident, ref mod_kind) => {
779-
let module = self.r.new_module(
763+
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
764+
765+
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
766+
self.r.mods_with_parse_errors.insert(def_id);
767+
}
768+
self.parent_scope.module = self.r.new_module(
780769
Some(parent),
781770
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
782771
expansion.to_expn_id(),
783772
item.span,
784773
parent.no_implicit_prelude
785774
|| ast::attr::contains_name(&item.attrs, sym::no_implicit_prelude),
786775
);
787-
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
788-
789-
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
790-
self.r.mods_with_parse_errors.insert(def_id);
791-
}
792-
793-
// Descend into the module.
794-
self.parent_scope.module = module;
795776
}
796777

797778
// These items live in the value namespace.
@@ -814,15 +795,15 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
814795
}
815796

816797
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => {
817-
let module = self.r.new_module(
798+
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
799+
800+
self.parent_scope.module = self.r.new_module(
818801
Some(parent),
819802
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
820803
expansion.to_expn_id(),
821804
item.span,
822805
parent.no_implicit_prelude,
823806
);
824-
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
825-
self.parent_scope.module = module;
826807
}
827808

828809
// These items live in both the type and value namespaces.
@@ -930,8 +911,9 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
930911
}
931912
.map(|module| {
932913
let used = self.process_macro_use_imports(item, module);
914+
let res = module.res().unwrap();
933915
let vis = ty::Visibility::<LocalDefId>::Public;
934-
let binding = (module, vis, sp, expansion).to_name_binding(self.r.arenas);
916+
let binding = (res, vis, sp, expansion).to_name_binding(self.r.arenas);
935917
(used, Some(ModuleOrUniformRoot::Module(module)), binding)
936918
})
937919
.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
}
@@ -2095,7 +2095,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20952095
true, // re-export
20962096
));
20972097
}
2098-
NameBindingKind::Res(_) | NameBindingKind::Module(_) => {}
2098+
NameBindingKind::Res(_) => {}
20992099
}
21002100
let first = binding == first_binding;
21012101
let def_span = self.tcx.sess.source_map().guess_head_span(binding.span);
@@ -2307,25 +2307,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23072307
.ok()
23082308
};
23092309
if let Some(binding) = binding {
2310-
let mut found = |what| {
2311-
msg = format!(
2312-
"expected {}, found {} `{}` in {}",
2313-
ns.descr(),
2314-
what,
2315-
ident,
2316-
parent
2317-
)
2318-
};
2319-
if binding.module().is_some() {
2320-
found("module")
2321-
} else {
2322-
match binding.res() {
2323-
// Avoid using TyCtxt::def_kind_descr in the resolver, because it
2324-
// indirectly *calls* the resolver, and would cause a query cycle.
2325-
Res::Def(kind, id) => found(kind.descr(id)),
2326-
_ => found(ns_to_try.descr()),
2327-
}
2328-
}
2310+
msg = format!(
2311+
"expected {}, found {} `{ident}` in {parent}",
2312+
ns.descr(),
2313+
binding.res().descr(),
2314+
);
23292315
};
23302316
}
23312317
(msg, None)

compiler/rustc_resolve/src/ident.rs

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

16401640
let maybe_assoc = opt_ns != Some(MacroNS) && PathSource::Type.is_expected(res);
1641-
if let Some(next_module) = binding.module() {
1642-
if self.mods_with_parse_errors.contains(&next_module.def_id()) {
1641+
if let Some(def_id) = binding.res().module_like_def_id() {
1642+
if self.mods_with_parse_errors.contains(&def_id) {
16431643
module_had_parse_errors = true;
16441644
}
1645-
module = Some(ModuleOrUniformRoot::Module(next_module));
1645+
module = Some(ModuleOrUniformRoot::Module(self.expect_module(def_id)));
16461646
record_segment_res(self, res);
16471647
} else if res == Res::ToolMod && !is_last && opt_ns.is_some() {
16481648
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
@@ -2651,18 +2651,17 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26512651
if result.is_some() || !name_binding.vis.is_visible_locally() {
26522652
return;
26532653
}
2654-
if let Some(module) = name_binding.module() {
2654+
if let Some(module_def_id) = name_binding.res().module_like_def_id() {
26552655
// form the path
26562656
let mut path_segments = path_segments.clone();
26572657
path_segments.push(ast::PathSegment::from_ident(ident));
2658-
let module_def_id = module.def_id();
26592658
let doc_visible = doc_visible
26602659
&& (module_def_id.is_local() || !r.tcx.is_doc_hidden(module_def_id));
26612660
if module_def_id == def_id {
26622661
let path =
26632662
Path { span: name_binding.span, segments: path_segments, tokens: None };
26642663
result = Some((
2665-
module,
2664+
r.expect_module(module_def_id),
26662665
ImportSuggestion {
26672666
did: Some(def_id),
26682667
descr: "module",
@@ -2677,6 +2676,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26772676
} else {
26782677
// add the module to the lookup
26792678
if seen_modules.insert(module_def_id) {
2679+
let module = r.expect_module(module_def_id);
26802680
worklist.push((module, path_segments, doc_visible));
26812681
}
26822682
}

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
}
@@ -1272,7 +1257,8 @@ impl<'ra> ResolverArenas<'ra> {
12721257
if let Some(def_id) = def_id {
12731258
module_map.insert(def_id, module);
12741259
let vis = ty::Visibility::<DefId>::Public;
1275-
let binding = (module, vis, module.span, LocalExpnId::ROOT).to_name_binding(self);
1260+
let res = module.res().unwrap();
1261+
let binding = (res, vis, module.span, LocalExpnId::ROOT).to_name_binding(self);
12761262
module_self_bindings.insert(module, binding);
12771263
}
12781264
module
@@ -1831,7 +1817,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18311817
module.ensure_traits(self);
18321818
let traits = module.traits.borrow();
18331819
for (trait_name, trait_binding) in traits.as_ref().unwrap().iter() {
1834-
if self.trait_may_have_item(trait_binding.module(), assoc_item) {
1820+
let trait_module = self.get_module(trait_binding.res().def_id());
1821+
if self.trait_may_have_item(trait_module, assoc_item) {
18351822
let def_id = trait_binding.res().def_id();
18361823
let import_ids = self.find_transitive_imports(&trait_binding.kind, *trait_name);
18371824
found_traits.push(TraitCandidate { def_id, import_ids });
@@ -2167,9 +2154,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21672154
} else {
21682155
self.crate_loader(|c| c.maybe_process_path_extern(ident.name))?
21692156
};
2170-
let crate_root = self.expect_module(crate_id.as_def_id());
2157+
let res = Res::Def(DefKind::Mod, crate_id.as_def_id());
21712158
let vis = ty::Visibility::<DefId>::Public;
2172-
(crate_root, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas)
2159+
(res, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas)
21732160
})
21742161
});
21752162

0 commit comments

Comments
 (0)