Skip to content

Commit 01b546a

Browse files
committed
resolve: Remove trait ToNameBinding
1 parent babe2c0 commit 01b546a

File tree

5 files changed

+84
-78
lines changed

5 files changed

+84
-78
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

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

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

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-
5542
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
5643
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
5744
/// 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);
45+
pub(crate) fn define_binding(
46+
&mut self,
47+
parent: Module<'ra>,
48+
ident: Ident,
49+
ns: Namespace,
50+
binding: NameBinding<'ra>,
51+
) {
6352
let key = self.new_disambiguated_key(ident, ns);
6453
if let Err(old_binding) = self.try_define(parent, key, binding, false) {
6554
self.report_conflict(parent, ident, ns, old_binding, binding);
6655
}
6756
}
6857

58+
fn define(
59+
&mut self,
60+
parent: Module<'ra>,
61+
ident: Ident,
62+
ns: Namespace,
63+
res: Res,
64+
vis: ty::Visibility<impl Into<DefId>>,
65+
span: Span,
66+
expn_id: LocalExpnId,
67+
) {
68+
let binding = self.arenas.new_res_binding(res, vis.to_def_id(), span, expn_id);
69+
self.define_binding(parent, ident, ns, binding)
70+
}
71+
6972
/// Walks up the tree of definitions starting at `def_id`,
7073
/// stopping at the first encountered module.
7174
/// Parent block modules for arbitrary def-ids are not recorded for the local crate,
@@ -223,7 +226,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
223226
_,
224227
)
225228
| Res::PrimTy(..)
226-
| Res::ToolMod => self.define(parent, ident, TypeNS, (res, vis, span, expansion)),
229+
| Res::ToolMod => self.define(parent, ident, TypeNS, res, vis, span, expansion),
227230
Res::Def(
228231
DefKind::Fn
229232
| DefKind::AssocFn
@@ -232,9 +235,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
232235
| DefKind::AssocConst
233236
| DefKind::Ctor(..),
234237
_,
235-
) => self.define(parent, ident, ValueNS, (res, vis, span, expansion)),
238+
) => self.define(parent, ident, ValueNS, res, vis, span, expansion),
236239
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
237-
self.define(parent, ident, MacroNS, (res, vis, span, expansion))
240+
self.define(parent, ident, MacroNS, res, vis, span, expansion)
238241
}
239242
Res::Def(
240243
DefKind::TyParam
@@ -698,7 +701,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
698701
let expansion = parent_scope.expansion;
699702

700703
// Define a name in the type namespace if it is not anonymous.
701-
self.r.define(parent, ident, TypeNS, (adt_res, adt_vis, adt_span, expansion));
704+
self.r.define(parent, ident, TypeNS, adt_res, adt_vis, adt_span, expansion);
702705
self.r.feed_visibility(feed, adt_vis);
703706
let def_id = feed.key();
704707

@@ -750,7 +753,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
750753
}
751754

752755
ItemKind::Mod(_, ident, ref mod_kind) => {
753-
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
756+
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
754757

755758
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
756759
self.r.mods_with_parse_errors.insert(def_id);
@@ -769,10 +772,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
769772
ItemKind::Const(box ConstItem { ident, .. })
770773
| ItemKind::Delegation(box Delegation { ident, .. })
771774
| ItemKind::Static(box StaticItem { ident, .. }) => {
772-
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
775+
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
773776
}
774777
ItemKind::Fn(box Fn { ident, .. }) => {
775-
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
778+
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
776779

777780
// Functions introducing procedural macros reserve a slot
778781
// in the macro namespace as well (see #52225).
@@ -781,11 +784,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
781784

782785
// These items live in the type namespace.
783786
ItemKind::TyAlias(box TyAlias { ident, .. }) | ItemKind::TraitAlias(ident, ..) => {
784-
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
787+
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
785788
}
786789

787790
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => {
788-
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
791+
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
789792

790793
self.parent_scope.module = self.r.new_module(
791794
Some(parent),
@@ -837,7 +840,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
837840
let feed = self.r.feed(ctor_node_id);
838841
let ctor_def_id = feed.key();
839842
let ctor_res = self.res(ctor_def_id);
840-
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
843+
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
841844
self.r.feed_visibility(feed, ctor_vis);
842845
// We need the field visibility spans also for the constructor for E0603.
843846
self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata.fields());
@@ -901,9 +904,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
901904
}
902905
.map(|module| {
903906
let used = self.process_macro_use_imports(item, module);
904-
let res = module.res().unwrap();
905-
let vis = ty::Visibility::<LocalDefId>::Public;
906-
let binding = (res, vis, sp, expansion).to_name_binding(self.r.arenas);
907+
let binding = self.r.arenas.new_pub_res_binding(module.res().unwrap(), sp, expansion);
907908
(used, Some(ModuleOrUniformRoot::Module(module)), binding)
908909
})
909910
.unwrap_or((true, None, self.r.dummy_binding));
@@ -960,7 +961,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
960961
);
961962
}
962963
}
963-
self.r.define(parent, ident, TypeNS, imported_binding);
964+
self.r.define_binding(parent, ident, TypeNS, imported_binding);
964965
}
965966

