Skip to content

Commit e6f4781

Browse files
committed
resolve: Remove trait ToNameBinding
1 parent 1b0ab9b commit e6f4781

File tree

5 files changed

+84
-79
lines changed

5 files changed

+84
-79
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,42 @@ use crate::imports::{ImportData, ImportKind};
3232
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
3333
use crate::{
3434
BindingKey, Determinacy, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind,
35-
ModuleOrUniformRoot, NameBinding, NameBindingData, NameBindingKind, ParentScope, PathResult,
36-
ResolutionError, Resolver, ResolverArenas, Segment, ToNameBinding, Used, VisResolutionError,
37-
errors,
35+
ModuleOrUniformRoot, NameBinding, ParentScope, PathResult, ResolutionError, Resolver, Segment,
36+
Used, VisResolutionError, errors,
3837
};
3938

4039
type Res = def::Res<NodeId>;
4140

42-
impl<'ra, Id: Into<DefId>> ToNameBinding<'ra> for (Res, ty::Visibility<Id>, Span, LocalExpnId) {
43-
fn to_name_binding(self, arenas: &'ra ResolverArenas<'ra>) -> NameBinding<'ra> {
44-
arenas.alloc_name_binding(NameBindingData {
45-
kind: NameBindingKind::Res(self.0),
46-
ambiguity: None,
47-
warn_ambiguity: false,
48-
vis: self.1.to_def_id(),
49-
span: self.2,
50-
expansion: self.3,
51-
})
52-
}
53-
}
54-
5541
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
5642
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
5743
/// otherwise, reports an error.
58-
pub(crate) fn define<T>(&mut self, parent: Module<'ra>, ident: Ident, ns: Namespace, def: T)
59-
where
60-
T: ToNameBinding<'ra>,
61-
{
62-
let binding = def.to_name_binding(self.arenas);
44+
pub(crate) fn define_binding(
45+
&mut self,
46+
parent: Module<'ra>,
47+
ident: Ident,
48+
ns: Namespace,
49+
binding: NameBinding<'ra>,
50+
) {
6351
let key = self.new_disambiguated_key(ident, ns);
6452
if let Err(old_binding) = self.try_define(parent, key, binding, false) {
6553
self.report_conflict(parent, ident, ns, old_binding, binding);
6654
}
6755
}
6856

57+
fn define(
58+
&mut self,
59+
parent: Module<'ra>,
60+
ident: Ident,
61+
ns: Namespace,
62+
res: Res,
63+
vis: ty::Visibility<impl Into<DefId>>,
64+
span: Span,
65+
expn_id: LocalExpnId,
66+
) {
67+
let binding = self.arenas.new_res_binding(res, vis.to_def_id(), span, expn_id);
68+
self.define_binding(parent, ident, ns, binding)
69+
}
70+
6971
/// Walks up the tree of definitions starting at `def_id`,
7072
/// stopping at the first encountered module.
7173
/// Parent block modules for arbitrary def-ids are not recorded for the local crate,
@@ -221,7 +223,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
221223
_,
222224
)
223225
| Res::PrimTy(..)
224-
| Res::ToolMod => self.define(parent, ident, TypeNS, (res, vis, span, expansion)),
226+
| Res::ToolMod => self.define(parent, ident, TypeNS, res, vis, span, expansion),
225227
Res::Def(
226228
DefKind::Fn
227229
| DefKind::AssocFn
@@ -230,9 +232,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
230232
| DefKind::AssocConst
231233
| DefKind::Ctor(..),
232234
_,
233-
) => self.define(parent, ident, ValueNS, (res, vis, span, expansion)),
235+
) => self.define(parent, ident, ValueNS, res, vis, span, expansion),
234236
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
235-
self.define(parent, ident, MacroNS, (res, vis, span, expansion))
237+
self.define(parent, ident, MacroNS, res, vis, span, expansion)
236238
}
237239
Res::Def(
238240
DefKind::TyParam
@@ -705,7 +707,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
705707
let expansion = parent_scope.expansion;
706708

707709
// Define a name in the type namespace if it is not anonymous.
708-
self.r.define(parent, ident, TypeNS, (adt_res, adt_vis, adt_span, expansion));
710+
self.r.define(parent, ident, TypeNS, adt_res, adt_vis, adt_span, expansion);
709711
self.r.feed_visibility(feed, adt_vis);
710712
let def_id = feed.key();
711713

@@ -757,7 +759,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
757759
}
758760

