Skip to content

Commit 8efd9cc

Browse files
committed
resolve: Use Interned for NameBinding
1 parent 6dab6dc commit 8efd9cc

File tree

8 files changed

+106
-124
lines changed

8 files changed

+106
-124
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::def_collector::collect_definitions;
99
use crate::imports::{Import, ImportKind};
1010
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
1111
use crate::Namespace::{self, MacroNS, TypeNS, ValueNS};
12-
use crate::{errors, BindingKey, MacroData};
12+
use crate::{errors, BindingKey, MacroData, NameBindingData};
1313
use crate::{Determinacy, ExternPreludeEntry, Finalize, Module, ModuleKind, ModuleOrUniformRoot};
1414
use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PerNS, ResolutionError};
1515
use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError};
@@ -38,8 +38,8 @@ type Res = def::Res<NodeId>;
3838
impl<'a, Id: Into<DefId>> ToNameBinding<'a>
3939
for (Module<'a>, ty::Visibility<Id>, Span, LocalExpnId)
4040
{
41-
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
42-
arenas.alloc_name_binding(NameBinding {
41+
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> NameBinding<'a> {
42+
arenas.alloc_name_binding(NameBindingData {
4343
kind: NameBindingKind::Module(self.0),
4444
ambiguity: None,
4545
vis: self.1.to_def_id(),
@@ -50,8 +50,8 @@ impl<'a, Id: Into<DefId>> ToNameBinding<'a>
5050
}
5151

5252
impl<'a, Id: Into<DefId>> ToNameBinding<'a> for (Res, ty::Visibility<Id>, Span, LocalExpnId) {
53-
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
54-
arenas.alloc_name_binding(NameBinding {
53+
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> NameBinding<'a> {
54+
arenas.alloc_name_binding(NameBindingData {
5555
kind: NameBindingKind::Res(self.0),
5656
ambiguity: None,
5757
vis: self.1.to_def_id(),
@@ -71,7 +71,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
7171
let binding = def.to_name_binding(self.arenas);
7272
let key = self.new_disambiguated_key(ident, ns);
7373
if let Err(old_binding) = self.try_define(parent, key, binding) {
74-
self.report_conflict(parent, ident, ns, old_binding, &binding);
74+
self.report_conflict(parent, ident, ns, old_binding, binding);
7575
}
7676
}
7777

@@ -996,7 +996,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
996996
fn add_macro_use_binding(
997997
&mut self,
998998
name: Symbol,
999-
binding: &'a NameBinding<'a>,
999+
binding: NameBinding<'a>,
10001000
span: Span,
10011001
allow_shadowing: bool,
10021002
) {

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
182182
}
183183
}
184184

185-
pub(crate) fn report_conflict<'b>(
185+
pub(crate) fn report_conflict(
186186
&mut self,
187187
parent: Module<'_>,
188188
ident: Ident,
189189
ns: Namespace,
190-
new_binding: &NameBinding<'b>,
191-
old_binding: &NameBinding<'b>,
190+
new_binding: NameBinding<'a>,
191+
old_binding: NameBinding<'a>,
192192
) {
193193
// Error on the second of two conflicting names
194194
if old_binding.span.lo() > new_binding.span.lo() {
@@ -262,7 +262,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
262262

263263
// See https://github.com/rust-lang/rust/issues/32354
264264
use NameBindingKind::Import;
265-
let can_suggest = |binding: &NameBinding<'_>, import: &self::Import<'_>| {
265+
let can_suggest = |binding: NameBinding<'_>, import: &self::Import<'_>| {
266266
!binding.span.is_dummy()
267267
&& !matches!(import.kind, ImportKind::MacroUse | ImportKind::MacroExport)
268268
};
@@ -455,7 +455,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
455455
&mut self,
456456
finalize: Option<Finalize>,
457457
path: &[Segment],
458-
second_binding: Option<&NameBinding<'_>>,
458+
second_binding: Option<NameBinding<'_>>,
459459
) {
460460
let Some(Finalize { node_id, root_span, .. }) = finalize else {
461461
return;
@@ -1515,7 +1515,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15151515
true
15161516
}
15171517

1518-
fn binding_description(&self, b: &NameBinding<'_>, ident: Ident, from_prelude: bool) -> String {
1518+
fn binding_description(&self, b: NameBinding<'_>, ident: Ident, from_prelude: bool) -> String {
15191519
let res = b.res();
15201520
if b.span.is_dummy() || !self.tcx.sess.source_map().is_span_accessible(b.span) {
15211521
// These already contain the "built-in" prefix or look bad with it.
@@ -1555,7 +1555,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15551555
err.span_label(ident.span, "ambiguous name");
15561556
err.note(format!("ambiguous because of {}", kind.descr()));
15571557

1558-
let mut could_refer_to = |b: &NameBinding<'_>, misc: AmbiguityErrorMisc, also: &str| {
1558+
let mut could_refer_to = |b: NameBinding<'_>, misc: AmbiguityErrorMisc, also: &str| {
15591559
let what = self.binding_description(b, ident, misc == AmbiguityErrorMisc::FromPrelude);
15601560
let note_msg = format!("`{ident}` could{also} refer to {what}");
15611561

@@ -1595,7 +1595,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15951595

15961596
/// If the binding refers to a tuple struct constructor with fields,
15971597
/// returns the span of its fields.
1598-
fn ctor_fields_span(&self, binding: &NameBinding<'_>) -> Option<Span> {
1598+
fn ctor_fields_span(&self, binding: NameBinding<'_>) -> Option<Span> {
15991599
if let NameBindingKind::Res(Res::Def(
16001600
DefKind::Ctor(CtorOf::Struct, CtorKind::Fn),
16011601
ctor_def_id,
@@ -1622,7 +1622,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16221622
if ctor_fields_span.is_some() { plain_descr + " constructor" } else { plain_descr };
16231623
let import_descr = nonimport_descr.clone() + " import";
16241624
let get_descr =
1625-
|b: &NameBinding<'_>| if b.is_import() { &import_descr } else { &nonimport_descr };
1625+
|b: NameBinding<'_>| if b.is_import() { &import_descr } else { &nonimport_descr };
16261626

16271627
// Print the primary message.
16281628
let descr = get_descr(binding);
@@ -1702,7 +1702,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
17021702
_ => None,
17031703
};
17041704

1705-
let first = ptr::eq(binding, first_binding);
1705+
let first = binding == first_binding;
17061706
let msg = format!(
17071707
"{and_refers_to}the {item} `{name}`{which} is defined here{dots}",
17081708
and_refers_to = if first { "" } else { "...and refers to " },
@@ -1762,7 +1762,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
17621762
opt_ns: Option<Namespace>, // `None` indicates a module path in import
17631763
parent_scope: &ParentScope<'a>,
17641764
ribs: Option<&PerNS<Vec<Rib<'a>>>>,
1765-
ignore_binding: Option<&'a NameBinding<'a>>,
1765+
ignore_binding: Option<NameBinding<'a>>,
17661766
module: Option<ModuleOrUniformRoot<'a>>,
17671767
failed_segment_idx: usize,
17681768
ident: Ident,

compiler/rustc_resolve/src/effective_visibilities.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@ use rustc_ast::visit::Visitor;
55
use rustc_ast::Crate;
66
use rustc_ast::EnumDef;
77
use rustc_data_structures::fx::FxHashSet;
8-
use rustc_data_structures::intern::Interned;
98
use rustc_hir::def_id::LocalDefId;
109
use rustc_hir::def_id::CRATE_DEF_ID;
1110
use rustc_middle::middle::privacy::Level;
1211
use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility};
1312
use rustc_middle::ty::Visibility;
1413
use std::mem;
1514

16-
type ImportId<'a> = Interned<'a, NameBinding<'a>>;
17-
1815
#[derive(Clone, Copy)]
1916
enum ParentId<'a> {
2017
Def(LocalDefId),
21-
Import(ImportId<'a>),
18+
Import(NameBinding<'a>),
2219
}
2320

2421
impl ParentId<'_> {
@@ -36,7 +33,7 @@ pub(crate) struct EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
3633
/// While walking import chains we need to track effective visibilities per-binding, and def id
3734
/// keys in `Resolver::effective_visibilities` are not enough for that, because multiple
3835
/// bindings can correspond to a single def id in imports. So we keep a separate table.
39-
import_effective_visibilities: EffectiveVisibilities<ImportId<'a>>,
36+
import_effective_visibilities: EffectiveVisibilities<NameBinding<'a>>,
4037
// It's possible to recalculate this at any point, but it's relatively expensive.
4138
current_private_vis: Visibility,
4239
changed: bool,
@@ -47,7 +44,7 @@ impl Resolver<'_, '_> {
4744
self.get_nearest_non_block_module(def_id.to_def_id()).nearest_parent_mod().expect_local()
4845
}
4946

50-
fn private_vis_import(&mut self, binding: ImportId<'_>) -> Visibility {
47+
fn private_vis_import(&mut self, binding: NameBinding<'_>) -> Visibility {
5148
let NameBindingKind::Import { import, .. } = binding.kind else { unreachable!() };
5249
Visibility::Restricted(
5350
import
@@ -75,7 +72,7 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
7572
pub(crate) fn compute_effective_visibilities<'c>(
7673
r: &'r mut Resolver<'a, 'tcx>,
7774
krate: &'c Crate,
78-
) -> FxHashSet<Interned<'a, NameBinding<'a>>> {
75+
) -> FxHashSet<NameBinding<'a>> {
7976
let mut visitor = EffectiveVisibilitiesVisitor {
8077
r,
8178
def_effective_visibilities: Default::default(),
@@ -133,8 +130,7 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
133130
// lint. For all bindings added to the table this way `is_ambiguity` returns true.
134131
let mut parent_id = ParentId::Def(module_id);
135132
while let NameBindingKind::Import { binding: nested_binding, .. } = binding.kind {
136-
let binding_id = ImportId::new_unchecked(binding);
137-
self.update_import(binding_id, parent_id);
133+
self.update_import(binding, parent_id);
138134

139135
if binding.ambiguity.is_some() {
140136
// Stop at the root ambiguity, further bindings in the chain should not
@@ -143,7 +139,7 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
143139
break;
144140
}
145141

146-
parent_id = ParentId::Import(binding_id);
142+
parent_id = ParentId::Import(binding);
147143
binding = nested_binding;
148144
}
149145

@@ -192,7 +188,7 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
192188
}
193189
}
194190

195-
fn update_import(&mut self, binding: ImportId<'a>, parent_id: ParentId<'a>) {
191+
fn update_import(&mut self, binding: NameBinding<'a>, parent_id: ParentId<'a>) {
196192
let nominal_vis = binding.vis.expect_local();
197193
let Some(cheap_private_vis) = self.may_update(nominal_vis, parent_id) else { return };
198194
let inherited_eff_vis = self.effective_vis_or_private(parent_id);

compiler/rustc_resolve/src/ident.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
284284
parent_scope: &ParentScope<'a>,
285285
finalize: Option<Finalize>,
286286
ribs: &[Rib<'a>],
287-
ignore_binding: Option<&'a NameBinding<'a>>,
287+
ignore_binding: Option<NameBinding<'a>>,
288288
) -> Option<LexicalScopeBinding<'a>> {
289289
assert!(ns == TypeNS || ns == ValueNS);
290290
let orig_ident = ident;
@@ -378,8 +378,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
378378
parent_scope: &ParentScope<'a>,
379379
finalize: Option<Finalize>,
380380
force: bool,
381-
ignore_binding: Option<&'a NameBinding<'a>>,
382-
) -> Result<&'a NameBinding<'a>, Determinacy> {
381+
ignore_binding: Option<NameBinding<'a>>,
382+
) -> Result<NameBinding<'a>, Determinacy> {
383383
bitflags::bitflags! {
384384
struct Flags: u8 {
385385
const MACRO_RULES = 1 << 0;
@@ -415,7 +415,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
415415
// }
416416
// So we have to save the innermost solution and continue searching in outer scopes
417417
// to detect potential ambiguities.
418-
let mut innermost_result: Option<(&NameBinding<'_>, Flags)> = None;
418+
let mut innermost_result: Option<(NameBinding<'_>, Flags)> = None;
419419
let mut determinacy = Determinacy::Determined;
420420

421421
// Go through all the scopes and try to resolve the name.
@@ -717,7 +717,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
717717
ident: Ident,
718718
ns: Namespace,
719719
parent_scope: &ParentScope<'a>,
720-
) -> Result<&'a NameBinding<'a>, Determinacy> {
720+
) -> Result<NameBinding<'a>, Determinacy> {
721721
self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, None, None)
722722
.map_err(|(determinacy, _)| determinacy)
723723
}
@@ -730,8 +730,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
730730
ns: Namespace,
731731
parent_scope: &ParentScope<'a>,
732732
finalize: Option<Finalize>,
733-
ignore_binding: Option<&'a NameBinding<'a>>,
734-
) -> Result<&'a NameBinding<'a>, Determinacy> {
733+
ignore_binding: Option<NameBinding<'a>>,
734+
) -> Result<NameBinding<'a>, Determinacy> {
735735
self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, finalize, ignore_binding)
736736
.map_err(|(determinacy, _)| determinacy)
737737
}
@@ -744,8 +744,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
744744
ns: Namespace,
745745
parent_scope: &ParentScope<'a>,
746746
finalize: Option<Finalize>,
747-
ignore_binding: Option<&'a NameBinding<'a>>,
748-
) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
747+
ignore_binding: Option<NameBinding<'a>>,
748+
) -> Result<NameBinding<'a>, (Determinacy, Weak)> {
749749
let tmp_parent_scope;
750750
let mut adjusted_parent_scope = parent_scope;
751751
match module {
@@ -782,8 +782,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
782782
ns: Namespace,
783783
parent_scope: &ParentScope<'a>,
784784
finalize: Option<Finalize>,
785-
ignore_binding: Option<&'a NameBinding<'a>>,
786-
) -> Result<&'a NameBinding<'a>, Determinacy> {
785+
ignore_binding: Option<NameBinding<'a>>,
786+
) -> Result<NameBinding<'a>, Determinacy> {
787787
self.resolve_ident_in_module_unadjusted_ext(
788788
module,
789789
ident,
@@ -809,8 +809,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
809809
finalize: Option<Finalize>,
810810
// This binding should be ignored during in-module resolution, so that we don't get
811811
// "self-confirming" import resolutions during import validation and checking.
812-
ignore_binding: Option<&'a NameBinding<'a>>,
813-
) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
812+
ignore_binding: Option<NameBinding<'a>>,
813+
) -> Result<NameBinding<'a>, (Determinacy, Weak)> {
814814
let module = match module {
815815
ModuleOrUniformRoot::Module(module) => module,
816816
ModuleOrUniformRoot::CrateRootAndExternPrelude => {
@@ -873,13 +873,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
873873
// binding if it exists. What we really want here is having two separate scopes in
874874
// a module - one for non-globs and one for globs, but until that's done use this
875875
// hack to avoid inconsistent resolution ICEs during import validation.
876-
let binding =
877-
[resolution.binding, resolution.shadowed_glob].into_iter().find_map(|binding| {
878-
match (binding, ignore_binding) {
879-
(Some(binding), Some(ignored)) if ptr::eq(binding, ignored) => None,
880-
_ => binding,
881-
}
882-
});
876+
let binding = [resolution.binding, resolution.shadowed_glob]
877+
.into_iter()
878+
.find_map(|binding| if binding == ignore_binding { None } else { binding });
883879

884880
if let Some(Finalize { path_span, report_private, .. }) = finalize {
885881
let Some(binding) = binding else {
@@ -930,7 +926,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
930926
return Ok(binding);
931927
}
932928

933-
let check_usable = |this: &mut Self, binding: &'a NameBinding<'a>| {
929+
let check_usable = |this: &mut Self, binding: NameBinding<'a>| {
934930
let usable = this.is_accessible_from(binding.vis, parent_scope.module);
935931
if usable { Ok(binding) } else { Err((Determined, Weak::No)) }
936932
};
@@ -1352,7 +1348,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13521348
opt_ns: Option<Namespace>, // `None` indicates a module path in import
13531349
parent_scope: &ParentScope<'a>,
13541350
finalize: Option<Finalize>,
1355-
ignore_binding: Option<&'a NameBinding<'a>>,
1351+
ignore_binding: Option<NameBinding<'a>>,
13561352
) -> PathResult<'a> {
13571353
self.resolve_path_with_ribs(path, opt_ns, parent_scope, finalize, None, ignore_binding)
13581354
}
@@ -1364,7 +1360,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13641360
parent_scope: &ParentScope<'a>,
13651361
finalize: Option<Finalize>,
13661362
ribs: Option<&PerNS<Vec<Rib<'a>>>>,
1367-
ignore_binding: Option<&'a NameBinding<'a>>,
1363+
ignore_binding: Option<NameBinding<'a>>,
13681364
) -> PathResult<'a> {
13691365
let mut module = None;
13701366
let mut allow_super = true;

0 commit comments

Comments
 (0)