Skip to content

Commit 5d3fe89

Browse files
petrochenkovLorrensP-2158466
authored andcommitted
resolve: Remove trait ToNameBinding
1 parent 8494efd commit 5d3fe89

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
@@ -33,40 +33,42 @@ use crate::imports::{ImportData, ImportKind};
3333
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
3434
use crate::{
3535
BindingKey, Determinacy, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind,
36-
ModuleOrUniformRoot, NameBinding, NameBindingData, NameBindingKind, ParentScope, PathResult,
37-
ResolutionError, Resolver, ResolverArenas, Segment, ToNameBinding, Used, VisResolutionError,
38-
errors,
36+
ModuleOrUniformRoot, NameBinding, ParentScope, PathResult, ResolutionError, Resolver, Segment,
37+
Used, VisResolutionError, errors,
3938
};
4039

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

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

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+
7072
/// Walks up the tree of definitions starting at `def_id`,
7173
/// stopping at the first encountered module.
7274
/// Parent block modules for arbitrary def-ids are not recorded for the local crate,
@@ -224,7 +226,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
224226
_,
225227
)
226228
| Res::PrimTy(..)
227-
| Res::ToolMod => self.define(parent, ident, TypeNS, (res, vis, span, expansion)),
229+
| Res::ToolMod => self.define(parent, ident, TypeNS, res, vis, span, expansion),
228230
Res::Def(
229231
DefKind::Fn
230232
| DefKind::AssocFn
@@ -233,9 +235,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
233235
| DefKind::AssocConst
234236
| DefKind::Ctor(..),
235237
_,
236-
) => self.define(parent, ident, ValueNS, (res, vis, span, expansion)),
238+
) => self.define(parent, ident, ValueNS, res, vis, span, expansion),
237239
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
238-
self.define(parent, ident, MacroNS, (res, vis, span, expansion))
240+
self.define(parent, ident, MacroNS, res, vis, span, expansion)
239241
}
240242
Res::Def(
241243
DefKind::TyParam
@@ -708,7 +710,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
708710
let expansion = parent_scope.expansion;
709711

710712
// Define a name in the type namespace if it is not anonymous.
711-
self.r.define(parent, ident, TypeNS, (adt_res, adt_vis, adt_span, expansion));
713+
self.r.define(parent, ident, TypeNS, adt_res, adt_vis, adt_span, expansion);
712714
self.r.feed_visibility(feed, adt_vis);
713715
let def_id = feed.key();
714716

@@ -760,7 +762,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
760762
}
761763

762764
ItemKind::Mod(_, ident, ref mod_kind) => {
763-
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
765+
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
764766

765767
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
766768
self.r.mods_with_parse_errors.insert(def_id);
@@ -779,10 +781,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
779781
ItemKind::Const(box ConstItem { ident, .. })
780782
| ItemKind::Delegation(box Delegation { ident, .. })
781783
| ItemKind::Static(box StaticItem { 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
ItemKind::Fn(box Fn { ident, .. }) => {
785-
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
787+
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
786788

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

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

797799
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => {
798-
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
800+
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
799801

800802
self.parent_scope.module = self.r.new_module(
801803
Some(parent),
@@ -847,7 +849,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
847849
let feed = self.r.feed(ctor_node_id);
848850
let ctor_def_id = feed.key();
849851
let ctor_res = self.res(ctor_def_id);
850-
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
852+
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
851853
self.r.feed_visibility(feed, ctor_vis);
852854
// We need the field visibility spans also for the constructor for E0603.
853855
self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata.fields());
@@ -911,9 +913,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
911913
}
912914
.map(|module| {
913915
let used = self.process_macro_use_imports(item, module);
914-
let res = module.res().unwrap();
915-
let vis = ty::Visibility::<LocalDefId>::Public;
916-
let binding = (res, vis, sp, expansion).to_name_binding(self.r.arenas);
916+
let binding = self.r.arenas.new_pub_res_binding(module.res().unwrap(), sp, expansion);
917917
(used, Some(ModuleOrUniformRoot::Module(module)), binding)
918918
})
919919
.unwrap_or((true, None, self.r.dummy_binding));
@@ -970,7 +970,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
970970
);
971971
}
972972
}
973-
self.r.define(parent, ident, TypeNS, imported_binding);
973+
self.r.define_binding(parent, ident, TypeNS, imported_binding);
974974
}
975975

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

