From 115885ba7e7f88ecc076497c0ac2b0ca304b16b5 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Mon, 18 Dec 2023 21:02:21 +0100 Subject: [PATCH] Replace some instances of FxHashMap/FxHashSet with stable alternatives (mostly in rustc_hir and rustc_ast_lowering) Part of https://github.com/rust-lang/compiler-team/issues/533 --- compiler/rustc_ast_lowering/src/index.rs | 9 ++++----- compiler/rustc_ast_lowering/src/lib.rs | 20 +++++++++---------- compiler/rustc_hir/src/def.rs | 4 ++-- compiler/rustc_hir/src/definitions.rs | 4 ++-- compiler/rustc_hir/src/diagnostic_items.rs | 7 ++++--- compiler/rustc_hir/src/hir.rs | 9 ++++----- compiler/rustc_hir/src/pat_util.rs | 7 +++---- compiler/rustc_metadata/src/rmeta/decoder.rs | 4 ++-- compiler/rustc_middle/src/query/mod.rs | 4 ++-- compiler/rustc_middle/src/ty/mod.rs | 2 +- compiler/rustc_passes/src/diagnostic_items.rs | 3 --- compiler/rustc_resolve/src/lib.rs | 4 ++-- 12 files changed, 36 insertions(+), 41 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/index.rs b/compiler/rustc_ast_lowering/src/index.rs index f042f46e59c..993ddf00eb5 100644 --- a/compiler/rustc_ast_lowering/src/index.rs +++ b/compiler/rustc_ast_lowering/src/index.rs @@ -1,7 +1,6 @@ -use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::sorted_map::SortedMap; use rustc_hir as hir; -use rustc_hir::def_id::LocalDefId; +use rustc_hir::def_id::{LocalDefId, LocalDefIdMap}; use rustc_hir::intravisit::Visitor; use rustc_hir::*; use rustc_index::{Idx, IndexVec}; @@ -17,7 +16,7 @@ struct NodeCollector<'a, 'hir> { /// Outputs nodes: IndexVec>>, - parenting: FxHashMap, + parenting: LocalDefIdMap, /// The parent of this node parent_node: hir::ItemLocalId, @@ -30,7 +29,7 @@ pub(super) fn index_hir<'hir>( tcx: TyCtxt<'hir>, item: hir::OwnerNode<'hir>, bodies: &SortedMap>, -) -> (IndexVec>>, FxHashMap) { +) -> (IndexVec>>, LocalDefIdMap) { let mut nodes = IndexVec::new(); // This node's parent should never be accessed: the owner's parent is computed by the // hir_owner_parent query. Make it invalid (= ItemLocalId::MAX) to force an ICE whenever it is @@ -42,7 +41,7 @@ pub(super) fn index_hir<'hir>( parent_node: ItemLocalId::new(0), nodes, bodies, - parenting: FxHashMap::default(), + parenting: Default::default(), }; match item { diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 96ed3eee02e..e29ecf55e2f 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -44,20 +44,20 @@ use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait}; +use rustc_ast::node_id::NodeMap; use rustc_ast::ptr::P; use rustc_ast::{self as ast, *}; use rustc_ast_pretty::pprust; use rustc_data_structures::captures::Captures; use rustc_data_structures::fingerprint::Fingerprint; -use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::sorted_map::SortedMap; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::Lrc; use rustc_errors::{DiagnosticArgFromDisplay, StashKey}; use rustc_hir as hir; use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res}; -use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID, LOCAL_CRATE}; -use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName, TraitCandidate}; +use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE}; +use rustc_hir::{ConstArg, GenericArg, ItemLocalMap, ParamName, TraitCandidate}; use rustc_index::{Idx, IndexSlice, IndexVec}; use rustc_middle::span_bug; use rustc_middle::ty::{ResolverAstLowering, TyCtxt, Visibility}; @@ -119,13 +119,13 @@ struct LoweringContext<'a, 'hir> { current_hir_id_owner: hir::OwnerId, item_local_id_counter: hir::ItemLocalId, - trait_map: FxHashMap>, + trait_map: ItemLocalMap>, impl_trait_defs: Vec>, impl_trait_bounds: Vec>, /// NodeIds that are lowered inside the current HIR owner. - node_id_to_local_id: FxHashMap, + node_id_to_local_id: NodeMap, allow_try_trait: Lrc<[Symbol]>, allow_gen_future: Lrc<[Symbol]>, @@ -135,7 +135,7 @@ struct LoweringContext<'a, 'hir> { /// For each captured lifetime (e.g., 'a), we create a new lifetime parameter that is a generic /// defined on the TAIT, so we have type Foo<'a1> = ... and we establish a mapping in this /// field from the original parameter 'a to the new parameter 'a1. - generics_def_id_map: Vec>, + generics_def_id_map: Vec>, host_param_id: Option, } @@ -380,7 +380,7 @@ enum AstOwner<'a> { } fn index_crate<'a>( - node_id_to_def_id: &FxHashMap, + node_id_to_def_id: &NodeMap, krate: &'a Crate, ) -> IndexVec> { let mut indexer = Indexer { node_id_to_def_id, index: IndexVec::new() }; @@ -390,7 +390,7 @@ fn index_crate<'a>( return indexer.index; struct Indexer<'s, 'a> { - node_id_to_def_id: &'s FxHashMap, + node_id_to_def_id: &'s NodeMap, index: IndexVec>, } @@ -642,7 +642,7 @@ fn with_hir_id_owner( /// `'a` declared on the TAIT, instead of the function. fn with_remapping( &mut self, - remap: FxHashMap, + remap: LocalDefIdMap, f: impl FnOnce(&mut Self) -> R, ) -> R { self.generics_def_id_map.push(remap); @@ -1657,7 +1657,7 @@ fn lower_opaque_inner( // Map from captured (old) lifetime to synthetic (new) lifetime. // Used to resolve lifetimes in the bounds of the opaque. - let mut captured_to_synthesized_mapping = FxHashMap::default(); + let mut captured_to_synthesized_mapping = LocalDefIdMap::default(); // List of (early-bound) synthetic lifetimes that are owned by the opaque. // This is used to create the `hir::Generics` owned by the opaque. let mut synthesized_lifetime_definitions = vec![]; diff --git a/compiler/rustc_hir/src/def.rs b/compiler/rustc_hir/src/def.rs index 258d6710bc5..e2bccf1ffa5 100644 --- a/compiler/rustc_hir/src/def.rs +++ b/compiler/rustc_hir/src/def.rs @@ -3,8 +3,8 @@ use rustc_ast as ast; use rustc_ast::NodeId; -use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::ToStableHashKey; +use rustc_data_structures::unord::UnordMap; use rustc_macros::HashStable_Generic; use rustc_span::def_id::{DefId, LocalDefId}; use rustc_span::hygiene::MacroKind; @@ -806,4 +806,4 @@ pub enum LifetimeRes { ElidedAnchor { start: NodeId, end: NodeId }, } -pub type DocLinkResMap = FxHashMap<(Symbol, Namespace), Option>>; +pub type DocLinkResMap = UnordMap<(Symbol, Namespace), Option>>; diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index d222325475d..2ab9a6ef32c 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -8,8 +8,8 @@ use crate::def_id::{CrateNum, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE}; use crate::def_path_hash_map::DefPathHashMap; -use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::{Hash64, StableHasher}; +use rustc_data_structures::unord::UnordMap; use rustc_index::IndexVec; use rustc_span::symbol::{kw, sym, Symbol}; @@ -95,7 +95,7 @@ pub fn enumerated_keys_and_path_hashes( #[derive(Debug)] pub struct Definitions { table: DefPathTable, - next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>, + next_disambiguator: UnordMap<(LocalDefId, DefPathData), u32>, /// The [StableCrateId] of the local crate. stable_crate_id: StableCrateId, diff --git a/compiler/rustc_hir/src/diagnostic_items.rs b/compiler/rustc_hir/src/diagnostic_items.rs index 243014b0027..d4d09f9a4e0 100644 --- a/compiler/rustc_hir/src/diagnostic_items.rs +++ b/compiler/rustc_hir/src/diagnostic_items.rs @@ -1,12 +1,13 @@ use crate::def_id::DefId; -use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; +use rustc_span::def_id::DefIdMap; use rustc_span::Symbol; #[derive(Debug, Default)] pub struct DiagnosticItems { - pub id_to_name: FxHashMap, - pub name_to_id: FxHashMap, + pub id_to_name: DefIdMap, + pub name_to_id: FxIndexMap, } impl HashStable for DiagnosticItems { diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 760945554f0..d148137091c 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1,6 +1,6 @@ use crate::def::{CtorKind, DefKind, Res}; -use crate::def_id::DefId; -pub(crate) use crate::hir_id::{HirId, ItemLocalId, OwnerId}; +use crate::def_id::{DefId, LocalDefIdMap}; +pub(crate) use crate::hir_id::{HirId, ItemLocalId, ItemLocalMap, OwnerId}; use crate::intravisit::FnKind; use crate::LangItem; @@ -11,7 +11,6 @@ pub use rustc_ast::{ImplPolarity, IsAuto, Movability, Mutability, UnOp}; use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; use rustc_data_structures::fingerprint::Fingerprint; -use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::sorted_map::SortedMap; use rustc_index::IndexVec; use rustc_macros::HashStable_Generic; @@ -874,12 +873,12 @@ pub struct OwnerInfo<'hir> { /// Contents of the HIR. pub nodes: OwnerNodes<'hir>, /// Map from each nested owner to its parent's local id. - pub parenting: FxHashMap, + pub parenting: LocalDefIdMap, /// Collected attributes of the HIR nodes. pub attrs: AttributeMap<'hir>, /// Map indicating what traits are in scope for places where this /// is relevant; generated by resolve. - pub trait_map: FxHashMap>, + pub trait_map: ItemLocalMap>, } impl<'tcx> OwnerInfo<'tcx> { diff --git a/compiler/rustc_hir/src/pat_util.rs b/compiler/rustc_hir/src/pat_util.rs index 838c123f83c..e6050327186 100644 --- a/compiler/rustc_hir/src/pat_util.rs +++ b/compiler/rustc_hir/src/pat_util.rs @@ -1,7 +1,6 @@ use crate::def::{CtorOf, DefKind, Res}; -use crate::def_id::DefId; +use crate::def_id::{DefId, DefIdSet}; use crate::hir::{self, BindingAnnotation, ByRef, HirId, PatKind}; -use rustc_data_structures::fx::FxHashSet; use rustc_span::symbol::Ident; use rustc_span::Span; @@ -114,9 +113,9 @@ pub fn necessary_variants(&self) -> Vec { } _ => true, }); - // We remove duplicates by inserting into a `FxHashSet` to avoid re-ordering + // We remove duplicates by inserting into a hash set to avoid re-ordering // the bounds - let mut duplicates = FxHashSet::default(); + let mut duplicates = DefIdSet::default(); variants.retain(|def_id| duplicates.insert(*def_id)); variants } diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 24ab4f94d5c..281a0eafee1 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -12,7 +12,7 @@ use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, DeriveProcMacro}; use rustc_hir::def::Res; -use rustc_hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::definitions::{DefPath, DefPathData}; use rustc_hir::diagnostic_items::DiagnosticItems; use rustc_index::Idx; @@ -1200,7 +1200,7 @@ fn get_stripped_cfg_items(self, cnum: CrateNum, tcx: TyCtxt<'tcx>) -> &'tcx [Str /// Iterates over the diagnostic items in the given crate. fn get_diagnostic_items(self) -> DiagnosticItems { - let mut id_to_name = FxHashMap::default(); + let mut id_to_name = DefIdMap::default(); let name_to_id = self .root .diagnostic_items diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index a69bff6ed8c..3a54f5f6b3d 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -69,7 +69,7 @@ CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LocalDefIdMap, LocalDefIdSet, LocalModDefId, }; use rustc_hir::lang_items::{LangItem, LanguageItems}; -use rustc_hir::{Crate, ItemLocalId, TraitCandidate}; +use rustc_hir::{Crate, ItemLocalId, ItemLocalMap, TraitCandidate}; use rustc_index::IndexVec; use rustc_query_system::ich::StableHashingContext; use rustc_query_system::query::{try_get_cached, CacheSelector, QueryCache, QueryMode, QueryState}; @@ -1490,7 +1490,7 @@ desc { "computing whether impls specialize one another" } } query in_scope_traits_map(_: hir::OwnerId) - -> Option<&'tcx FxHashMap>> { + -> Option<&'tcx ItemLocalMap>> { desc { "getting traits in scope at a block" } } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 96de9c447b6..35c135830c3 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -192,7 +192,7 @@ pub struct ResolverAstLowering { pub next_node_id: ast::NodeId, - pub node_id_to_def_id: FxHashMap, + pub node_id_to_def_id: NodeMap, pub def_id_to_node_id: IndexVec, pub trait_map: NodeMap>, diff --git a/compiler/rustc_passes/src/diagnostic_items.rs b/compiler/rustc_passes/src/diagnostic_items.rs index 5f767c9acaa..d8b9f4fae87 100644 --- a/compiler/rustc_passes/src/diagnostic_items.rs +++ b/compiler/rustc_passes/src/diagnostic_items.rs @@ -83,9 +83,6 @@ fn all_diagnostic_items(tcx: TyCtxt<'_>, (): ()) -> DiagnosticItems { // Collect diagnostic items in other crates. for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) { - // We are collecting many DiagnosticItems hash maps into one - // DiagnosticItems hash map. The iteration order does not matter. - #[allow(rustc::potential_query_instability)] for (&name, &def_id) in &tcx.diagnostic_items(cnum).name_to_id { collect_item(tcx, &mut items, name, def_id); } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index ad637472b47..670fdcfb0d2 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1084,7 +1084,7 @@ pub struct Resolver<'a, 'tcx> { next_node_id: NodeId, - node_id_to_def_id: FxHashMap, + node_id_to_def_id: NodeMap, def_id_to_node_id: IndexVec, /// Indices of unnamed struct or variant fields with unresolved attributes. @@ -1296,7 +1296,7 @@ pub fn new( let mut def_id_to_node_id = IndexVec::default(); assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), CRATE_DEF_ID); - let mut node_id_to_def_id = FxHashMap::default(); + let mut node_id_to_def_id = NodeMap::default(); node_id_to_def_id.insert(CRATE_NODE_ID, CRATE_DEF_ID); let mut invocation_parents = FxHashMap::default();