Skip to content

Commit db132c5

Browse files
Replace a number of FxHashMaps/Sets with stable-iteration-order alternatives.
1 parent 739e5ef commit db132c5

File tree

9 files changed

+40
-33
lines changed

9 files changed

+40
-33
lines changed

compiler/rustc_hir_analysis/src/astconv/errors.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::errors::{
66
use crate::fluent_generated as fluent;
77
use crate::traits::error_reporting::report_object_safety_error;
88
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
9+
use rustc_data_structures::unord::UnordMap;
910
use rustc_errors::{pluralize, struct_span_err, Applicability, Diagnostic, ErrorGuaranteed};
1011
use rustc_hir as hir;
1112
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -673,7 +674,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
673674
}))
674675
})
675676
.flatten()
676-
.collect::<FxHashMap<Symbol, &ty::AssocItem>>();
677+
.collect::<UnordMap<Symbol, &ty::AssocItem>>();
677678

678679
let mut names = names
679680
.into_iter()
@@ -709,7 +710,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
709710
let mut where_constraints = vec![];
710711
let mut already_has_generics_args_suggestion = false;
711712
for (span, assoc_items) in &associated_types {
712-
let mut names: FxHashMap<_, usize> = FxHashMap::default();
713+
let mut names: UnordMap<_, usize> = Default::default();
713714
for item in assoc_items {
714715
types_count += 1;
715716
*names.entry(item.name).or_insert(0) += 1;

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
use rustc_data_structures::captures::Captures;
1818
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
19+
use rustc_data_structures::unord::UnordMap;
1920
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, StashKey};
2021
use rustc_hir as hir;
2122
use rustc_hir::def::DefKind;
@@ -979,7 +980,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
979980
})
980981
// Check for duplicates
981982
.and_then(|list| {
982-
let mut set: FxHashMap<Symbol, Span> = FxHashMap::default();
983+
let mut set: UnordMap<Symbol, Span> = Default::default();
983984
let mut no_dups = true;
984985

985986
for ident in &*list {

compiler/rustc_lint/src/foreign_modules.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
21
use rustc_data_structures::stack::ensure_sufficient_stack;
2+
use rustc_data_structures::unord::{UnordMap, UnordSet};
33
use rustc_hir as hir;
44
use rustc_hir::def::DefKind;
55
use rustc_middle::query::Providers;
@@ -72,7 +72,7 @@ struct ClashingExternDeclarations {
7272
/// the symbol should be reported as a clashing declaration.
7373
// FIXME: Technically, we could just store a &'tcx str here without issue; however, the
7474
// `impl_lint_pass` macro doesn't currently support lints parametric over a lifetime.
75-
seen_decls: FxHashMap<Symbol, hir::OwnerId>,
75+
seen_decls: UnordMap<Symbol, hir::OwnerId>,
7676
}
7777

7878
/// Differentiate between whether the name for an extern decl came from the link_name attribute or
@@ -96,7 +96,7 @@ impl SymbolName {
9696

9797
impl ClashingExternDeclarations {
9898
pub(crate) fn new() -> Self {
99-
ClashingExternDeclarations { seen_decls: FxHashMap::default() }
99+
ClashingExternDeclarations { seen_decls: Default::default() }
100100
}
101101

102102
/// Insert a new foreign item into the seen set. If a symbol with the same name already exists
@@ -209,12 +209,12 @@ fn structurally_same_type<'tcx>(
209209
b: Ty<'tcx>,
210210
ckind: types::CItemKind,
211211
) -> bool {
212-
let mut seen_types = FxHashSet::default();
212+
let mut seen_types = UnordSet::default();
213213
structurally_same_type_impl(&mut seen_types, tcx, param_env, a, b, ckind)
214214
}
215215

216216
fn structurally_same_type_impl<'tcx>(
217-
seen_types: &mut FxHashSet<(Ty<'tcx>, Ty<'tcx>)>,
217+
seen_types: &mut UnordSet<(Ty<'tcx>, Ty<'tcx>)>,
218218
tcx: TyCtxt<'tcx>,
219219
param_env: ty::ParamEnv<'tcx>,
220220
a: Ty<'tcx>,

compiler/rustc_lint/src/non_ascii_idents.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::lints::{
44
};
55
use crate::{EarlyContext, EarlyLintPass, LintContext};
66
use rustc_ast as ast;
7-
use rustc_data_structures::fx::FxHashMap;
7+
use rustc_data_structures::fx::FxIndexMap;
8+
use rustc_data_structures::unord::UnordMap;
89
use rustc_span::symbol::Symbol;
910

1011
declare_lint! {
@@ -194,8 +195,8 @@ impl EarlyLintPass for NonAsciiIdents {
194195
}
195196

196197
if has_non_ascii_idents && check_confusable_idents {
197-
let mut skeleton_map: FxHashMap<Symbol, (Symbol, Span, bool)> =
198-
FxHashMap::with_capacity_and_hasher(symbols.len(), Default::default());
198+
let mut skeleton_map: UnordMap<Symbol, (Symbol, Span, bool)> =
199+
UnordMap::with_capacity(symbols.len());
199200
let mut skeleton_buf = String::new();
200201

201202
for (&symbol, &sp) in symbols.iter() {
@@ -248,8 +249,8 @@ impl EarlyLintPass for NonAsciiIdents {
248249
Verified,
249250
}
250251

251-
let mut script_states: FxHashMap<AugmentedScriptSet, ScriptSetUsage> =
252-
FxHashMap::default();
252+
let mut script_states: FxIndexMap<AugmentedScriptSet, ScriptSetUsage> =
253+
Default::default();
253254
let latin_augmented_script_set = AugmentedScriptSet::for_char('A');
254255
script_states.insert(latin_augmented_script_set, ScriptSetUsage::Verified);
255256

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,14 +1914,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19141914
empty_proc_macro!(self);
19151915
let tcx = self.tcx;
19161916
let lib_features = tcx.lib_features(LOCAL_CRATE);
1917-
self.lazy_array(lib_features.to_vec())
1917+
self.lazy_array(lib_features.to_sorted_vec())
19181918
}
19191919

19201920
fn encode_stability_implications(&mut self) -> LazyArray<(Symbol, Symbol)> {
19211921
empty_proc_macro!(self);
19221922
let tcx = self.tcx;
19231923
let implications = tcx.stability_implications(LOCAL_CRATE);
1924-
self.lazy_array(implications.iter().map(|(k, v)| (*k, *v)))
1924+
let sorted = implications.to_sorted_stable_ord();
1925+
self.lazy_array(sorted.into_iter().map(|(k, v)| (*k, *v)))
19251926
}
19261927

19271928
fn encode_diagnostic_items(&mut self) -> LazyArray<(Symbol, DefIndex)> {

compiler/rustc_middle/src/middle/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod dependency_format;
44
pub mod exported_symbols;
55
pub mod lang_items;
66
pub mod lib_features {
7-
use rustc_data_structures::fx::FxHashMap;
7+
use rustc_data_structures::unord::UnordMap;
88
use rustc_span::{symbol::Symbol, Span};
99

1010
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -16,15 +16,16 @@ pub mod lib_features {
1616

1717
#[derive(HashStable, Debug, Default)]
1818
pub struct LibFeatures {
19-
pub stability: FxHashMap<Symbol, (FeatureStability, Span)>,
19+
pub stability: UnordMap<Symbol, (FeatureStability, Span)>,
2020
}
2121

2222
impl LibFeatures {
23-
pub fn to_vec(&self) -> Vec<(Symbol, FeatureStability)> {
24-
let mut all_features: Vec<_> =
25-
self.stability.iter().map(|(&sym, &(stab, _))| (sym, stab)).collect();
26-
all_features.sort_unstable_by(|(a, _), (b, _)| a.as_str().cmp(b.as_str()));
27-
all_features
23+
pub fn to_sorted_vec(&self) -> Vec<(Symbol, FeatureStability)> {
24+
self.stability
25+
.to_sorted_stable_ord()
26+
.iter()
27+
.map(|(&sym, &(stab, _))| (sym, stab))
28+
.collect()
2829
}
2930
}
3031
}

compiler/rustc_middle/src/middle/stability.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_attr::{
99
self as attr, ConstStability, DefaultBodyStability, DeprecatedSince, Deprecation, Stability,
1010
};
1111
use rustc_data_structures::fx::FxHashMap;
12+
use rustc_data_structures::unord::UnordMap;
1213
use rustc_errors::{Applicability, Diagnostic};
1314
use rustc_feature::GateIssue;
1415
use rustc_hir::def::DefKind;
@@ -77,7 +78,7 @@ pub struct Index {
7778
/// to know that the feature implies another feature. If it were reversed, and the `#[stable]`
7879
/// attribute had an `implies` meta item, then a map would be necessary when avoiding a "use of
7980
/// unstable feature" error for a feature that was implied.
80-
pub implications: FxHashMap<Symbol, Symbol>,
81+
pub implications: UnordMap<Symbol, Symbol>,
8182
}
8283

8384
impl Index {

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
6161
use rustc_data_structures::steal::Steal;
6262
use rustc_data_structures::svh::Svh;
6363
use rustc_data_structures::sync::Lrc;
64-
use rustc_data_structures::unord::UnordSet;
64+
use rustc_data_structures::unord::{UnordMap, UnordSet};
6565
use rustc_errors::ErrorGuaranteed;
6666
use rustc_hir as hir;
6767
use rustc_hir::def::{DefKind, DocLinkResMap};
@@ -1739,7 +1739,7 @@ rustc_queries! {
17391739
separate_provide_extern
17401740
arena_cache
17411741
}
1742-
query stability_implications(_: CrateNum) -> &'tcx FxHashMap<Symbol, Symbol> {
1742+
query stability_implications(_: CrateNum) -> &'tcx UnordMap<Symbol, Symbol> {
17431743
arena_cache
17441744
desc { "calculating the implications between `#[unstable]` features defined in a crate" }
17451745
separate_provide_extern

compiler/rustc_passes/src/stability.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use rustc_attr::{
66
self as attr, ConstStability, DeprecatedSince, Stability, StabilityLevel, StableSince,
77
Unstable, UnstableReason, VERSION_PLACEHOLDER,
88
};
9-
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
9+
use rustc_data_structures::fx::FxIndexMap;
10+
use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet};
1011
use rustc_hir as hir;
1112
use rustc_hir::def::{DefKind, Res};
1213
use rustc_hir::def_id::{LocalDefId, LocalModDefId, CRATE_DEF_ID, LOCAL_CRATE};
@@ -923,7 +924,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
923924
}
924925

925926
let declared_lang_features = &tcx.features().declared_lang_features;
926-
let mut lang_features = FxHashSet::default();
927+
let mut lang_features = UnordSet::default();
927928
for &(feature, span, since) in declared_lang_features {
928929
if let Some(since) = since {
929930
// Warn if the user has enabled an already-stable lang feature.
@@ -980,11 +981,11 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
980981
fn check_features<'tcx>(
981982
tcx: TyCtxt<'tcx>,
982983
remaining_lib_features: &mut FxIndexMap<&Symbol, Span>,
983-
remaining_implications: &mut FxHashMap<Symbol, Symbol>,
984+
remaining_implications: &mut UnordMap<Symbol, Symbol>,
984985
defined_features: &LibFeatures,
985-
all_implications: &FxHashMap<Symbol, Symbol>,
986+
all_implications: &UnordMap<Symbol, Symbol>,
986987
) {
987-
for (feature, since) in defined_features.to_vec() {
988+
for (feature, since) in defined_features.to_sorted_vec() {
988989
if let FeatureStability::AcceptedSince(since) = since
989990
&& let Some(span) = remaining_lib_features.get(&feature)
990991
{
@@ -1021,7 +1022,8 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
10211022
// `remaining_lib_features`.
10221023
let mut all_implications = remaining_implications.clone();
10231024
for &cnum in tcx.crates(()) {
1024-
all_implications.extend(tcx.stability_implications(cnum));
1025+
all_implications
1026+
.extend_unord(tcx.stability_implications(cnum).items().map(|(k, v)| (*k, *v)));
10251027
}
10261028

10271029
check_features(
@@ -1052,8 +1054,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
10521054

10531055
// We only use the hash map contents to emit errors, and the order of
10541056
// emitted errors do not affect query stability.
1055-
#[allow(rustc::potential_query_instability)]
1056-
for (implied_by, feature) in remaining_implications {
1057+
for (&implied_by, &feature) in remaining_implications.to_sorted_stable_ord() {
10571058
let local_defined_features = tcx.lib_features(LOCAL_CRATE);
10581059
let span = local_defined_features
10591060
.stability

0 commit comments

Comments
 (0)