Skip to content

Commit 0162726

Browse files
authored
Rollup merge of #113332 - petrochenkov:bindintern, r=cjgillot
resolve: Use `Interned` for some interned structures Enough to get rid of all existing `ptr::eq`s and "partial" uses of `Interned`.
2 parents a105aa2 + 9f3fba8 commit 0162726

File tree

9 files changed

+224
-255
lines changed

9 files changed

+224
-255
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
//! Imports are also considered items and placed into modules here, but not resolved yet.
77
88
use crate::def_collector::collect_definitions;
9-
use crate::imports::{Import, ImportKind};
9+
use crate::imports::{ImportData, 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};
@@ -31,15 +31,14 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
3131
use rustc_span::Span;
3232

3333
use std::cell::Cell;
34-
use std::ptr;
3534

3635
type Res = def::Res<NodeId>;
3736

3837
impl<'a, Id: Into<DefId>> ToNameBinding<'a>
3938
for (Module<'a>, ty::Visibility<Id>, Span, LocalExpnId)
4039
{
41-
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
42-
arenas.alloc_name_binding(NameBinding {
40+
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> NameBinding<'a> {
41+
arenas.alloc_name_binding(NameBindingData {
4342
kind: NameBindingKind::Module(self.0),
4443
ambiguity: None,
4544
vis: self.1.to_def_id(),
@@ -50,8 +49,8 @@ impl<'a, Id: Into<DefId>> ToNameBinding<'a>
5049
}
5150

5251
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 {
52+
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> NameBinding<'a> {
53+
arenas.alloc_name_binding(NameBindingData {
5554
kind: NameBindingKind::Res(self.0),
5655
ambiguity: None,
5756
vis: self.1.to_def_id(),
@@ -71,7 +70,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
7170
let binding = def.to_name_binding(self.arenas);
7271
let key = self.new_disambiguated_key(ident, ns);
7372
if let Err(old_binding) = self.try_define(parent, key, binding) {
74-
self.report_conflict(parent, ident, ns, old_binding, &binding);
73+
self.report_conflict(parent, ident, ns, old_binding, binding);
7574
}
7675
}
7776

@@ -142,8 +141,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
142141
Some(def_id) => self.macro_def_scope(def_id),
143142
None => expn_id
144143
.as_local()
145-
.and_then(|expn_id| self.ast_transform_scopes.get(&expn_id))
146-
.unwrap_or(&self.graph_root),
144+
.and_then(|expn_id| self.ast_transform_scopes.get(&expn_id).copied())
145+
.unwrap_or(self.graph_root),
147146
}
148147
}
149148

@@ -354,7 +353,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
354353
vis: ty::Visibility,
355354
) {
356355
let current_module = self.parent_scope.module;
357-
let import = self.r.arenas.alloc_import(Import {
356+
let import = self.r.arenas.alloc_import(ImportData {
358357
kind,
359358
parent_scope: self.parent_scope,
360359
module_path,
@@ -378,7 +377,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
378377
if !type_ns_only || ns == TypeNS {
379378
let key = BindingKey::new(target, ns);
380379
let mut resolution = this.resolution(current_module, key).borrow_mut();
381-
resolution.add_single_import(import);
380+
resolution.single_imports.insert(import);
382381
}
383382
});
384383
}
@@ -848,7 +847,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
848847
(used, Some(ModuleOrUniformRoot::Module(module)), binding)
849848
})
850849
.unwrap_or((true, None, self.r.dummy_binding));
851-
let import = self.r.arenas.alloc_import(Import {
850+
let import = self.r.arenas.alloc_import(ImportData {
852851
kind: ImportKind::ExternCrate { source: orig_name, target: ident, id: item.id },
853852
root_id: item.id,
854853
parent_scope: self.parent_scope,
@@ -864,7 +863,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
864863
});
865864
self.r.potentially_unused_imports.push(import);
866865
let imported_binding = self.r.import(binding, import);
867-
if ptr::eq(parent, self.r.graph_root) {
866+
if parent == self.r.graph_root {
868867
if let Some(entry) = self.r.extern_prelude.get(&ident.normalize_to_macros_2_0()) {
869868
if expansion != LocalExpnId::ROOT
870869
&& orig_name.is_some()
@@ -996,7 +995,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
996995
fn add_macro_use_binding(
997996
&mut self,
998997
name: Symbol,
999-
binding: &'a NameBinding<'a>,
998+
binding: NameBinding<'a>,
1000999
span: Span,
10011000
allow_shadowing: bool,
10021001
) {
@@ -1058,7 +1057,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
10581057
}
10591058

10601059
let macro_use_import = |this: &Self, span| {
1061-
this.r.arenas.alloc_import(Import {
1060+
this.r.arenas.alloc_import(ImportData {
10621061
kind: ImportKind::MacroUse,
10631062
root_id: item.id,
10641063
parent_scope: this.parent_scope,
@@ -1228,7 +1227,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
12281227
self.r.set_binding_parent_module(binding, parent_scope.module);
12291228
self.r.all_macro_rules.insert(ident.name, res);
12301229
if is_macro_export {
1231-
let import = self.r.arenas.alloc_import(Import {
1230+
let import = self.r.arenas.alloc_import(ImportData {
12321231
kind: ImportKind::MacroExport,
12331232
root_id: item.id,
12341233
parent_scope: self.parent_scope,

compiler/rustc_resolve/src/diagnostics.rs

+28-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::ptr;
2-
31
use rustc_ast::expand::StrippedCfgItem;
42
use rustc_ast::ptr::P;
53
use rustc_ast::visit::{self, Visitor};
@@ -182,13 +180,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
182180
}
183181
}
184182

185-
pub(crate) fn report_conflict<'b>(
183+
pub(crate) fn report_conflict(
186184
&mut self,
187185
parent: Module<'_>,
188186
ident: Ident,
189187
ns: Namespace,
190-
new_binding: &NameBinding<'b>,
191-
old_binding: &NameBinding<'b>,
188+
new_binding: NameBinding<'a>,
189+
old_binding: NameBinding<'a>,
192190
) {
193191
// Error on the second of two conflicting names
194192
if old_binding.span.lo() > new_binding.span.lo() {
@@ -262,7 +260,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
262260

263261
// See https://github.com/rust-lang/rust/issues/32354
264262
use NameBindingKind::Import;
265-
let can_suggest = |binding: &NameBinding<'_>, import: &self::Import<'_>| {
263+
let can_suggest = |binding: NameBinding<'_>, import: self::Import<'_>| {
266264
!binding.span.is_dummy()
267265
&& !matches!(import.kind, ImportKind::MacroUse | ImportKind::MacroExport)
268266
};
@@ -272,22 +270,22 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
272270
(Import { import: new, .. }, Import { import: old, .. })
273271
if {
274272
(new.has_attributes || old.has_attributes)
275-
&& can_suggest(old_binding, old)
276-
&& can_suggest(new_binding, new)
273+
&& can_suggest(old_binding, *old)
274+
&& can_suggest(new_binding, *new)
277275
} =>
278276
{
279277
if old.has_attributes {
280-
Some((new, new_binding.span, true))
278+
Some((*new, new_binding.span, true))
281279
} else {
282-
Some((old, old_binding.span, true))
280+
Some((*old, old_binding.span, true))
283281
}
284282
}
285283
// Otherwise prioritize the new binding.
286-
(Import { import, .. }, other) if can_suggest(new_binding, import) => {
287-
Some((import, new_binding.span, other.is_import()))
284+
(Import { import, .. }, other) if can_suggest(new_binding, *import) => {
285+
Some((*import, new_binding.span, other.is_import()))
288286
}
289-
(other, Import { import, .. }) if can_suggest(old_binding, import) => {
290-
Some((import, old_binding.span, other.is_import()))
287+
(other, Import { import, .. }) if can_suggest(old_binding, *import) => {
288+
Some((*import, old_binding.span, other.is_import()))
291289
}
292290
_ => None,
293291
};
@@ -341,7 +339,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
341339
&self,
342340
err: &mut Diagnostic,
343341
name: Symbol,
344-
import: &Import<'_>,
342+
import: Import<'_>,
345343
binding_span: Span,
346344
) {
347345
let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
@@ -413,7 +411,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
413411
fn add_suggestion_for_duplicate_nested_use(
414412
&self,
415413
err: &mut Diagnostic,
416-
import: &Import<'_>,
414+
import: Import<'_>,
417415
binding_span: Span,
418416
) {
419417
assert!(import.is_nested());
@@ -455,7 +453,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
455453
&mut self,
456454
finalize: Option<Finalize>,
457455
path: &[Segment],
458-
second_binding: Option<&NameBinding<'_>>,
456+
second_binding: Option<NameBinding<'_>>,
459457
) {
460458
let Some(Finalize { node_id, root_span, .. }) = finalize else {
461459
return;
@@ -1198,7 +1196,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11981196
// avoid suggesting anything with a hygienic name
11991197
if ident.name == lookup_ident.name
12001198
&& ns == namespace
1201-
&& !ptr::eq(in_module, parent_scope.module)
1199+
&& in_module != parent_scope.module
12021200
&& !ident.span.normalize_to_macros_2_0().from_expansion()
12031201
{
12041202
let res = name_binding.res();
@@ -1515,7 +1513,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15151513
true
15161514
}
15171515

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

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

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

15961594
/// If the binding refers to a tuple struct constructor with fields,
15971595
/// returns the span of its fields.
1598-
fn ctor_fields_span(&self, binding: &NameBinding<'_>) -> Option<Span> {
1596+
fn ctor_fields_span(&self, binding: NameBinding<'_>) -> Option<Span> {
15991597
if let NameBindingKind::Res(Res::Def(
16001598
DefKind::Ctor(CtorOf::Struct, CtorKind::Fn),
16011599
ctor_def_id,
@@ -1622,7 +1620,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16221620
if ctor_fields_span.is_some() { plain_descr + " constructor" } else { plain_descr };
16231621
let import_descr = nonimport_descr.clone() + " import";
16241622
let get_descr =
1625-
|b: &NameBinding<'_>| if b.is_import() { &import_descr } else { &nonimport_descr };
1623+
|b: NameBinding<'_>| if b.is_import() { &import_descr } else { &nonimport_descr };
16261624

16271625
// Print the primary message.
16281626
let descr = get_descr(binding);
@@ -1702,7 +1700,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
17021700
_ => None,
17031701
};
17041702

1705-
let first = ptr::eq(binding, first_binding);
1703+
let first = binding == first_binding;
17061704
let msg = format!(
17071705
"{and_refers_to}the {item} `{name}`{which} is defined here{dots}",
17081706
and_refers_to = if first { "" } else { "...and refers to " },
@@ -1732,7 +1730,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
17321730
pub(crate) fn find_similarly_named_module_or_crate(
17331731
&mut self,
17341732
ident: Symbol,
1735-
current_module: &Module<'a>,
1733+
current_module: Module<'a>,
17361734
) -> Option<Symbol> {
17371735
let mut candidates = self
17381736
.extern_prelude
@@ -1742,7 +1740,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
17421740
self.module_map
17431741
.iter()
17441742
.filter(|(_, module)| {
1745-
current_module.is_ancestor_of(module) && !ptr::eq(current_module, *module)
1743+
current_module.is_ancestor_of(**module) && current_module != **module
17461744
})
17471745
.flat_map(|(_, module)| module.kind.name()),
17481746
)
@@ -1762,7 +1760,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
17621760
opt_ns: Option<Namespace>, // `None` indicates a module path in import
17631761
parent_scope: &ParentScope<'a>,
17641762
ribs: Option<&PerNS<Vec<Rib<'a>>>>,
1765-
ignore_binding: Option<&'a NameBinding<'a>>,
1763+
ignore_binding: Option<NameBinding<'a>>,
17661764
module: Option<ModuleOrUniformRoot<'a>>,
17671765
failed_segment_idx: usize,
17681766
ident: Ident,
@@ -1945,7 +1943,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
19451943
}
19461944

19471945
suggestion = suggestion.or_else(|| {
1948-
self.find_similarly_named_module_or_crate(ident.name, &parent_scope.module).map(
1946+
self.find_similarly_named_module_or_crate(ident.name, parent_scope.module).map(
19491947
|sugg| {
19501948
(
19511949
vec![(ident.span, sugg.to_string())],
@@ -2114,7 +2112,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
21142112
/// ```
21152113
pub(crate) fn check_for_module_export_macro(
21162114
&mut self,
2117-
import: &'a Import<'a>,
2115+
import: Import<'a>,
21182116
module: ModuleOrUniformRoot<'a>,
21192117
ident: Ident,
21202118
) -> Option<(Option<Suggestion>, Option<String>)> {
@@ -2126,9 +2124,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
21262124
crate_module = parent;
21272125
}
21282126

2129-
if ModuleOrUniformRoot::same_def(ModuleOrUniformRoot::Module(crate_module), module) {
2130-
// Don't make a suggestion if the import was already from the root of the
2131-
// crate.
2127+
if module == ModuleOrUniformRoot::Module(crate_module) {
2128+
// Don't make a suggestion if the import was already from the root of the crate.
21322129
return None;
21332130
}
21342131

0 commit comments

Comments
 (0)