Skip to content

Commit df5d47e

Browse files
committed
Auto merge of rust-lang#120931 - chenyukang:yukang-cleanup-hashmap, r=<try>
Clean up potential_query_instability with FxIndexMap and UnordMap From rust-lang#120485 (comment) r? `@michaelwoerister`
2 parents a84bb95 + 1e32922 commit df5d47e

File tree

30 files changed

+122
-139
lines changed

30 files changed

+122
-139
lines changed

compiler/rustc_borrowck/src/universal_regions.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#![allow(rustc::diagnostic_outside_of_impl)]
1616
#![allow(rustc::untranslatable_diagnostic)]
1717

18-
use rustc_data_structures::fx::FxHashMap;
18+
use rustc_data_structures::fx::FxIndexMap;
1919
use rustc_errors::Diagnostic;
2020
use rustc_hir::def_id::{DefId, LocalDefId};
2121
use rustc_hir::lang_items::LangItem;
@@ -180,7 +180,7 @@ struct UniversalRegionIndices<'tcx> {
180180
/// basically equivalent to an `GenericArgs`, except that it also
181181
/// contains an entry for `ReStatic` -- it might be nice to just
182182
/// use an args, and then handle `ReStatic` another way.
183-
indices: FxHashMap<ty::Region<'tcx>, RegionVid>,
183+
indices: FxIndexMap<ty::Region<'tcx>, RegionVid>,
184184

185185
/// The vid assigned to `'static`. Used only for diagnostics.
186186
pub fr_static: RegionVid,
@@ -325,9 +325,6 @@ impl<'tcx> UniversalRegions<'tcx> {
325325
}
326326