966967
/// Constructs the reduced graph for one foreign item.
@@ -977,7 +978,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
977978
let parent = self.parent_scope.module;
978979
let expansion = self.parent_scope.expansion;
979980
let vis = self.resolve_visibility(&item.vis);
980-
self.r.define(parent, ident, ns, (self.res(def_id), vis, item.span, expansion));
981+
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
981982
self.r.feed_visibility(feed, vis);
982983
}
983984

@@ -1217,7 +1218,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12171218
} else {
12181219
ty::Visibility::Restricted(CRATE_DEF_ID)
12191220
};
1220-
let binding = (res, vis, span, expansion).to_name_binding(self.r.arenas);
1221+
let binding = self.r.arenas.new_res_binding(res, vis.to_def_id(), span, expansion);
12211222
self.r.set_binding_parent_module(binding, parent_scope.module);
12221223
self.r.all_macro_rules.insert(ident.name);
12231224
if is_macro_export {
@@ -1236,7 +1237,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12361237
});
12371238
self.r.import_use_map.insert(import, Used::Other);
12381239
let import_binding = self.r.import(binding, import);
1239-
self.r.define(self.r.graph_root, ident, MacroNS, import_binding);
1240+
self.r.define_binding(self.r.graph_root, ident, MacroNS, import_binding);
12401241
} else {
12411242
self.r.check_reserved_macro_name(ident, res);
12421243
self.insert_unused_macro(ident, def_id, item.id);
@@ -1264,7 +1265,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12641265
if !vis.is_public() {
12651266
self.insert_unused_macro(ident, def_id, item.id);
12661267
}
1267-
self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
1268+
self.r.define(module, ident, MacroNS, res, vis, span, expansion);
12681269
self.r.feed_visibility(feed, vis);
12691270
self.parent_scope.macro_rules
12701271
}
@@ -1400,7 +1401,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14001401
if ctxt == AssocCtxt::Trait {
14011402
let parent = self.parent_scope.module;
14021403
let expansion = self.parent_scope.expansion;
1403-
self.r.define(parent, ident, ns, (self.res(def_id), vis, item.span, expansion));
1404+
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
14041405
} else if !matches!(&item.kind, AssocItemKind::Delegation(deleg) if deleg.from_glob) {
14051406
let impl_def_id = self.r.tcx.local_parent(local_def_id);
14061407
let key = BindingKey::new(ident.normalize_to_macros_2_0(), ns);
@@ -1485,7 +1486,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14851486
let feed = self.r.feed(variant.id);
14861487
let def_id = feed.key();
14871488
let vis = self.resolve_visibility(&variant.vis);
1488-
self.r.define(parent, ident, TypeNS, (self.res(def_id), vis, variant.span, expn_id));
1489+
self.r.define(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
14891490
self.r.feed_visibility(feed, vis);
14901491

14911492
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
@@ -1501,7 +1502,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
15011502
let feed = self.r.feed(ctor_node_id);
15021503
let ctor_def_id = feed.key();
15031504
let ctor_res = self.res(ctor_def_id);
1504-
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));
1505+
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id);
15051506
self.r.feed_visibility(feed, ctor_vis);
15061507
}
15071508

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
@@ -872,7 +872,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
872872
}
873873
// We need the `target`, `source` can be extracted.
874874
let imported_binding = this.import(binding, import);
875-
this.define(parent, target, ns, imported_binding);
875+
this.define_binding(parent, target, ns, imported_binding);
876876
PendingBinding::Ready(Some(imported_binding))
877877
}
878878
Err(Determinacy::Determined) => {

compiler/rustc_resolve/src/lib.rs

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

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

12421232
impl<'ra> ResolverArenas<'ra> {
1233+
fn new_res_binding(
1234+
&'ra self,
1235+
res: Res,
1236+
vis: ty::Visibility<DefId>,
1237+
span: Span,
1238+
expansion: LocalExpnId,
1239+
) -> NameBinding<'ra> {
1240+
self.alloc_name_binding(NameBindingData {
1241+
kind: NameBindingKind::Res(res),
1242+
ambiguity: None,
1243+
warn_ambiguity: false,
1244+
vis,
1245+
span,
1246+
expansion,
1247+
})
1248+
}
1249+
1250+
fn new_pub_res_binding(
1251+
&'ra self,
1252+
res: Res,
1253+
span: Span,
1254+
expn_id: LocalExpnId,
1255+
) -> NameBinding<'ra> {
1256+
self.new_res_binding(res, Visibility::Public, span, expn_id)
1257+
}
1258+
12431259
fn new_module(
12441260
&'ra self,
12451261
parent: Option<Module<'ra>>,
@@ -1263,9 +1279,8 @@ impl<'ra> ResolverArenas<'ra> {
12631279
}
12641280
if let Some(def_id) = def_id {
12651281
module_map.insert(def_id, module);
1266-
let vis = ty::Visibility::<DefId>::Public;
12671282
let res = module.res().unwrap();
1268-
let binding = (res, vis, module.span, LocalExpnId::ROOT).to_name_binding(self);
1283+
let binding = self.new_pub_res_binding(res, module.span, LocalExpnId::ROOT);
12691284
module_self_bindings.insert(module, binding);
12701285
}
12711286
module
@@ -1456,8 +1471,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14561471
}
14571472

