Skip to content

Commit f51ebab

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 980cf08 + 487167c commit f51ebab

File tree

15 files changed

+62
-68
lines changed

15 files changed

+62
-68
lines changed

compiler/rustc_borrowck/src/region_infer/values.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use rustc_data_structures::fx::FxHashSet;
21
use rustc_data_structures::fx::FxIndexSet;
32
use rustc_index::bit_set::SparseBitMatrix;
43
use rustc_index::interval::IntervalSet;
@@ -42,7 +41,7 @@ pub(crate) struct LivenessValues {
4241

4342
/// Which regions are live. This is exclusive with the fine-grained tracking in `points`, and
4443
/// currently only used for validating promoteds (which don't care about more precise tracking).
45-
live_regions: Option<FxHashSet<RegionVid>>,
44+
live_regions: Option<FxIndexSet<RegionVid>>,
4645

4746
/// For each region: the points where it is live.
4847
///
@@ -104,10 +103,6 @@ impl LivenessValues {
104103
self.points.as_ref().expect("use with_specific_points").rows()
105104
}
106105

107-
/// Iterate through each region that has a value in this set.
108-
// We are passing query instability implications to the caller.
109-
#[rustc_lint_query_instability]
110-
#[allow(rustc::potential_query_instability)]
111106
pub(crate) fn live_regions_unordered(&self) -> impl Iterator<Item = RegionVid> + '_ {
112107
self.live_regions.as_ref().unwrap().iter().copied()
113108
}