759761
ItemKind::Mod(_, ident, ref mod_kind) => {
760-
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
762+
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
761763

762764
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
763765
self.r.mods_with_parse_errors.insert(def_id);
@@ -776,10 +778,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
776778
ItemKind::Const(box ConstItem { ident, .. })
777779
| ItemKind::Delegation(box Delegation { ident, .. })
778780
| ItemKind::Static(box StaticItem { ident, .. }) => {
779-
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
781+
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
780782
}
781783
ItemKind::Fn(box Fn { ident, .. }) => {
782-
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
784+
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
783785

784786
// Functions introducing procedural macros reserve a slot
785787
// in the macro namespace as well (see #52225).
@@ -788,11 +790,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
788790

789791
// These items live in the type namespace.
790792
ItemKind::TyAlias(box TyAlias { ident, .. }) | ItemKind::TraitAlias(ident, ..) => {
791-
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
793+
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
792794
}
793795

794796
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => {
795-
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
797+
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
796798

797799
self.parent_scope.module = self.r.new_module(
798800
Some(parent),
@@ -844,7 +846,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
844846
let feed = self.r.feed(ctor_node_id);
845847
let ctor_def_id = feed.key();
846848
let ctor_res = self.res(ctor_def_id);
847-
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
849+
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
848850
self.r.feed_visibility(feed, ctor_vis);
849851
// We need the field visibility spans also for the constructor for E0603.
850852
self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata.fields());
@@ -908,9 +910,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
908910
}
909911
.map(|module| {
910912
let used = self.process_macro_use_imports(item, module);
911-
let res = module.res().unwrap();
912-
let vis = ty::Visibility::<LocalDefId>::Public;
913-
let binding = (res, vis, sp, expansion).to_name_binding(self.r.arenas);
913+
let binding = self.r.arenas.new_pub_res_binding(module.res().unwrap(), sp, expansion);
914914
(used, Some(ModuleOrUniformRoot::Module(module)), binding)
915915
})
916916
.unwrap_or((true, None, self.r.dummy_binding));
@@ -967,7 +967,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
967967
);
968968
}
969969
}
970-
self.r.define(parent, ident, TypeNS, imported_binding);
970+
self.r.define_binding(parent, ident, TypeNS, imported_binding);
971971
}
972972

973973
/// Constructs the reduced graph for one foreign item.
@@ -984,7 +984,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
984984
let parent = self.parent_scope.module;
985985
let expansion = self.parent_scope.expansion;
986986
let vis = self.resolve_visibility(&item.vis);
987-
self.r.define(parent, ident, ns, (self.res(def_id), vis, item.span, expansion));
987+
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
988988
self.r.feed_visibility(feed, vis);
989989
}
990990

@@ -1225,7 +1225,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12251225
} else {
12261226
ty::Visibility::Restricted(CRATE_DEF_ID)
12271227
};
1228-
let binding = (res, vis, span, expansion).to_name_binding(self.r.arenas);
1228+
let binding = self.r.arenas.new_res_binding(res, vis.to_def_id(), span, expansion);
12291229
self.r.set_binding_parent_module(binding, parent_scope.module);
12301230
self.r.all_macro_rules.insert(ident.name);
12311231
if is_macro_export {
@@ -1244,7 +1244,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12441244
});
12451245
self.r.import_use_map.insert(import, Used::Other);
12461246
let import_binding = self.r.import(binding, import);
1247-
self.r.define(self.r.graph_root, ident, MacroNS, import_binding);
1247+
self.r.define_binding(self.r.graph_root, ident, MacroNS, import_binding);
12481248
} else {
12491249
self.r.check_reserved_macro_name(ident, res);
12501250
self.insert_unused_macro(ident, def_id, item.id);
@@ -1272,7 +1272,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12721272
if !vis.is_public() {
12731273
self.insert_unused_macro(ident, def_id, item.id);
12741274
}
1275-
self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
1275+
self.r.define(module, ident, MacroNS, res, vis, span, expansion);
12761276
self.r.feed_visibility(feed, vis);
12771277
self.parent_scope.macro_rules
12781278
}
@@ -1408,7 +1408,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14081408
if ctxt == AssocCtxt::Trait {
14091409
let parent = self.parent_scope.module;
14101410
let expansion = self.parent_scope.expansion;
1411-
self.r.define(parent, ident, ns, (self.res(def_id), vis, item.span, expansion));
1411+
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
14121412
} else if !matches!(&item.kind, AssocItemKind::Delegation(deleg) if deleg.from_glob) {
14131413
let impl_def_id = self.r.tcx.local_parent(local_def_id);
14141414
let key = BindingKey::new(ident.normalize_to_macros_2_0(), ns);
@@ -1493,7 +1493,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14931493
let feed = self.r.feed(variant.id);
14941494
let def_id = feed.key();
14951495
let vis = self.resolve_visibility(&variant.vis);
1496-
self.r.define(parent, ident, TypeNS, (self.res(def_id), vis, variant.span, expn_id));
1496+
self.r.define(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
14971497
self.r.feed_visibility(feed, vis);
14981498

14991499
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
@@ -1509,7 +1509,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
15091509
let feed = self.r.feed(ctor_node_id);
15101510
let ctor_def_id = feed.key();
15111511
let ctor_res = self.res(ctor_def_id);
1512-
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));
1512+
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id);
15131513
self.r.feed_visibility(feed, ctor_vis);
15141514
}
15151515