14581473
let registered_tools = tcx.registered_tools(());
1459-
1460-
let pub_vis = ty::Visibility::<DefId>::Public;
14611474
let edition = tcx.sess.edition();
14621475

14631476
let mut resolver = Resolver {
@@ -1506,29 +1519,28 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15061519
macro_expanded_macro_export_errors: BTreeSet::new(),
15071520

15081521
arenas,
1509-
dummy_binding: (Res::Err, pub_vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(arenas),
1522+
dummy_binding: arenas.new_pub_res_binding(Res::Err, DUMMY_SP, LocalExpnId::ROOT),
15101523
builtin_types_bindings: PrimTy::ALL
15111524
.iter()
15121525
.map(|prim_ty| {
1513-
let binding = (Res::PrimTy(*prim_ty), pub_vis, DUMMY_SP, LocalExpnId::ROOT)
1514-
.to_name_binding(arenas);
1526+
let res = Res::PrimTy(*prim_ty);
1527+
let binding = arenas.new_pub_res_binding(res, DUMMY_SP, LocalExpnId::ROOT);
15151528
(prim_ty.name(), binding)
15161529
})
15171530
.collect(),
15181531
builtin_attrs_bindings: BUILTIN_ATTRIBUTES
15191532
.iter()
15201533
.map(|builtin_attr| {
15211534
let res = Res::NonMacroAttr(NonMacroAttrKind::Builtin(builtin_attr.name));
1522-
let binding =
1523-
(res, pub_vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(arenas);
1535+
let binding = arenas.new_pub_res_binding(res, DUMMY_SP, LocalExpnId::ROOT);
15241536
(builtin_attr.name, binding)
15251537
})
15261538
.collect(),
15271539
registered_tool_bindings: registered_tools
15281540
.iter()
15291541
.map(|ident| {
1530-
let binding = (Res::ToolMod, pub_vis, ident.span, LocalExpnId::ROOT)
1531-
.to_name_binding(arenas);
1542+
let res = Res::ToolMod;
1543+
let binding = arenas.new_pub_res_binding(res, ident.span, LocalExpnId::ROOT);
15321544
(*ident, binding)
15331545
})
15341546
.collect(),
@@ -2162,8 +2174,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21622174
self.crate_loader(|c| c.maybe_process_path_extern(ident.name))?
21632175
};
21642176
let res = Res::Def(DefKind::Mod, crate_id.as_def_id());
2165-
let vis = ty::Visibility::<DefId>::Public;
2166-
(res, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas)
2177+
self.arenas.new_pub_res_binding(res, DUMMY_SP, LocalExpnId::ROOT)
21672178
})
21682179
});
21692180

0 commit comments

Comments
 (0)