diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-02-04 14:26:29 +1100 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-02-15 15:50:29 +1100 |
| commit | 0c2ebbd412362ce3e9f1cce099f72eabf92d6217 (patch) | |
| tree | d6afc689439a29266be7bc3d39411580a0432afd /compiler/rustc_resolve | |
| parent | 028e57ba1dcb95481dee4744a101e75a13cc6482 (diff) | |
| download | rust-0c2ebbd412362ce3e9f1cce099f72eabf92d6217.tar.gz rust-0c2ebbd412362ce3e9f1cce099f72eabf92d6217.zip | |
Rename `PtrKey` as `Interned` and improve it.
In particular, there's now more protection against incorrect usage, because you can only create one via `Interned::new_unchecked`, which makes it more obvious that you must be careful. There are also some tests.
Diffstat (limited to 'compiler/rustc_resolve')
| -rw-r--r-- | compiler/rustc_resolve/src/imports.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/lib.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/macros.rs | 4 |
3 files changed, 14 insertions, 12 deletions
diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index e7f76a18ad3..a8c2a5e1424 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -11,7 +11,7 @@ use crate::{NameBinding, NameBindingKind, PathResult, PrivacyError, ToNameBindin use rustc_ast::NodeId; use rustc_data_structures::fx::FxHashSet; -use rustc_data_structures::ptr_key::PtrKey; +use rustc_data_structures::intern::Interned; use rustc_errors::{pluralize, struct_span_err, Applicability}; use rustc_hir::def::{self, PartialRes}; use rustc_hir::def_id::DefId; @@ -134,7 +134,7 @@ impl<'a> Import<'a> { pub struct NameResolution<'a> { /// Single imports that may define the name in the namespace. /// Imports are arena-allocated, so it's ok to use pointers as keys. - single_imports: FxHashSet<PtrKey<'a, Import<'a>>>, + single_imports: FxHashSet<Interned<'a, Import<'a>>>, /// The least shadowable known binding for this name, or None if there are no known bindings. pub binding: Option<&'a NameBinding<'a>>, shadowed_glob: Option<&'a NameBinding<'a>>, @@ -153,7 +153,7 @@ impl<'a> NameResolution<'a> { } crate fn add_single_import(&mut self, import: &'a Import<'a>) { - self.single_imports.insert(PtrKey(import)); + self.single_imports.insert(Interned::new_unchecked(import)); } } @@ -850,7 +850,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { Err(Determined) => { let key = this.new_key(target, ns); this.update_resolution(parent, key, |_, resolution| { - resolution.single_imports.remove(&PtrKey(import)); + resolution.single_imports.remove(&Interned::new_unchecked(import)); }); } Ok(binding) if !binding.is_importable() => { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index dbda59e8884..28d8d9247ac 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -38,7 +38,7 @@ use rustc_ast::{ItemKind, ModKind, Path}; use rustc_ast_lowering::ResolverAstLowering; use rustc_ast_pretty::pprust; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap}; -use rustc_data_structures::ptr_key::PtrKey; +use rustc_data_structures::intern::Interned; use rustc_data_structures::sync::Lrc; use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder}; use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind}; @@ -964,7 +964,7 @@ pub struct Resolver<'a> { /// language items. empty_module: Module<'a>, module_map: FxHashMap<DefId, Module<'a>>, - binding_parent_modules: FxHashMap<PtrKey<'a, NameBinding<'a>>, Module<'a>>, + binding_parent_modules: FxHashMap<Interned<'a, NameBinding<'a>>, Module<'a>>, underscore_disambiguator: u32, /// Maps glob imports to the names of items actually imported. @@ -1115,7 +1115,7 @@ impl<'a> ResolverArenas<'a> { self.name_resolutions.alloc(Default::default()) } fn alloc_macro_rules_scope(&'a self, scope: MacroRulesScope<'a>) -> MacroRulesScopeRef<'a> { - PtrKey(self.dropless.alloc(Cell::new(scope))) + Interned::new_unchecked(self.dropless.alloc(Cell::new(scope))) } fn alloc_macro_rules_binding( &'a self, @@ -2938,7 +2938,9 @@ impl<'a> Resolver<'a> { } fn set_binding_parent_module(&mut self, binding: &'a NameBinding<'a>, module: Module<'a>) { - if let Some(old_module) = self.binding_parent_modules.insert(PtrKey(binding), module) { + if let Some(old_module) = + self.binding_parent_modules.insert(Interned::new_unchecked(binding), module) + { if !ptr::eq(module, old_module) { span_bug!(binding.span, "parent module is reset for binding"); } @@ -2954,8 +2956,8 @@ impl<'a> Resolver<'a> { // is disambiguated to mitigate regressions from macro modularization. // Scoping for `macro_rules` behaves like scoping for `let` at module level, in general. match ( - self.binding_parent_modules.get(&PtrKey(macro_rules)), - self.binding_parent_modules.get(&PtrKey(modularized)), + self.binding_parent_modules.get(&Interned::new_unchecked(macro_rules)), + self.binding_parent_modules.get(&Interned::new_unchecked(modularized)), ) { (Some(macro_rules), Some(modularized)) => { macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod() diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 82807e2d0a2..89c2a0c74bd 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -11,7 +11,7 @@ use rustc_ast_lowering::ResolverAstLowering; use rustc_ast_pretty::pprust; use rustc_attr::StabilityLevel; use rustc_data_structures::fx::FxHashSet; -use rustc_data_structures::ptr_key::PtrKey; +use rustc_data_structures::intern::Interned; use rustc_data_structures::sync::Lrc; use rustc_errors::struct_span_err; use rustc_expand::base::{Annotatable, DeriveResolutions, Indeterminate, ResolverExpand}; @@ -71,7 +71,7 @@ pub enum MacroRulesScope<'a> { /// This helps to avoid uncontrollable growth of `macro_rules!` scope chains, /// which usually grow lineraly with the number of macro invocations /// in a module (including derives) and hurt performance. -pub(crate) type MacroRulesScopeRef<'a> = PtrKey<'a, Cell<MacroRulesScope<'a>>>; +pub(crate) type MacroRulesScopeRef<'a> = Interned<'a, Cell<MacroRulesScope<'a>>>; // Macro namespace is separated into two sub-namespaces, one for bang macros and // one for attribute-like macros (attributes, derives). |