@@ -1227,7 +1227,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12271227
} else {
12281228
ty::Visibility::Restricted(CRATE_DEF_ID)
12291229
};
1230-
let binding = (res, vis, span, expansion).to_name_binding(self.r.arenas);
1230+
let binding = self.r.arenas.new_res_binding(res, vis.to_def_id(), span, expansion);
12311231
self.r.set_binding_parent_module(binding, parent_scope.module);
12321232
self.r.all_macro_rules.insert(ident.name);
12331233
if is_macro_export {
@@ -1246,7 +1246,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12461246
});
12471247
self.r.import_use_map.insert(import, Used::Other);
12481248
let import_binding = self.r.import(binding, import);
1249-
self.r.define(self.r.graph_root, ident, MacroNS, import_binding);
1249+
self.r.define_binding(self.r.graph_root, ident, MacroNS, import_binding);
12501250
} else {
12511251
self.r.check_reserved_macro_name(ident, res);
12521252
self.insert_unused_macro(ident, def_id, item.id);
@@ -1274,7 +1274,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12741274
if !vis.is_public() {
12751275
self.insert_unused_macro(ident, def_id, item.id);
12761276
}
1277-
self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
1277+
self.r.define(module, ident, MacroNS, res, vis, span, expansion);
12781278
self.r.feed_visibility(feed, vis);
12791279
self.parent_scope.macro_rules
12801280
}
@@ -1410,7 +1410,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14101410
if ctxt == AssocCtxt::Trait {
14111411
let parent = self.parent_scope.module;
14121412
let expansion = self.parent_scope.expansion;
1413-
self.r.define(parent, ident, ns, (self.res(def_id), vis, item.span, expansion));
1413+
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
14141414
} else if !matches!(&item.kind, AssocItemKind::Delegation(deleg) if deleg.from_glob) {
14151415
let impl_def_id = self.r.tcx.local_parent(local_def_id);
14161416
let key = BindingKey::new(ident.normalize_to_macros_2_0(), ns);
@@ -1495,7 +1495,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14951495
let feed = self.r.feed(variant.id);
14961496
let def_id = feed.key();
14971497
let vis = self.resolve_visibility(&variant.vis);
1498-
self.r.define(parent, ident, TypeNS, (self.res(def_id), vis, variant.span, expn_id));
1498+
self.r.define(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
14991499
self.r.feed_visibility(feed, vis);
15001500

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

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
@@ -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),
@@ -1233,6 +1223,32 @@ pub struct ResolverArenas<'ra> {
12331223
}
12341224

12351225
impl<'ra> ResolverArenas<'ra> {
1226+
fn new_res_binding(
1227+
&'ra self,
1228+
res: Res,
1229+
vis: ty::Visibility<DefId>,
1230+
span: Span,
1231+
expansion: LocalExpnId,
1232+
) -> NameBinding<'ra> {
1233+
self.alloc_name_binding(NameBindingData {
1234+
kind: NameBindingKind::Res(res),
1235+
ambiguity: None,
1236+
warn_ambiguity: false,
1237+
vis,
1238+
span,
1239+
expansion,
1240+
})
1241+
}
1242+
1243+
fn new_pub_res_binding(
1244+
&'ra self,
1245+
res: Res,
1246+
span: Span,
1247+
expn_id: LocalExpnId,
1248+
) -> NameBinding<'ra> {
1249+
self.new_res_binding(res, Visibility::Public, span, expn_id)
1250+
}
1251+
12361252
fn new_module(
12371253
&'ra self,
12381254
parent: Option<Module<'ra>>,
@@ -1256,9 +1272,8 @@ impl<'ra> ResolverArenas<'ra> {
12561272
}
12571273
if let Some(def_id) = def_id {
12581274
module_map.insert(def_id, module);
1259-
let vis = ty::Visibility::<DefId>::Public;
12601275
let res = module.res().unwrap();
1261-
let binding = (res, vis, module.span, LocalExpnId::ROOT).to_name_binding(self);
1276+
let binding = self.new_pub_res_binding(res, module.span, LocalExpnId::ROOT);
12621277
module_self_bindings.insert(module, binding);
12631278
}
12641279
module
@@ -1449,8 +1464,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14491464
}
14501465

14511466
let registered_tools = tcx.registered_tools(());
1452-
1453-
let pub_vis = ty::Visibility::<DefId>::Public;
14541467
let edition = tcx.sess.edition();
14551468

14561469
let mut resolver = Resolver {
@@ -1499,29 +1512,28 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14991512
macro_expanded_macro_export_errors: BTreeSet::new(),
15001513

15011514
arenas,
1502-
dummy_binding: (Res::Err, pub_vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(arenas),
1515+
dummy_binding: arenas.new_pub_res_binding(Res::Err, DUMMY_SP, LocalExpnId::ROOT),
15031516
builtin_types_bindings: PrimTy::ALL
15041517
.iter()
15051518
.map(|prim_ty| {
1506-
let binding = (Res::PrimTy(*prim_ty), pub_vis, DUMMY_SP, LocalExpnId::ROOT)
1507-
.to_name_binding(arenas);
1519+
let res = Res::PrimTy(*prim_ty);
1520+
let binding = arenas.new_pub_res_binding(res, DUMMY_SP, LocalExpnId::ROOT);
15081521
(prim_ty.name(), binding)
15091522
})
15101523
.collect(),
15111524
builtin_attrs_bindings: BUILTIN_ATTRIBUTES
15121525
.iter()
15131526
.map(|builtin_attr| {
15141527
let res = Res::NonMacroAttr(NonMacroAttrKind::Builtin(builtin_attr.name));
1515-
let binding =
1516-
(res, pub_vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(arenas);
1528+
let binding = arenas.new_pub_res_binding(res, DUMMY_SP, LocalExpnId::ROOT);
15171529
(builtin_attr.name, binding)
15181530
})
15191531
.collect(),
15201532
registered_tool_bindings: registered_tools
15211533
.iter()
15221534
.map(|ident| {
1523-
let binding = (Res::ToolMod, pub_vis, ident.span, LocalExpnId::ROOT)
1524-
.to_name_binding(arenas);
1535+
let res = Res::ToolMod;
1536+
let binding = arenas.new_pub_res_binding(res, ident.span, LocalExpnId::ROOT);
15251537
(*ident, binding)
15261538
})
15271539
.collect(),
@@ -2155,8 +2167,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21552167
self.crate_loader(|c| c.maybe_process_path_extern(ident.name))?
21562168
};
21572169
let res = Res::Def(DefKind::Mod, crate_id.as_def_id());
2158-
let vis = ty::Visibility::<DefId>::Public;
2159-
(res, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas)
2170+
self.arenas.new_pub_res_binding(res, DUMMY_SP, LocalExpnId::ROOT)
21602171
})
21612172
});
21622173

0 commit comments

Comments
 (0)