compiler/rustc_resolve/src/ident.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ use Namespace::*;
33
use rustc_ast::{self as ast, NodeId};
44
use rustc_errors::ErrorGuaranteed;
55
use rustc_hir::def::{DefKind, Namespace, NonMacroAttrKind, PartialRes, PerNS};
6-
use rustc_middle::{bug, ty};
6+
use rustc_middle::bug;
77
use rustc_session::lint::BuiltinLintDiag;
88
use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
99
use rustc_session::parse::feature_err;
10-
use rustc_span::def_id::LocalDefId;
1110
use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
1211
use rustc_span::{Ident, Span, kw, sym};
1312
use tracing::{debug, instrument};
@@ -20,11 +19,9 @@ use crate::{
2019
AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingKey, Determinacy, Finalize,
2120
ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot, NameBinding,
2221
NameBindingKind, ParentScope, PathResult, PrivacyError, Res, ResolutionError, Resolver, Scope,
23-
ScopeSet, Segment, ToNameBinding, Used, Weak, errors,
22+
ScopeSet, Segment, Used, Weak, errors,
2423
};
2524

26-
type Visibility = ty::Visibility<LocalDefId>;
27-
2825
#[derive(Copy, Clone)]
2926
pub enum UsePrelude {
3027
No,
@@ -464,13 +461,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
464461
) {
465462
Ok((Some(ext), _)) => {
466463
if ext.helper_attrs.contains(&ident.name) {
467-
let binding = (
464+
let binding = this.arenas.new_pub_res_binding(
468465
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat),
469-
Visibility::Public,
470466
derive.span,
471467
LocalExpnId::ROOT,
472-
)
473-
.to_name_binding(this.arenas);
468+
);
474469
result = Ok((binding, Flags::empty()));
475470
break;
476471
}

compiler/rustc_resolve/src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
865865

866866
let imported_binding = this.import(binding, import);
867867
target_bindings[ns].set(Some(imported_binding));
868-
this.define(parent, target, ns, imported_binding);
868+
this.define_binding(parent, target, ns, imported_binding);
869869
}
870870
Err(Determined) => {
871871
// Don't update the resolution for underscores, because it was never added.

compiler/rustc_resolve/src/lib.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -767,16 +767,6 @@ impl std::hash::Hash for NameBindingData<'_> {
767767
}
768768
}
769769

770-
trait ToNameBinding<'ra> {
771-
fn to_name_binding(self, arenas: &'ra ResolverArenas<'ra>) -> NameBinding<'ra>;
772-
}
773-
774-
impl<'ra> ToNameBinding<'ra> for NameBinding<'ra> {
775-
fn to_name_binding(self, _: &'ra ResolverArenas<'ra>) -> NameBinding<'ra> {
776-
self
777-
}
778-
}
779-
780770
#[derive(Clone, Copy, Debug)]
781771
enum NameBindingKind<'ra> {
782772
Res(Res),
@@ -1229,6 +1219,32 @@ pub struct ResolverArenas<'ra> {
12291219
}
12301220