compiler/rustc_borrowck/src/type_check/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
599599
//
600600
// add_location doesn't care about ordering so not a problem for the live regions to be
601601
// unordered.
602-
#[allow(rustc::potential_query_instability)]
603602
for region in liveness_constraints.live_regions_unordered() {
604603
self.cx
605604
.borrowck_context

compiler/rustc_borrowck/src/universal_regions.rs

+2-3
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,
@@ -327,7 +327,6 @@ impl<'tcx> UniversalRegions<'tcx> {
327327
/// Gets an iterator over all the early-bound regions that have names.
328328
/// Iteration order may be unstable, so this should only be used when
329329
/// iteration order doesn't affect anything
330-
#[allow(rustc::potential_query_instability)]
331330
pub fn named_universal_regions<'s>(
332331
&'s self,
333332
) -> impl Iterator<Item = (ty::Region<'tcx>, ty::RegionVid)> + 's {

compiler/rustc_codegen_llvm/src/back/lto.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_codegen_ssa::traits::*;
1414
use rustc_codegen_ssa::{looks_like_rust_object_file, ModuleCodegen, ModuleKind};
1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_data_structures::memmap::Mmap;
17+
use rustc_data_structures::unord::UnordMap;
1718
use rustc_errors::{DiagCtxt, FatalError};
1819
use rustc_hir::def_id::LOCAL_CRATE;
1920
use rustc_middle::bug;
@@ -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: UnordMap<String, String>,
791792
}
792793

793794
impl ThinLTOKeysMap {
@@ -797,16 +798,15 @@ 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)]
801-
for (module, key) in &self.keys {
801+
for (module, key) in self.keys.items().into_sorted_stable_ord() {
802802
writeln!(writer, "{module} {key}")?;
803803
}
804804
Ok(())
805805
}
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 = UnordMap::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

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_arena::TypedArena;
22
use rustc_ast::CRATE_NODE_ID;
3-
use rustc_data_structures::fx::FxHashSet;
43
use rustc_data_structures::fx::FxIndexMap;
4+
use rustc_data_structures::fx::FxIndexSet;
55
use rustc_data_structures::memmap::Mmap;
66
use rustc_data_structures::temp_dir::MaybeTempDir;
77
use rustc_errors::{DiagCtxt, ErrorGuaranteed};
@@ -534,9 +534,9 @@ fn link_staticlib<'a>(
534534

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

539-
let bundled_libs: FxHashSet<_> = native_libs.filter_map(|lib| lib.filename).collect();
539+
let bundled_libs: FxIndexSet<_> = native_libs.filter_map(|lib| lib.filename).collect();
540540
ab.add_archive(
541541
path,
542542
Box::new(move |fname: &str| {
@@ -565,7 +565,6 @@ fn link_staticlib<'a>(
565565
.unwrap_or_else(|e| sess.dcx().emit_fatal(e));
566566

567567
// We sort the libraries below
568-
#[allow(rustc::potential_query_instability)]
569568
let mut relevant_libs: Vec<Symbol> = relevant_libs.into_iter().collect();
570569
relevant_libs.sort_unstable();
571570
for filename in relevant_libs {
@@ -682,13 +681,14 @@ fn link_dwarf_object<'a>(
682681
}
683682

684683
// Input rlibs contain .o/.dwo files from dependencies.
685-
#[allow(rustc::potential_query_instability)]
686684
let input_rlibs = cg_results
687685
.crate_info
688686
.used_crate_source
689-
.values()
690-
.filter_map(|csource| csource.rlib.as_ref())
691-
.map(|(path, _)| path);
687+
.items()
688+
.filter_map(|(_, csource)| csource.rlib.as_ref())
689+
.map(|(path, _)| path)
690+
.into_sorted_stable_ord();
691+
692692
for input_rlib in input_rlibs {
693693
debug!(?input_rlib);
694694
package.add_input_object(input_rlib)?;
@@ -2456,7 +2456,7 @@ fn add_native_libs_from_crate(
24562456
codegen_results: &CodegenResults,
24572457
tmpdir: &Path,
24582458
search_paths: &SearchPaths,
2459-
bundled_libs: &FxHashSet<Symbol>,
2459+
bundled_libs: &FxIndexSet<Symbol>,
24602460
cnum: CrateNum,
24612461
link_static: bool,
24622462
link_dynamic: bool,
@@ -2777,7 +2777,7 @@ fn add_static_crate<'a>(
27772777
codegen_results: &CodegenResults,
27782778
tmpdir: &Path,
27792779
cnum: CrateNum,
2780-
bundled_lib_file_names: &FxHashSet<Symbol>,
2780+
bundled_lib_file_names: &FxIndexSet<Symbol>,
27812781
) {
27822782
let src = &codegen_results.crate_info.used_crate_source[&cnum];
27832783
let cratepath = &src.rlib.as_ref().unwrap().0;

compiler/rustc_codegen_ssa/src/base.rs

+7-9
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))
@@ -915,7 +914,6 @@ impl CrateInfo {
915914

916915
// This loop only adds new items to values of the hash map, so the order in which we
917916
// iterate over the values is not important.
918-
#[allow(rustc::potential_query_instability)]
919917
info.linked_symbols
920918
.iter_mut()
921919
.filter(|(crate_type, _)| {

compiler/rustc_codegen_ssa/src/lib.rs

+6-4
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::FxIndexMap;
2728
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
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>,
161163
pub native_libraries: FxHashMap<CrateNum, Vec<NativeLib>>,
162-
pub crate_name: FxHashMap<CrateNum, Symbol>,
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_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_hir_typeck/src/expr.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::{
2424
use rustc_ast as ast;
2525
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2626
use rustc_data_structures::stack::ensure_sufficient_stack;
27+
use rustc_data_structures::unord::UnordMap;
2728
use rustc_errors::{
2829
codes::*, pluralize, struct_span_code_err, AddToDiagnostic, Applicability, Diagnostic,
2930
DiagnosticBuilder, ErrCode, ErrorGuaranteed, StashKey,
@@ -1716,7 +1717,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17161717
.fields
17171718
.iter_enumerated()
17181719
.map(|(i, field)| (field.ident(tcx).normalize_to_macros_2_0(), (i, field)))
1719-
.collect::<FxHashMap<_, _>>();
1720+
.collect::<UnordMap<_, _>>();
17201721

17211722
let mut seen_fields = FxHashMap::default();
17221723

@@ -1960,18 +1961,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19601961
&self,
19611962
adt_ty: Ty<'tcx>,
19621963
span: Span,
1963-
remaining_fields: FxHashMap<Ident, (FieldIdx, &ty::FieldDef)>,
1964+
remaining_fields: UnordMap<Ident, (FieldIdx, &ty::FieldDef)>,
19641965
variant: &'tcx ty::VariantDef,
19651966
ast_fields: &'tcx [hir::ExprField<'tcx>],
19661967
args: GenericArgsRef<'tcx>,
19671968
) {
19681969
let len = remaining_fields.len();
19691970

1970-
#[allow(rustc::potential_query_instability)]
1971-
let mut displayable_field_names: Vec<&str> =
1972-
remaining_fields.keys().map(|ident| ident.as_str()).collect();
1973-
// sorting &str primitives here, sort_unstable is ok
1974-
displayable_field_names.sort_unstable();
1971+
let displayable_field_names: Vec<&str> =
1972+
remaining_fields.items().map(|(ident, _)| ident.as_str()).into_sorted_stable_ord();
19751973

19761974
let mut truncated_fields_error = String::new();
19771975
let remaining_fields_names = match &displayable_field_names[..] {

0 commit comments

Comments
 (0)