Skip to content

Commit 32f5db9

Browse files
committed
Auto merge of rust-lang#119093 - michaelwoerister:mcp533-18, r=petrochenkov
Replace some instances of FxHashMap/FxHashSet with stable alternatives (mostly in rustc_hir and rustc_ast_lowering) Part of rust-lang/compiler-team#533. We should be getting close to being able to remove the HashStable impl of HashMap.
2 parents be69926 + 115885b commit 32f5db9

File tree

12 files changed

+36
-41
lines changed

12 files changed

+36
-41
lines changed

compiler/rustc_ast_lowering/src/index.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use rustc_data_structures::fx::FxHashMap;
21
use rustc_data_structures::sorted_map::SortedMap;
32
use rustc_hir as hir;
4-
use rustc_hir::def_id::LocalDefId;
3+
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap};
54
use rustc_hir::intravisit::Visitor;
65
use rustc_hir::*;
76
use rustc_index::{Idx, IndexVec};
@@ -17,7 +16,7 @@ struct NodeCollector<'a, 'hir> {
1716

1817
/// Outputs
1918
nodes: IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>,
20-
parenting: FxHashMap<LocalDefId, ItemLocalId>,
19+
parenting: LocalDefIdMap<ItemLocalId>,
2120

2221
/// The parent of this node
2322
parent_node: hir::ItemLocalId,
@@ -30,7 +29,7 @@ pub(super) fn index_hir<'hir>(
3029
tcx: TyCtxt<'hir>,
3130
item: hir::OwnerNode<'hir>,
3231
bodies: &SortedMap<ItemLocalId, &'hir Body<'hir>>,
33-
) -> (IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>, FxHashMap<LocalDefId, ItemLocalId>) {
32+
) -> (IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>, LocalDefIdMap<ItemLocalId>) {
3433
let mut nodes = IndexVec::new();
3534
// This node's parent should never be accessed: the owner's parent is computed by the
3635
// 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>(
4241
parent_node: ItemLocalId::new(0),
4342
nodes,
4443
bodies,
45-
parenting: FxHashMap::default(),
44+
parenting: Default::default(),
4645
};
4746

4847
match item {

compiler/rustc_ast_lowering/src/lib.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ extern crate tracing;
4444

4545
use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait};
4646

47+
use rustc_ast::node_id::NodeMap;
4748
use rustc_ast::ptr::P;
4849
use rustc_ast::{self as ast, *};
4950
use rustc_ast_pretty::pprust;
5051
use rustc_data_structures::captures::Captures;
5152
use rustc_data_structures::fingerprint::Fingerprint;
52-
use rustc_data_structures::fx::FxHashMap;
5353
use rustc_data_structures::sorted_map::SortedMap;
5454
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5555
use rustc_data_structures::sync::Lrc;
5656
use rustc_errors::{DiagnosticArgFromDisplay, StashKey};
5757
use rustc_hir as hir;
5858
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
59-
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
60-
use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName, TraitCandidate};
59+
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
60+
use rustc_hir::{ConstArg, GenericArg, ItemLocalMap, ParamName, TraitCandidate};
6161
use rustc_index::{Idx, IndexSlice, IndexVec};
6262
use rustc_middle::span_bug;
6363
use rustc_middle::ty::{ResolverAstLowering, TyCtxt, Visibility};
@@ -119,13 +119,13 @@ struct LoweringContext<'a, 'hir> {
119119

120120
current_hir_id_owner: hir::OwnerId,
121121
item_local_id_counter: hir::ItemLocalId,
122-
trait_map: FxHashMap<ItemLocalId, Box<[TraitCandidate]>>,
122+
trait_map: ItemLocalMap<Box<[TraitCandidate]>>,
123123

124124
impl_trait_defs: Vec<hir::GenericParam<'hir>>,
125125
impl_trait_bounds: Vec<hir::WherePredicate<'hir>>,
126126

127127
/// NodeIds that are lowered inside the current HIR owner.
128-
node_id_to_local_id: FxHashMap<NodeId, hir::ItemLocalId>,
128+
node_id_to_local_id: NodeMap<hir::ItemLocalId>,
129129

130130
allow_try_trait: Lrc<[Symbol]>,
131131
allow_gen_future: Lrc<[Symbol]>,
@@ -135,7 +135,7 @@ struct LoweringContext<'a, 'hir> {
135135
/// For each captured lifetime (e.g., 'a), we create a new lifetime parameter that is a generic
136136
/// defined on the TAIT, so we have type Foo<'a1> = ... and we establish a mapping in this
137137
/// field from the original parameter 'a to the new parameter 'a1.
138-
generics_def_id_map: Vec<FxHashMap<LocalDefId, LocalDefId>>,
138+
generics_def_id_map: Vec<LocalDefIdMap<LocalDefId>>,
139139

140140
host_param_id: Option<LocalDefId>,
141141
}
@@ -380,7 +380,7 @@ enum AstOwner<'a> {
380380
}
381381