12311221
impl<'ra> ResolverArenas<'ra> {
1222+
fn new_res_binding(
1223+
&'ra self,
1224+
res: Res,
1225+
vis: ty::Visibility<DefId>,
1226+
span: Span,
1227+
expansion: LocalExpnId,
1228+
) -> NameBinding<'ra> {
1229+
self.alloc_name_binding(NameBindingData {
1230+
kind: NameBindingKind::Res(res),
1231+
ambiguity: None,
1232+
warn_ambiguity: false,
1233+
vis,
1234+
span,
1235+
expansion,
1236+
})
1237+
}
1238+
1239+
fn new_pub_res_binding(
1240+
&'ra self,
1241+
res: Res,
1242+
span: Span,
1243+
expn_id: LocalExpnId,
1244+
) -> NameBinding<'ra> {
1245+
self.new_res_binding(res, Visibility::Public, span, expn_id)
1246+
}
1247+
12321248
fn new_module(
12331249
&'ra self,
12341250
parent: Option<Module<'ra>>,
@@ -1252,9 +1268,8 @@ impl<'ra> ResolverArenas<'ra> {
12521268
}
12531269
if let Some(def_id) = def_id {
12541270
module_map.insert(def_id, module);
1255-
let vis = ty::Visibility::<DefId>::Public;
12561271
let res = module.res().unwrap();
1257-
let binding = (res, vis, module.span, LocalExpnId::ROOT).to_name_binding(self);
1272+
let binding = self.new_pub_res_binding(res, module.span, LocalExpnId::ROOT);
12581273
module_self_bindings.insert(module, binding);
12591274
}
12601275
module
@@ -1442,8 +1457,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14421457
}
14431458

14441459
let registered_tools = tcx.registered_tools(());
1445-
1446-
let pub_vis = ty::Visibility::<DefId>::Public;
14471460
let edition = tcx.sess.edition();
14481461

14491462
let mut resolver = Resolver {
@@ -1492,29 +1505,28 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14921505
macro_expanded_macro_export_errors: BTreeSet::new(),
14931506

14941507
arenas,
1495-
dummy_binding: (Res::Err, pub_vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(arenas),
1508+
dummy_binding: arenas.new_pub_res_binding(Res::Err, DUMMY_SP, LocalExpnId::ROOT),
14961509
builtin_types_bindings: PrimTy::ALL
14971510
.iter()
14981511
.map(|prim_ty| {
1499-
let binding = (Res::PrimTy(*prim_ty), pub_vis, DUMMY_SP, LocalExpnId::ROOT)
1500-
.to_name_binding(arenas);
1512+
let res = Res::PrimTy(*prim_ty);
1513+
let binding = arenas.new_pub_res_binding(res, DUMMY_SP, LocalExpnId::ROOT);
15011514
(prim_ty.name(), binding)
15021515
})
15031516
.collect(),
15041517
builtin_attrs_bindings: BUILTIN_ATTRIBUTES
15051518
.iter()
15061519
.map(|builtin_attr| {
15071520
let res = Res::NonMacroAttr(NonMacroAttrKind::Builtin(builtin_attr.name));
1508-
let binding =
1509-
(res, pub_vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(arenas);
1521+
let binding = arenas.new_pub_res_binding(res, DUMMY_SP, LocalExpnId::ROOT);
15101522
(builtin_attr.name, binding)
15111523
})
15121524
.collect(),
15131525
registered_tool_bindings: registered_tools
15141526
.iter()
15151527
.map(|ident| {
1516-
let binding = (Res::ToolMod, pub_vis, ident.span, LocalExpnId::ROOT)
1517-
.to_name_binding(arenas);
1528+
let res = Res::ToolMod;
1529+
let binding = arenas.new_pub_res_binding(res, ident.span, LocalExpnId::ROOT);
15181530
(*ident, binding)
15191531
})
15201532
.collect(),
@@ -2140,8 +2152,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21402152
self.crate_loader(|c| c.maybe_process_path_extern(ident.name))?
21412153
};
21422154
let res = Res::Def(DefKind::Mod, crate_id.as_def_id());
2143-
let vis = ty::Visibility::<DefId>::Public;
2144-
(res, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas)
2155+
self.arenas.new_pub_res_binding(res, DUMMY_SP, LocalExpnId::ROOT)
21452156
})
21462157
});
21472158

0 commit comments

Comments
 (0)