Skip to content

Commit 4abdaeb

Browse files
committed
resolve: Use Interned for Import
1 parent 8efd9cc commit 4abdaeb

File tree

5 files changed

+58
-71
lines changed

5 files changed

+58
-71
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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};
1212
use crate::{errors, BindingKey, MacroData, NameBindingData};
@@ -354,7 +354,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
354354
vis: ty::Visibility,
355355
) {
356356
let current_module = self.parent_scope.module;
357-
let import = self.r.arenas.alloc_import(Import {
357+
let import = self.r.arenas.alloc_import(ImportData {
358358
kind,
359359
parent_scope: self.parent_scope,
360360
module_path,
@@ -378,7 +378,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
378378
if !type_ns_only || ns == TypeNS {
379379
let key = BindingKey::new(target, ns);
380380
let mut resolution = this.resolution(current_module, key).borrow_mut();
381-
resolution.add_single_import(import);
381+
resolution.single_imports.insert(import);
382382
}
383383
});
384384
}
@@ -848,7 +848,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
848848
(used, Some(ModuleOrUniformRoot::Module(module)), binding)
849849
})
850850
.unwrap_or((true, None, self.r.dummy_binding));
851-
let import = self.r.arenas.alloc_import(Import {
851+
let import = self.r.arenas.alloc_import(ImportData {
852852
kind: ImportKind::ExternCrate { source: orig_name, target: ident, id: item.id },
853853
root_id: item.id,
854854
parent_scope: self.parent_scope,
@@ -1058,7 +1058,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
10581058
}
10591059

10601060
let macro_use_import = |this: &Self, span| {
1061-
this.r.arenas.alloc_import(Import {
1061+
this.r.arenas.alloc_import(ImportData {
10621062
kind: ImportKind::MacroUse,
10631063
root_id: item.id,
10641064
parent_scope: this.parent_scope,
@@ -1228,7 +1228,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
12281228
self.r.set_binding_parent_module(binding, parent_scope.module);
12291229
self.r.all_macro_rules.insert(ident.name, res);
12301230
if is_macro_export {
1231-
let import = self.r.arenas.alloc_import(Import {
1231+
let import = self.r.arenas.alloc_import(ImportData {
12321232
kind: ImportKind::MacroExport,
12331233
root_id: item.id,
12341234
parent_scope: self.parent_scope,

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};
@@ -272,22 +272,22 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
272272
(Import { import: new, .. }, Import { import: old, .. })
273273
if {
274274
(new.has_attributes || old.has_attributes)
275-
&& can_suggest(old_binding, old)
276-
&& can_suggest(new_binding, new)
275+
&& can_suggest(old_binding, *old)
276+
&& can_suggest(new_binding, *new)
277277
} =>
278278
{
279279
if old.has_attributes {
280-
Some((new, new_binding.span, true))
280+
Some((*new, new_binding.span, true))
281281
} else {
282-
Some((old, old_binding.span, true))
282+
Some((*old, old_binding.span, true))
283283
}
284284
}
285285
// Otherwise prioritize the new binding.
286-
(Import { import, .. }, other) if can_suggest(new_binding, import) => {
287-
Some((import, new_binding.span, other.is_import()))
286+
(Import { import, .. }, other) if can_suggest(new_binding, *import) => {
287+
Some((*import, new_binding.span, other.is_import()))
288288
}
289-
(other, Import { import, .. }) if can_suggest(old_binding, import) => {
290-
Some((import, old_binding.span, other.is_import()))
289+
(other, Import { import, .. }) if can_suggest(old_binding, *import) => {
290+
Some((*import, old_binding.span, other.is_import()))
291291
}
292292
_ => None,
293293
};
@@ -341,7 +341,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
341341
&self,
342342
err: &mut Diagnostic,
343343
name: Symbol,
344-
import: &Import<'_>,
344+
import: Import<'_>,
345345
binding_span: Span,
346346
) {
347347
let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
@@ -413,7 +413,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
413413
fn add_suggestion_for_duplicate_nested_use(
414414
&self,
415415
err: &mut Diagnostic,
416-
import: &Import<'_>,
416+
import: Import<'_>,
417417
binding_span: Span,
418418
) {
419419
assert!(import.is_nested());
@@ -2114,7 +2114,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
21142114
/// ```
21152115
pub(crate) fn check_for_module_export_macro(
21162116
&mut self,
2117-
import: &'a Import<'a>,
2117+
import: Import<'a>,
21182118
module: ModuleOrUniformRoot<'a>,
21192119
ident: Ident,
21202120
) -> Option<(Option<Suggestion>, Option<String>)> {

compiler/rustc_resolve/src/ident.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::late::{
2020
use crate::macros::{sub_namespace_match, MacroRulesScope};
2121
use crate::BindingKey;
2222
use crate::{errors, AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
23-
use crate::{Import, ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
23+
use crate::{ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
2424
use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res};
2525
use crate::{ResolutionError, Resolver, Scope, ScopeSet, Segment, ToNameBinding, Weak};
2626

@@ -913,11 +913,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
913913
}
914914

915915
if !restricted_shadowing && binding.expansion != LocalExpnId::ROOT {
916-
if let NameBindingKind::Import {
917-
import: Import { kind: ImportKind::MacroExport, .. },
918-
..
919-
} = binding.kind
920-
{
916+
if let NameBindingKind::Import { import, .. } = binding.kind
917+
&& matches!(import.kind, ImportKind::MacroExport) {
921918
self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
922919
}
923920
}
@@ -951,7 +948,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
951948
}
952949
if let Some(ignored) = ignore_binding &&
953950
let NameBindingKind::Import { import, .. } = ignored.kind &&
954-
ptr::eq(import, &**single_import) {
951+
import == *single_import {
955952
// Ignore not just the binding itself, but if it has a shadowed_glob,
956953
// ignore that, too, because this loop is supposed to only process
957954
// named imports.

compiler/rustc_resolve/src/imports.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'a> std::fmt::Debug for ImportKind<'a> {
135135

136136
/// One import.
137137
#[derive(Debug, Clone)]
138-
pub(crate) struct Import<'a> {
138+
pub(crate) struct ImportData<'a> {
139139
pub kind: ImportKind<'a>,
140140

141141
/// Node ID of the "root" use item -- this is always the same as `ImportKind`'s `id`
@@ -172,7 +172,9 @@ pub(crate) struct Import<'a> {
172172
pub used: Cell<bool>,
173173
}
174174

175-
impl<'a> Import<'a> {
175+
pub(crate) type Import<'a> = Interned<'a, ImportData<'a>>;
176+
177+
impl<'a> ImportData<'a> {
176178
pub(crate) fn is_glob(&self) -> bool {
177179
matches!(self.kind, ImportKind::Glob { .. })
178180
}
@@ -214,7 +216,7 @@ impl<'a> Import<'a> {
214216
pub(crate) struct NameResolution<'a> {
215217
/// Single imports that may define the name in the namespace.
216218
/// Imports are arena-allocated, so it's ok to use pointers as keys.
217-
pub single_imports: FxHashSet<Interned<'a, Import<'a>>>,
219+
pub single_imports: FxHashSet<Import<'a>>,
218220
/// The least shadowable known binding for this name, or None if there are no known bindings.
219221
pub binding: Option<NameBinding<'a>>,
220222
pub shadowed_glob: Option<NameBinding<'a>>,
@@ -231,10 +233,6 @@ impl<'a> NameResolution<'a> {
231233
}
232234
})
233235
}
234-
235-
pub(crate) fn add_single_import(&mut self, import: &'a Import<'a>) {
236-
self.single_imports.insert(Interned::new_unchecked(import));
237-
}
238236
}
239237

240238
/// An error that may be transformed into a diagnostic later. Used to combine multiple unresolved
@@ -250,27 +248,20 @@ struct UnresolvedImportError {
250248

251249
// Reexports of the form `pub use foo as bar;` where `foo` is `extern crate foo;`
252250
// are permitted for backward-compatibility under a deprecation lint.
253-
fn pub_use_of_private_extern_crate_hack(import: &Import<'_>, binding: NameBinding<'_>) -> bool {
251+
fn pub_use_of_private_extern_crate_hack(import: Import<'_>, binding: NameBinding<'_>) -> bool {
254252
match (&import.kind, &binding.kind) {
255-
(
256-
ImportKind::Single { .. },
257-
NameBindingKind::Import {
258-
import: Import { kind: ImportKind::ExternCrate { .. }, .. },
259-
..
260-
},
261-
) => import.expect_vis().is_public(),
253+
(ImportKind::Single { .. }, NameBindingKind::Import { import: binding_import, .. }) => {
254+
matches!(binding_import.kind, ImportKind::ExternCrate { .. })
255+
&& import.expect_vis().is_public()
256+
}
262257
_ => false,
263258
}
264259
}
265260

266261
impl<'a, 'tcx> Resolver<'a, 'tcx> {
267262
/// Given a binding and an import that resolves to it,
268263
/// return the corresponding binding defined by the import.
269-
pub(crate) fn import(
270-
&self,
271-
binding: NameBinding<'a>,
272-
import: &'a Import<'a>,
273-
) -> NameBinding<'a> {
264+
pub(crate) fn import(&self, binding: NameBinding<'a>, import: Import<'a>) -> NameBinding<'a> {
274265
let import_vis = import.expect_vis().to_def_id();
275266
let vis = if binding.vis.is_at_least(import_vis, self.tcx)
276267
|| pub_use_of_private_extern_crate_hack(import, binding)
@@ -411,7 +402,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
411402
None => continue,
412403
};
413404
if self.is_accessible_from(binding.vis, scope) {
414-
let imported_binding = self.import(binding, import);
405+
let imported_binding = self.import(binding, *import);
415406
let key = BindingKey { ident, ..key };
416407
let _ = self.try_define(import.parent_scope.module, key, imported_binding);
417408
}
@@ -422,7 +413,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
422413

423414
// Define a dummy resolution containing a `Res::Err` as a placeholder for a failed
424415
// or indeterminate resolution, also mark such failed imports as used to avoid duplicate diagnostics.
425-
fn import_dummy_binding(&mut self, import: &'a Import<'a>, is_indeterminate: bool) {
416+
fn import_dummy_binding(&mut self, import: Import<'a>, is_indeterminate: bool) {
426417
if let ImportKind::Single { target, ref target_bindings, .. } = import.kind {
427418
if !(is_indeterminate || target_bindings.iter().all(|binding| binding.get().is_none()))
428419
{
@@ -460,7 +451,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
460451
prev_indeterminate_count = indeterminate_count;
461452
indeterminate_count = 0;
462453
for import in mem::take(&mut self.indeterminate_imports) {
463-
let import_indeterminate_count = self.resolve_import(&import);
454+
let import_indeterminate_count = self.resolve_import(import);
464455
indeterminate_count += import_indeterminate_count;
465456
match import_indeterminate_count {
466457
0 => self.determined_imports.push(import),
@@ -609,7 +600,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
609600
}
610601
}
611602

612-
fn throw_unresolved_import_error(&mut self, errors: Vec<(&Import<'_>, UnresolvedImportError)>) {
603+
fn throw_unresolved_import_error(&mut self, errors: Vec<(Import<'_>, UnresolvedImportError)>) {
613604
if errors.is_empty() {
614605
return;
615606
}
@@ -701,7 +692,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
701692
///
702693
/// Meanwhile, if resolve successful, the resolved bindings are written
703694
/// into the module.
704-
fn resolve_import(&mut self, import: &'a Import<'a>) -> usize {
695+
fn resolve_import(&mut self, import: Import<'a>) -> usize {
705696
debug!(
706697
"(resolving import for module) resolving import `{}::...` in `{}`",
707698
Segment::names_to_string(&import.module_path),
@@ -781,7 +772,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
781772
}
782773
let key = BindingKey::new(target, ns);
783774
this.update_resolution(parent, key, |_, resolution| {
784-
resolution.single_imports.remove(&Interned::new_unchecked(import));
775+
resolution.single_imports.remove(&import);
785776
});
786777
}
787778
}
@@ -795,7 +786,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
795786
///
796787
/// Optionally returns an unresolved import error. This error is buffered and used to
797788
/// consolidate multiple unresolved import errors into a single diagnostic.
798-
fn finalize_import(&mut self, import: &'a Import<'a>) -> Option<UnresolvedImportError> {
789+
fn finalize_import(&mut self, import: Import<'a>) -> Option<UnresolvedImportError> {
799790
let orig_vis = import.vis.take();
800791
let ignore_binding = match &import.kind {
801792
ImportKind::Single { target_bindings, .. } => target_bindings[TypeNS].get(),
@@ -1239,7 +1230,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12391230
fn check_for_redundant_imports(
12401231
&mut self,
12411232
ident: Ident,
1242-
import: &'a Import<'a>,
1233+
import: Import<'a>,
12431234
source_bindings: &PerNS<Cell<Result<NameBinding<'a>, Determinacy>>>,
12441235
target_bindings: &PerNS<Cell<Option<NameBinding<'a>>>>,
12451236
target: Ident,
@@ -1302,7 +1293,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13021293
}
13031294
}
13041295

1305-
fn resolve_glob_import(&mut self, import: &'a Import<'a>) {
1296+
fn resolve_glob_import(&mut self, import: Import<'a>) {
13061297
// This function is only called for glob imports.
13071298
let ImportKind::Glob { id, is_prelude, .. } = import.kind else { unreachable!() };
13081299

0 commit comments

Comments
 (0)