382382
fn index_crate<'a>(
383-
node_id_to_def_id: &FxHashMap<NodeId, LocalDefId>,
383+
node_id_to_def_id: &NodeMap<LocalDefId>,
384384
krate: &'a Crate,
385385
) -> IndexVec<LocalDefId, AstOwner<'a>> {
386386
let mut indexer = Indexer { node_id_to_def_id, index: IndexVec::new() };
@@ -390,7 +390,7 @@ fn index_crate<'a>(
390390
return indexer.index;
391391

392392
struct Indexer<'s, 'a> {
393-
node_id_to_def_id: &'s FxHashMap<NodeId, LocalDefId>,
393+
node_id_to_def_id: &'s NodeMap<LocalDefId>,
394394
index: IndexVec<LocalDefId, AstOwner<'a>>,
395395
}
396396

@@ -642,7 +642,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
642642
/// `'a` declared on the TAIT, instead of the function.
643643
fn with_remapping<R>(
644644
&mut self,
645-
remap: FxHashMap<LocalDefId, LocalDefId>,
645+
remap: LocalDefIdMap<LocalDefId>,
646646
f: impl FnOnce(&mut Self) -> R,
647647
) -> R {
648648
self.generics_def_id_map.push(remap);
@@ -1657,7 +1657,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16571657

16581658
// Map from captured (old) lifetime to synthetic (new) lifetime.
16591659
// Used to resolve lifetimes in the bounds of the opaque.
1660-
let mut captured_to_synthesized_mapping = FxHashMap::default();
1660+
let mut captured_to_synthesized_mapping = LocalDefIdMap::default();
16611661
// List of (early-bound) synthetic lifetimes that are owned by the opaque.
16621662
// This is used to create the `hir::Generics` owned by the opaque.
16631663
let mut synthesized_lifetime_definitions = vec![];

compiler/rustc_hir/src/def.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::hir;
33

44
use rustc_ast as ast;
55
use rustc_ast::NodeId;
6-
use rustc_data_structures::fx::FxHashMap;
76
use rustc_data_structures::stable_hasher::ToStableHashKey;
7+
use rustc_data_structures::unord::UnordMap;
88
use rustc_macros::HashStable_Generic;
99
use rustc_span::def_id::{DefId, LocalDefId};
1010
use rustc_span::hygiene::MacroKind;
@@ -806,4 +806,4 @@ pub enum LifetimeRes {
806806
ElidedAnchor { start: NodeId, end: NodeId },
807807
}
808808

809-
pub type DocLinkResMap = FxHashMap<(Symbol, Namespace), Option<Res<NodeId>>>;
809+
pub type DocLinkResMap = UnordMap<(Symbol, Namespace), Option<Res<NodeId>>>;

compiler/rustc_hir/src/definitions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ pub use crate::def_id::DefPathHash;
88
use crate::def_id::{CrateNum, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE};
99
use crate::def_path_hash_map::DefPathHashMap;
1010

11-
use rustc_data_structures::fx::FxHashMap;
1211
use rustc_data_structures::stable_hasher::{Hash64, StableHasher};
12+
use rustc_data_structures::unord::UnordMap;
1313
use rustc_index::IndexVec;
1414
use rustc_span::symbol::{kw, sym, Symbol};
1515

@@ -95,7 +95,7 @@ impl DefPathTable {
9595
#[derive(Debug)]
9696
pub struct Definitions {
9797
table: DefPathTable,
98-
next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>,
98+
next_disambiguator: UnordMap<(LocalDefId, DefPathData), u32>,
9999

100100
/// The [StableCrateId] of the local crate.
101101
stable_crate_id: StableCrateId,

compiler/rustc_hir/src/diagnostic_items.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use crate::def_id::DefId;
2-
use rustc_data_structures::fx::FxHashMap;
2+
use rustc_data_structures::fx::FxIndexMap;
33
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4+
use rustc_span::def_id::DefIdMap;
45
use rustc_span::Symbol;
56

67
#[derive(Debug, Default)]
78
pub struct DiagnosticItems {
8-
pub id_to_name: FxHashMap<DefId, Symbol>,
9-
pub name_to_id: FxHashMap<Symbol, DefId>,
9+
pub id_to_name: DefIdMap<Symbol>,
10+
pub name_to_id: FxIndexMap<Symbol, DefId>,
1011
}
1112

1213
impl<CTX: crate::HashStableContext> HashStable<CTX> for DiagnosticItems {

compiler/rustc_hir/src/hir.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::def::{CtorKind, DefKind, Res};
2-
use crate::def_id::DefId;
3-
pub(crate) use crate::hir_id::{HirId, ItemLocalId, OwnerId};
2+
use crate::def_id::{DefId, LocalDefIdMap};
3+
pub(crate) use crate::hir_id::{HirId, ItemLocalId, ItemLocalMap, OwnerId};
44
use crate::intravisit::FnKind;
55
use crate::LangItem;
66

@@ -11,7 +11,6 @@ pub use rustc_ast::{BinOp, BinOpKind, BindingAnnotation, BorrowKind, ByRef, Capt
1111
pub use rustc_ast::{ImplPolarity, IsAuto, Movability, Mutability, UnOp};
1212
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
1313
use rustc_data_structures::fingerprint::Fingerprint;
14-
use rustc_data_structures::fx::FxHashMap;
1514
use rustc_data_structures::sorted_map::SortedMap;
1615
use rustc_index::IndexVec;
1716
use rustc_macros::HashStable_Generic;
@@ -874,12 +873,12 @@ pub struct OwnerInfo<'hir> {
874873
/// Contents of the HIR.
875874
pub nodes: OwnerNodes<'hir>,
876875
/// Map from each nested owner to its parent's local id.
877-
pub parenting: FxHashMap<LocalDefId, ItemLocalId>,
876+
pub parenting: LocalDefIdMap<ItemLocalId>,
878877
/// Collected attributes of the HIR nodes.
879878
pub attrs: AttributeMap<'hir>,
880879
/// Map indicating what traits are in scope for places where this
881880
/// is relevant; generated by resolve.
882-
pub trait_map: FxHashMap<ItemLocalId, Box<[TraitCandidate]>>,
881+
pub trait_map: ItemLocalMap<Box<[TraitCandidate]>>,
883882
}
884883

885884
impl<'tcx> OwnerInfo<'tcx> {

compiler/rustc_hir/src/pat_util.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::def::{CtorOf, DefKind, Res};
2-
use crate::def_id::DefId;
2+
use crate::def_id::{DefId, DefIdSet};
33
use crate::hir::{self, BindingAnnotation, ByRef, HirId, PatKind};
4-
use rustc_data_structures::fx::FxHashSet;
54
use rustc_span::symbol::Ident;
65
use rustc_span::Span;
76

@@ -114,9 +113,9 @@ impl hir::Pat<'_> {
114113
}
115114
_ => true,
116115
});
117-
// We remove duplicates by inserting into a `FxHashSet` to avoid re-ordering
116+
// We remove duplicates by inserting into a hash set to avoid re-ordering
118117
// the bounds
119-
let mut duplicates = FxHashSet::default();
118+
let mut duplicates = DefIdSet::default();
120119
variants.retain(|def_id| duplicates.insert(*def_id));
121120
variants
122121
}

compiler/rustc_metadata/src/rmeta/decoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_data_structures::unhash::UnhashMap;
1212
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
1313
use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, DeriveProcMacro};
1414
use rustc_hir::def::Res;
15-
use rustc_hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE};
15+
use rustc_hir::def_id::{DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
1616
use rustc_hir::definitions::{DefPath, DefPathData};
1717
use rustc_hir::diagnostic_items::DiagnosticItems;
1818
use rustc_index::Idx;
@@ -1200,7 +1200,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12001200

12011201
/// Iterates over the diagnostic items in the given crate.
12021202
fn get_diagnostic_items(self) -> DiagnosticItems {
1203-
let mut id_to_name = FxHashMap::default();
1203+
let mut id_to_name = DefIdMap::default();
12041204
let name_to_id = self
12051205
.root
12061206
.diagnostic_items

compiler/rustc_middle/src/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use rustc_hir::def_id::{
6969
CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LocalDefIdMap, LocalDefIdSet, LocalModDefId,
7070
};
7171
use rustc_hir::lang_items::{LangItem, LanguageItems};
72-
use rustc_hir::{Crate, ItemLocalId, TraitCandidate};
72+
use rustc_hir::{Crate, ItemLocalId, ItemLocalMap, TraitCandidate};
7373
use rustc_index::IndexVec;
7474
use rustc_query_system::ich::StableHashingContext;
7575
use rustc_query_system::query::{try_get_cached, CacheSelector, QueryCache, QueryMode, QueryState};
@@ -1490,7 +1490,7 @@ rustc_queries! {
14901490
desc { "computing whether impls specialize one another" }
14911491
}
14921492
query in_scope_traits_map(_: hir::OwnerId)
1493-
-> Option<&'tcx FxHashMap<ItemLocalId, Box<[TraitCandidate]>>> {
1493+
-> Option<&'tcx ItemLocalMap<Box<[TraitCandidate]>>> {
14941494
desc { "getting traits in scope at a block" }
14951495
}
14961496

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub struct ResolverAstLowering {
192192

193193
pub next_node_id: ast::NodeId,
194194

195-
pub node_id_to_def_id: FxHashMap<ast::NodeId, LocalDefId>,
195+
pub node_id_to_def_id: NodeMap<LocalDefId>,
196196
pub def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
197197

198198
pub trait_map: NodeMap<Vec<hir::TraitCandidate>>,

compiler/rustc_passes/src/diagnostic_items.rs

-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ fn all_diagnostic_items(tcx: TyCtxt<'_>, (): ()) -> DiagnosticItems {
8383

8484
// Collect diagnostic items in other crates.
8585
for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
86-
// We are collecting many DiagnosticItems hash maps into one
87-
// DiagnosticItems hash map. The iteration order does not matter.
88-
#[allow(rustc::potential_query_instability)]
8986
for (&name, &def_id) in &tcx.diagnostic_items(cnum).name_to_id {
9087
collect_item(tcx, &mut items, name, def_id);
9188
}

compiler/rustc_resolve/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ pub struct Resolver<'a, 'tcx> {
10841084

10851085
next_node_id: NodeId,
10861086

1087-
node_id_to_def_id: FxHashMap<ast::NodeId, LocalDefId>,
1087+
node_id_to_def_id: NodeMap<LocalDefId>,
10881088
def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
10891089

10901090
/// Indices of unnamed struct or variant fields with unresolved attributes.
@@ -1296,7 +1296,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12961296

12971297
let mut def_id_to_node_id = IndexVec::default();
12981298
assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), CRATE_DEF_ID);
1299-
let mut node_id_to_def_id = FxHashMap::default();
1299+
let mut node_id_to_def_id = NodeMap::default();
13001300
node_id_to_def_id.insert(CRATE_NODE_ID, CRATE_DEF_ID);
13011301

13021302
let mut invocation_parents = FxHashMap::default();

0 commit comments

Comments
 (0)