327327
/// Gets an iterator over all the early-bound regions that have names.
328-
/// Iteration order may be unstable, so this should only be used when
329-
/// iteration order doesn't affect anything
330-
#[allow(rustc::potential_query_instability)]
331328
pub fn named_universal_regions<'s>(
332329
&'s self,
333330
) -> impl Iterator<Item = (ty::Region<'tcx>, ty::RegionVid)> + 's {

compiler/rustc_codegen_llvm/src/back/lto.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_middle::dep_graph::WorkProduct;
2121
use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
2222
use rustc_session::config::{self, CrateType, Lto};
2323

24+
use std::collections::BTreeMap;
2425
use std::ffi::{CStr, CString};
2526
use std::fs::File;
2627
use std::io;
@@ -787,7 +788,7 @@ pub unsafe fn optimize_thin_module(
787788
#[derive(Debug, Default)]
788789
pub struct ThinLTOKeysMap {
789790
// key = llvm name of importing module, value = LLVM cache key
790-
keys: FxHashMap<String, String>,
791+
keys: BTreeMap<String, String>,
791792
}
792793

793794
impl ThinLTOKeysMap {
@@ -797,7 +798,6 @@ impl ThinLTOKeysMap {
797798
let mut writer = io::BufWriter::new(file);
798799
// The entries are loaded back into a hash map in `load_from_file()`, so
799800
// the order in which we write them to file here does not matter.
800-
#[allow(rustc::potential_query_instability)]
801801
for (module, key) in &self.keys {
802802
writeln!(writer, "{module} {key}")?;
803803
}
@@ -806,7 +806,7 @@ impl ThinLTOKeysMap {
806806

807807
fn load_from_file(path: &Path) -> io::Result<Self> {
808808
use std::io::BufRead;
809-
let mut keys = FxHashMap::default();
809+
let mut keys = BTreeMap::default();
810810
let file = File::open(path)?;
811811
for line in io::BufReader::new(file).lines() {
812812
let line = line?;

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ fn codegenned_and_inlined_items(tcx: TyCtxt<'_>) -> DefIdSet {
403403
let mut result = items.clone();
404404

405405
for cgu in cgus {
406-
#[allow(rustc::potential_query_instability)]
407406
for item in cgu.items().keys() {
408407
if let mir::mono::MonoItem::Fn(ref instance) = item {
409408
let did = instance.def_id();

compiler/rustc_codegen_ssa/src/assert_module_sources.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
2626
use crate::errors;
2727
use rustc_ast as ast;
28-
use rustc_data_structures::fx::FxHashMap;
28+
use rustc_data_structures::unord::UnordMap;
2929
use rustc_data_structures::unord::UnordSet;
3030
use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg};
3131
use rustc_hir::def_id::LOCAL_CRATE;
@@ -218,8 +218,8 @@ pub enum ComparisonKind {
218218
}
219219

220220
struct TrackerData {
221-
actual_reuse: FxHashMap<String, CguReuse>,
222-
expected_reuse: FxHashMap<String, (String, Span, CguReuse, ComparisonKind)>,
221+
actual_reuse: UnordMap<String, CguReuse>,
222+
expected_reuse: UnordMap<String, (String, Span, CguReuse, ComparisonKind)>,
223223
}
224224

225225
pub struct CguReuseTracker {
@@ -267,9 +267,7 @@ impl CguReuseTracker {
267267

268268
fn check_expected_reuse(&self, sess: &Session) {
269269
if let Some(ref data) = self.data {
270-
#[allow(rustc::potential_query_instability)]
271-
let mut keys = data.expected_reuse.keys().collect::<Vec<_>>();
272-
keys.sort_unstable();
270+
let keys = data.expected_reuse.keys().into_sorted_stable_ord();
273271
for cgu_name in keys {
274272
let &(ref cgu_user_name, ref error_span, expected_reuse, comparison_kind) =
275273
data.expected_reuse.get(cgu_name).unwrap();

compiler/rustc_codegen_ssa/src/back/archive.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_data_structures::fx::FxHashSet;
1+
use rustc_data_structures::fx::FxIndexSet;
22
use rustc_data_structures::memmap::Mmap;
33
use rustc_session::cstore::DllImport;
44
use rustc_session::Session;
@@ -41,7 +41,7 @@ pub trait ArchiveBuilderBuilder {
4141
&'a self,
4242
rlib: &'a Path,
4343
outdir: &Path,
44-
bundled_lib_file_names: &FxHashSet<Symbol>,
44+
bundled_lib_file_names: &FxIndexSet<Symbol>,
4545
) -> Result<(), ExtractBundledLibsError<'_>> {
4646
let archive_map = unsafe {
4747
Mmap::map(

compiler/rustc_codegen_ssa/src/back/link.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use rustc_arena::TypedArena;
22
use rustc_ast::CRATE_NODE_ID;
3-
use rustc_data_structures::fx::FxHashSet;
4-
use rustc_data_structures::fx::FxIndexMap;
3+
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
54
use rustc_data_structures::memmap::Mmap;
65
use rustc_data_structures::temp_dir::MaybeTempDir;
76
use rustc_errors::{DiagCtxt, ErrorGuaranteed};
@@ -534,9 +533,9 @@ fn link_staticlib<'a>(
534533

535534
let native_libs = codegen_results.crate_info.native_libraries[&cnum].iter();
536535
let relevant = native_libs.clone().filter(|lib| relevant_lib(sess, lib));
537-
let relevant_libs: FxHashSet<_> = relevant.filter_map(|lib| lib.filename).collect();
536+
let relevant_libs: FxIndexSet<_> = relevant.filter_map(|lib| lib.filename).collect();
538537

539-
let bundled_libs: FxHashSet<_> = native_libs.filter_map(|lib| lib.filename).collect();
538+
let bundled_libs: FxIndexSet<_> = native_libs.filter_map(|lib| lib.filename).collect();
540539
ab.add_archive(
541540
path,
542541
Box::new(move |fname: &str| {
@@ -564,11 +563,7 @@ fn link_staticlib<'a>(
564563
.extract_bundled_libs(path, tempdir.as_ref(), &relevant_libs)
565564
.unwrap_or_else(|e| sess.dcx().emit_fatal(e));
566565

567-
// We sort the libraries below
568-
#[allow(rustc::potential_query_instability)]
569-
let mut relevant_libs: Vec<Symbol> = relevant_libs.into_iter().collect();
570-
relevant_libs.sort_unstable();
571-
for filename in relevant_libs {
566+
for filename in relevant_libs.iter() {
572567
let joined = tempdir.as_ref().join(filename.as_str());
573568
let path = joined.as_path();
574569
ab.add_archive(path, Box::new(|_| false)).unwrap();
@@ -682,13 +677,14 @@ fn link_dwarf_object<'a>(
682677
}
683678

684679
// Input rlibs contain .o/.dwo files from dependencies.
685-
#[allow(rustc::potential_query_instability)]
686680
let input_rlibs = cg_results
687681
.crate_info
688682
.used_crate_source
689-
.values()
690-
.filter_map(|csource| csource.rlib.as_ref())
691-
.map(|(path, _)| path);
683+
.items()
684+
.filter_map(|(_, csource)| csource.rlib.as_ref())
685+
.map(|(path, _)| path)
686+
.into_sorted_stable_ord();
687+
692688
for input_rlib in input_rlibs {
693689
debug!(?input_rlib);
694690
package.add_input_object(input_rlib)?;
@@ -2456,7 +2452,7 @@ fn add_native_libs_from_crate(
24562452
codegen_results: &CodegenResults,
24572453
tmpdir: &Path,
24582454
search_paths: &SearchPaths,
2459-
bundled_libs: &FxHashSet<Symbol>,
2455+
bundled_libs: &FxIndexSet<Symbol>,
24602456
cnum: CrateNum,
24612457
link_static: bool,
24622458
link_dynamic: bool,
@@ -2777,7 +2773,7 @@ fn add_static_crate<'a>(
27772773
codegen_results: &CodegenResults,
27782774
tmpdir: &Path,
27792775
cnum: CrateNum,
2780-
bundled_lib_file_names: &FxHashSet<Symbol>,
2776+
bundled_lib_file_names: &FxIndexSet<Symbol>,
27812777
) {
27822778
let src = &codegen_results.crate_info.used_crate_source[&cnum];
27832779
let cratepath = &src.rlib.as_ref().unwrap().0;

compiler/rustc_codegen_ssa/src/back/write.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ pub struct CguMessage;
10011001

10021002
struct Diagnostic {
10031003
msgs: Vec<(DiagnosticMessage, Style)>,
1004-
args: FxHashMap<DiagnosticArgName, DiagnosticArgValue>,
1004+
args: FxIndexMap<DiagnosticArgName, DiagnosticArgValue>,
10051005
code: Option<ErrCode>,
10061006
lvl: Level,
10071007
}
@@ -1813,7 +1813,7 @@ impl Translate for SharedEmitter {
18131813

18141814
impl Emitter for SharedEmitter {
18151815
fn emit_diagnostic(&mut self, diag: rustc_errors::Diagnostic) {
1816-
let args: FxHashMap<DiagnosticArgName, DiagnosticArgValue> =
1816+
let args: FxIndexMap<DiagnosticArgName, DiagnosticArgValue> =
18171817
diag.args().map(|(name, arg)| (name.clone(), arg.clone())).collect();
18181818
drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
18191819
msgs: diag.messages.clone(),

compiler/rustc_codegen_ssa/src/base.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ use crate::{CachedModuleCodegen, CompiledModule, CrateInfo, MemFlags, ModuleCode
1616

1717
use rustc_ast::expand::allocator::{global_fn_name, AllocatorKind, ALLOCATOR_METHODS};
1818
use rustc_attr as attr;
19-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
19+
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
2020
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
2121
use rustc_data_structures::sync::par_map;
22+
use rustc_data_structures::unord::UnordMap;
2223
use rustc_hir as hir;
2324
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
2425
use rustc_hir::lang_items::LangItem;
@@ -851,6 +852,8 @@ impl CrateInfo {
851852
// `compiler_builtins` are always placed last to ensure that they're linked correctly.
852853
used_crates.extend(compiler_builtins);
853854

855+
let crates = tcx.crates(());
856+
let n_crates = crates.len();
854857
let mut info = CrateInfo {
855858
target_cpu,
856859
crate_types,
@@ -862,19 +865,15 @@ impl CrateInfo {
862865
is_no_builtins: Default::default(),
863866
native_libraries: Default::default(),
864867
used_libraries: tcx.native_libraries(LOCAL_CRATE).iter().map(Into::into).collect(),
865-
crate_name: Default::default(),
868+
crate_name: UnordMap::with_capacity(n_crates),
866869
used_crates,
867-
used_crate_source: Default::default(),
870+
used_crate_source: UnordMap::with_capacity(n_crates),
868871
dependency_formats: tcx.dependency_formats(()).clone(),
869872
windows_subsystem,
870873
natvis_debugger_visualizers: Default::default(),
871874
};
872-
let crates = tcx.crates(());
873875

874-
let n_crates = crates.len();
875876
info.native_libraries.reserve(n_crates);
876-
info.crate_name.reserve(n_crates);
877-
info.used_crate_source.reserve(n_crates);
878877

879878
for &cnum in crates.iter() {
880879
info.native_libraries
@@ -901,7 +900,7 @@ impl CrateInfo {
901900
// by the compiler, but that's ok because all this stuff is unstable anyway.
902901
let target = &tcx.sess.target;
903902
if !are_upstream_rust_objects_already_included(tcx.sess) {
904-
let missing_weak_lang_items: FxHashSet<Symbol> = info
903+
let missing_weak_lang_items: FxIndexSet<Symbol> = info
905904
.used_crates
906905
.iter()
907906
.flat_map(|&cnum| tcx.missing_lang_items(cnum))

compiler/rustc_codegen_ssa/src/lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ extern crate tracing;
2424
extern crate rustc_middle;
2525

2626
use rustc_ast as ast;
27-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
27+
use rustc_data_structures::fx::FxHashSet;
28+
use rustc_data_structures::fx::FxIndexMap;
2829
use rustc_data_structures::sync::Lrc;
30+
use rustc_data_structures::unord::UnordMap;
2931
use rustc_hir::def_id::CrateNum;
3032
use rustc_middle::dep_graph::WorkProduct;
3133
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
@@ -152,16 +154,16 @@ impl From<&cstore::NativeLib> for NativeLib {
152154
pub struct CrateInfo {
153155
pub target_cpu: String,
154156
pub crate_types: Vec<CrateType>,
155-
pub exported_symbols: FxHashMap<CrateType, Vec<String>>,
156-
pub linked_symbols: FxHashMap<CrateType, Vec<(String, SymbolExportKind)>>,
157+
pub exported_symbols: UnordMap<CrateType, Vec<String>>,
158+
pub linked_symbols: FxIndexMap<CrateType, Vec<(String, SymbolExportKind)>>,
157159
pub local_crate_name: Symbol,
158160
pub compiler_builtins: Option<CrateNum>,
159161
pub profiler_runtime: Option<CrateNum>,
160162
pub is_no_builtins: FxHashSet<CrateNum>,
161-
pub native_libraries: FxHashMap<CrateNum, Vec<NativeLib>>,
162-
pub crate_name: FxHashMap<CrateNum, Symbol>,
163+
pub native_libraries: FxIndexMap<CrateNum, Vec<NativeLib>>,
164+
pub crate_name: UnordMap<CrateNum, Symbol>,
163165
pub used_libraries: Vec<NativeLib>,
164-
pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
166+
pub used_crate_source: UnordMap<CrateNum, Lrc<CrateSource>>,
165167
pub used_crates: Vec<CrateNum>,
166168
pub dependency_formats: Lrc<Dependencies>,
167169
pub windows_subsystem: Option<String>,

compiler/rustc_const_eval/src/interpret/memory.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::fmt;
1313
use std::ptr;
1414

1515
use rustc_ast::Mutability;
16-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
16+
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
1717
use rustc_middle::mir::display_allocation;
1818
use rustc_middle::ty::{self, Instance, ParamEnv, Ty, TyCtxt};
1919
use rustc_target::abi::{Align, HasDataLayout, Size};
@@ -104,13 +104,13 @@ pub struct Memory<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
104104
pub(super) alloc_map: M::MemoryMap,
105105

106106
/// Map for "extra" function pointers.
107-
extra_fn_ptr_map: FxHashMap<AllocId, M::ExtraFnVal>,
107+
extra_fn_ptr_map: FxIndexMap<AllocId, M::ExtraFnVal>,
108108

109109
/// To be able to compare pointers with null, and to check alignment for accesses
110110
/// to ZSTs (where pointers may dangle), we keep track of the size even for allocations
111111
/// that do not exist any more.
112112
// FIXME: this should not be public, but interning currently needs access to it
113-
pub(super) dead_alloc_map: FxHashMap<AllocId, (Size, Align)>,
113+
pub(super) dead_alloc_map: FxIndexMap<AllocId, (Size, Align)>,
114114
}
115115

116116
/// A reference to some allocation that was already bounds-checked for the given region
@@ -135,8 +135,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
135135
pub fn new() -> Self {
136136
Memory {
137137
alloc_map: M::MemoryMap::default(),
138-
extra_fn_ptr_map: FxHashMap::default(),
139-
dead_alloc_map: FxHashMap::default(),
138+
extra_fn_ptr_map: FxIndexMap::default(),
139+
dead_alloc_map: FxIndexMap::default(),
140140
}
141141
}
142142

compiler/rustc_data_structures/src/unord.rs

+5
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,11 @@ impl<K: Eq + Hash, V> UnordMap<K, V> {
524524
UnordItems(self.inner.into_iter())
525525
}
526526

527+
#[inline]
528+
pub fn keys(&self) -> UnordItems<&K, impl Iterator<Item = &K>> {
529+
UnordItems(self.inner.keys())
530+
}
531+
527532
/// Returns the entries of this map in stable sort order (as defined by `ToStableHashKey`).
528533
///
529534
/// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or

compiler/rustc_errors/src/diagnostic.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
CodeSuggestion, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, ErrCode, Level,
44
MultiSpan, SubdiagnosticMessage, Substitution, SubstitutionPart, SuggestionStyle,
55
};
6-
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
6+
use rustc_data_structures::fx::FxIndexMap;
77
use rustc_error_messages::fluent_value_from_str_list_sep_by_and;
88
use rustc_error_messages::FluentValue;
99
use rustc_lint_defs::{Applicability, LintExpectationId};
@@ -105,7 +105,7 @@ pub struct Diagnostic {
105105
pub span: MultiSpan,
106106
pub children: Vec<SubDiagnostic>,
107107
pub suggestions: Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
108-
args: FxHashMap<DiagnosticArgName, DiagnosticArgValue>,
108+
args: FxIndexMap<DiagnosticArgName, DiagnosticArgValue>,
109109

110110
/// This is not used for highlighting or rendering any error message. Rather, it can be used
111111
/// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
@@ -898,9 +898,6 @@ impl Diagnostic {
898898
self
899899
}
900900

901-
// Exact iteration order of diagnostic arguments shouldn't make a difference to output because
902-
// they're only used in interpolation.
903-
#[allow(rustc::potential_query_instability)]
904901
pub fn args(&self) -> impl Iterator<Item = DiagnosticArg<'_>> {
905902
self.args.iter()
906903
}
@@ -914,7 +911,7 @@ impl Diagnostic {
914911
self
915912
}
916913

917-
pub fn replace_args(&mut self, args: FxHashMap<DiagnosticArgName, DiagnosticArgValue>) {
914+
pub fn replace_args(&mut self, args: FxIndexMap<DiagnosticArgName, DiagnosticArgValue>) {
918915
self.args = args;
919916
}
920917

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_data_structures::fx::FxHashMap;
1+
use rustc_data_structures::fx::FxIndexMap;
22
use rustc_errors::{codes::*, struct_span_code_err};
33
use rustc_hir as hir;
44
use rustc_hir::def::{DefKind, Res};
@@ -241,7 +241,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
241241
binding: &ConvertedBinding<'_, 'tcx>,
242242
bounds: &mut Bounds<'tcx>,
243243
speculative: bool,
244-
dup_bindings: &mut FxHashMap<DefId, Span>,
244+
dup_bindings: &mut FxIndexMap<DefId, Span>,
245245
path_span: Span,
246246
only_self_bounds: OnlySelfBounds,
247247
) -> Result<(), ErrorGuaranteed> {

0 commit comments

Comments
 (0)