Replace a number of FxHashMaps/Sets with stable-iteration-order alternatives.

This commit is contained in:
Michael Woerister 2023-12-21 10:52:27 +01:00
parent 739e5ef49e
commit db132c575d
9 changed files with 40 additions and 33 deletions

View file

@ -6,6 +6,7 @@ use crate::errors::{
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use crate::traits::error_reporting::report_object_safety_error; use crate::traits::error_reporting::report_object_safety_error;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet}; use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::unord::UnordMap;
use rustc_errors::{pluralize, struct_span_err, Applicability, Diagnostic, ErrorGuaranteed}; use rustc_errors::{pluralize, struct_span_err, Applicability, Diagnostic, ErrorGuaranteed};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
@ -673,7 +674,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
})) }))
}) })
.flatten() .flatten()
.collect::<FxHashMap<Symbol, &ty::AssocItem>>(); .collect::<UnordMap<Symbol, &ty::AssocItem>>();
let mut names = names let mut names = names
.into_iter() .into_iter()
@ -709,7 +710,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let mut where_constraints = vec![]; let mut where_constraints = vec![];
let mut already_has_generics_args_suggestion = false; let mut already_has_generics_args_suggestion = false;
for (span, assoc_items) in &associated_types { for (span, assoc_items) in &associated_types {
let mut names: FxHashMap<_, usize> = FxHashMap::default(); let mut names: UnordMap<_, usize> = Default::default();
for item in assoc_items { for item in assoc_items {
types_count += 1; types_count += 1;
*names.entry(item.name).or_insert(0) += 1; *names.entry(item.name).or_insert(0) += 1;

View file

@ -16,6 +16,7 @@
use rustc_data_structures::captures::Captures; use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::unord::UnordMap;
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, StashKey}; use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, StashKey};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
@ -979,7 +980,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
}) })
// Check for duplicates // Check for duplicates
.and_then(|list| { .and_then(|list| {
let mut set: FxHashMap<Symbol, Span> = FxHashMap::default(); let mut set: UnordMap<Symbol, Span> = Default::default();
let mut no_dups = true; let mut no_dups = true;
for ident in &*list { for ident in &*list {

View file

@ -1,5 +1,5 @@
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_data_structures::unord::{UnordMap, UnordSet};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_middle::query::Providers; use rustc_middle::query::Providers;
@ -72,7 +72,7 @@ struct ClashingExternDeclarations {
/// the symbol should be reported as a clashing declaration. /// the symbol should be reported as a clashing declaration.
// FIXME: Technically, we could just store a &'tcx str here without issue; however, the // FIXME: Technically, we could just store a &'tcx str here without issue; however, the
// `impl_lint_pass` macro doesn't currently support lints parametric over a lifetime. // `impl_lint_pass` macro doesn't currently support lints parametric over a lifetime.
seen_decls: FxHashMap<Symbol, hir::OwnerId>, seen_decls: UnordMap<Symbol, hir::OwnerId>,
} }
/// Differentiate between whether the name for an extern decl came from the link_name attribute or /// Differentiate between whether the name for an extern decl came from the link_name attribute or
@ -96,7 +96,7 @@ impl SymbolName {
impl ClashingExternDeclarations { impl ClashingExternDeclarations {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
ClashingExternDeclarations { seen_decls: FxHashMap::default() } ClashingExternDeclarations { seen_decls: Default::default() }
} }
/// Insert a new foreign item into the seen set. If a symbol with the same name already exists /// 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>(
b: Ty<'tcx>, b: Ty<'tcx>,
ckind: types::CItemKind, ckind: types::CItemKind,
) -> bool { ) -> bool {
let mut seen_types = FxHashSet::default(); let mut seen_types = UnordSet::default();
structurally_same_type_impl(&mut seen_types, tcx, param_env, a, b, ckind) structurally_same_type_impl(&mut seen_types, tcx, param_env, a, b, ckind)
} }
fn structurally_same_type_impl<'tcx>( fn structurally_same_type_impl<'tcx>(
seen_types: &mut FxHashSet<(Ty<'tcx>, Ty<'tcx>)>, seen_types: &mut UnordSet<(Ty<'tcx>, Ty<'tcx>)>,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
a: Ty<'tcx>, a: Ty<'tcx>,

View file

@ -4,7 +4,8 @@ use crate::lints::{
}; };
use crate::{EarlyContext, EarlyLintPass, LintContext}; use crate::{EarlyContext, EarlyLintPass, LintContext};
use rustc_ast as ast; use rustc_ast as ast;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::unord::UnordMap;
use rustc_span::symbol::Symbol; use rustc_span::symbol::Symbol;
declare_lint! { declare_lint! {
@ -194,8 +195,8 @@ impl EarlyLintPass for NonAsciiIdents {
} }
if has_non_ascii_idents && check_confusable_idents { if has_non_ascii_idents && check_confusable_idents {
let mut skeleton_map: FxHashMap<Symbol, (Symbol, Span, bool)> = let mut skeleton_map: UnordMap<Symbol, (Symbol, Span, bool)> =
FxHashMap::with_capacity_and_hasher(symbols.len(), Default::default()); UnordMap::with_capacity(symbols.len());
let mut skeleton_buf = String::new(); let mut skeleton_buf = String::new();
for (&symbol, &sp) in symbols.iter() { for (&symbol, &sp) in symbols.iter() {
@ -248,8 +249,8 @@ impl EarlyLintPass for NonAsciiIdents {
Verified, Verified,
} }
let mut script_states: FxHashMap<AugmentedScriptSet, ScriptSetUsage> = let mut script_states: FxIndexMap<AugmentedScriptSet, ScriptSetUsage> =
FxHashMap::default(); Default::default();
let latin_augmented_script_set = AugmentedScriptSet::for_char('A'); let latin_augmented_script_set = AugmentedScriptSet::for_char('A');
script_states.insert(latin_augmented_script_set, ScriptSetUsage::Verified); script_states.insert(latin_augmented_script_set, ScriptSetUsage::Verified);

View file

@ -1914,14 +1914,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
empty_proc_macro!(self); empty_proc_macro!(self);
let tcx = self.tcx; let tcx = self.tcx;
let lib_features = tcx.lib_features(LOCAL_CRATE); let lib_features = tcx.lib_features(LOCAL_CRATE);
self.lazy_array(lib_features.to_vec()) self.lazy_array(lib_features.to_sorted_vec())
} }
fn encode_stability_implications(&mut self) -> LazyArray<(Symbol, Symbol)> { fn encode_stability_implications(&mut self) -> LazyArray<(Symbol, Symbol)> {
empty_proc_macro!(self); empty_proc_macro!(self);
let tcx = self.tcx; let tcx = self.tcx;
let implications = tcx.stability_implications(LOCAL_CRATE); let implications = tcx.stability_implications(LOCAL_CRATE);
self.lazy_array(implications.iter().map(|(k, v)| (*k, *v))) let sorted = implications.to_sorted_stable_ord();
self.lazy_array(sorted.into_iter().map(|(k, v)| (*k, *v)))
} }
fn encode_diagnostic_items(&mut self) -> LazyArray<(Symbol, DefIndex)> { fn encode_diagnostic_items(&mut self) -> LazyArray<(Symbol, DefIndex)> {

View file

@ -4,7 +4,7 @@ pub mod dependency_format;
pub mod exported_symbols; pub mod exported_symbols;
pub mod lang_items; pub mod lang_items;
pub mod lib_features { pub mod lib_features {
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::unord::UnordMap;
use rustc_span::{symbol::Symbol, Span}; use rustc_span::{symbol::Symbol, Span};
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
@ -16,15 +16,16 @@ pub mod lib_features {
#[derive(HashStable, Debug, Default)] #[derive(HashStable, Debug, Default)]
pub struct LibFeatures { pub struct LibFeatures {
pub stability: FxHashMap<Symbol, (FeatureStability, Span)>, pub stability: UnordMap<Symbol, (FeatureStability, Span)>,
} }
impl LibFeatures { impl LibFeatures {
pub fn to_vec(&self) -> Vec<(Symbol, FeatureStability)> { pub fn to_sorted_vec(&self) -> Vec<(Symbol, FeatureStability)> {
let mut all_features: Vec<_> = self.stability
self.stability.iter().map(|(&sym, &(stab, _))| (sym, stab)).collect(); .to_sorted_stable_ord()
all_features.sort_unstable_by(|(a, _), (b, _)| a.as_str().cmp(b.as_str())); .iter()
all_features .map(|(&sym, &(stab, _))| (sym, stab))
.collect()
} }
} }
} }

View file

@ -9,6 +9,7 @@ use rustc_attr::{
self as attr, ConstStability, DefaultBodyStability, DeprecatedSince, Deprecation, Stability, self as attr, ConstStability, DefaultBodyStability, DeprecatedSince, Deprecation, Stability,
}; };
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::unord::UnordMap;
use rustc_errors::{Applicability, Diagnostic}; use rustc_errors::{Applicability, Diagnostic};
use rustc_feature::GateIssue; use rustc_feature::GateIssue;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
@ -77,7 +78,7 @@ pub struct Index {
/// to know that the feature implies another feature. If it were reversed, and the `#[stable]` /// to know that the feature implies another feature. If it were reversed, and the `#[stable]`
/// attribute had an `implies` meta item, then a map would be necessary when avoiding a "use of /// attribute had an `implies` meta item, then a map would be necessary when avoiding a "use of
/// unstable feature" error for a feature that was implied. /// unstable feature" error for a feature that was implied.
pub implications: FxHashMap<Symbol, Symbol>, pub implications: UnordMap<Symbol, Symbol>,
} }
impl Index { impl Index {

View file

@ -61,7 +61,7 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::steal::Steal; use rustc_data_structures::steal::Steal;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_data_structures::unord::UnordSet; use rustc_data_structures::unord::{UnordMap, UnordSet};
use rustc_errors::ErrorGuaranteed; use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{DefKind, DocLinkResMap}; use rustc_hir::def::{DefKind, DocLinkResMap};
@ -1739,7 +1739,7 @@ rustc_queries! {
separate_provide_extern separate_provide_extern
arena_cache arena_cache
} }
query stability_implications(_: CrateNum) -> &'tcx FxHashMap<Symbol, Symbol> { query stability_implications(_: CrateNum) -> &'tcx UnordMap<Symbol, Symbol> {
arena_cache arena_cache
desc { "calculating the implications between `#[unstable]` features defined in a crate" } desc { "calculating the implications between `#[unstable]` features defined in a crate" }
separate_provide_extern separate_provide_extern

View file

@ -6,7 +6,8 @@ use rustc_attr::{
self as attr, ConstStability, DeprecatedSince, Stability, StabilityLevel, StableSince, self as attr, ConstStability, DeprecatedSince, Stability, StabilityLevel, StableSince,
Unstable, UnstableReason, VERSION_PLACEHOLDER, Unstable, UnstableReason, VERSION_PLACEHOLDER,
}; };
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap}; use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{LocalDefId, LocalModDefId, CRATE_DEF_ID, LOCAL_CRATE}; 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<'_>) {
} }
let declared_lang_features = &tcx.features().declared_lang_features; let declared_lang_features = &tcx.features().declared_lang_features;
let mut lang_features = FxHashSet::default(); let mut lang_features = UnordSet::default();
for &(feature, span, since) in declared_lang_features { for &(feature, span, since) in declared_lang_features {
if let Some(since) = since { if let Some(since) = since {
// Warn if the user has enabled an already-stable lang feature. // Warn if the user has enabled an already-stable lang feature.
@ -980,11 +981,11 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
fn check_features<'tcx>( fn check_features<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
remaining_lib_features: &mut FxIndexMap<&Symbol, Span>, remaining_lib_features: &mut FxIndexMap<&Symbol, Span>,
remaining_implications: &mut FxHashMap<Symbol, Symbol>, remaining_implications: &mut UnordMap<Symbol, Symbol>,
defined_features: &LibFeatures, defined_features: &LibFeatures,
all_implications: &FxHashMap<Symbol, Symbol>, all_implications: &UnordMap<Symbol, Symbol>,
) { ) {
for (feature, since) in defined_features.to_vec() { for (feature, since) in defined_features.to_sorted_vec() {
if let FeatureStability::AcceptedSince(since) = since if let FeatureStability::AcceptedSince(since) = since
&& let Some(span) = remaining_lib_features.get(&feature) && let Some(span) = remaining_lib_features.get(&feature)
{ {
@ -1021,7 +1022,8 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
// `remaining_lib_features`. // `remaining_lib_features`.
let mut all_implications = remaining_implications.clone(); let mut all_implications = remaining_implications.clone();
for &cnum in tcx.crates(()) { for &cnum in tcx.crates(()) {
all_implications.extend(tcx.stability_implications(cnum)); all_implications
.extend_unord(tcx.stability_implications(cnum).items().map(|(k, v)| (*k, *v)));
} }
check_features( check_features(
@ -1052,8 +1054,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
// We only use the hash map contents to emit errors, and the order of // We only use the hash map contents to emit errors, and the order of
// emitted errors do not affect query stability. // emitted errors do not affect query stability.
#[allow(rustc::potential_query_instability)] for (&implied_by, &feature) in remaining_implications.to_sorted_stable_ord() {
for (implied_by, feature) in remaining_implications {
let local_defined_features = tcx.lib_features(LOCAL_CRATE); let local_defined_features = tcx.lib_features(LOCAL_CRATE);
let span = local_defined_features let span = local_defined_features
.stability .stability