Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1e98e80

Browse files
committedMar 31, 2024·
Auto merge of rust-lang#121851 - michaelwoerister:mcp-533-effective-vis, r=cjgillot
Use FxIndexMap instead FxHashMap to stabilize iteration order in EffectiveVisibilities Part of [MCP 533](rust-lang/compiler-team#533).
2 parents a8cfc83 + 7e4bc4a commit 1e98e80

File tree

2 files changed

+6
-51
lines changed

2 files changed

+6
-51
lines changed
 

‎compiler/rustc_data_structures/src/stable_hasher.rs

+2-46
Original file line numberDiff line numberDiff line change
@@ -684,26 +684,11 @@ where
684684
impl_stable_traits_for_trivial_type!(::std::path::Path);
685685
impl_stable_traits_for_trivial_type!(::std::path::PathBuf);
686686

687-
impl<K, V, R, HCX> HashStable<HCX> for ::std::collections::HashMap<K, V, R>
688-
where
689-
K: ToStableHashKey<HCX> + Eq,
690-
V: HashStable<HCX>,
691-
R: BuildHasher,
692-
{
693-
#[inline]
694-
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
695-
stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, (key, value)| {
696-
let key = key.to_stable_hash_key(hcx);
697-
key.hash_stable(hcx, hasher);
698-
value.hash_stable(hcx, hasher);
699-
});
700-
}
701-
}
702-
703-
// It is not safe to implement HashStable for HashSet or any other collection type
687+
// It is not safe to implement HashStable for HashSet, HashMap or any other collection type
704688
// with unstable but observable iteration order.
705689
// See https://github.com/rust-lang/compiler-team/issues/533 for further information.
706690
impl<V, HCX> !HashStable<HCX> for std::collections::HashSet<V> {}
691+
impl<K, V, HCX> !HashStable<HCX> for std::collections::HashMap<K, V> {}
707692

708693
impl<K, V, HCX> HashStable<HCX> for ::std::collections::BTreeMap<K, V>
709694
where
@@ -730,35 +715,6 @@ where
730715
}
731716
}
732717

733-
fn stable_hash_reduce<HCX, I, C, F>(
734-
hcx: &mut HCX,
735-
hasher: &mut StableHasher,
736-
mut collection: C,
737-
length: usize,
738-
hash_function: F,
739-
) where
740-
C: Iterator<Item = I>,
741-
F: Fn(&mut StableHasher, &mut HCX, I),
742-
{
743-
length.hash_stable(hcx, hasher);
744-
745-
match length {
746-
1 => {
747-
hash_function(hasher, hcx, collection.next().unwrap());
748-
}
749-
_ => {
750-
let hash = collection
751-
.map(|value| {
752-
let mut hasher = StableHasher::new();
753-
hash_function(&mut hasher, hcx, value);
754-
hasher.finish::<Hash128>()
755-
})
756-
.reduce(|accum, value| accum.wrapping_add(value));
757-
hash.hash_stable(hcx, hasher);
758-
}
759-
}
760-
}
761-
762718
/// Controls what data we do or do not hash.
763719
/// Whenever a `HashStable` implementation caches its
764720
/// result, it needs to include `HashingControls` as part

‎compiler/rustc_middle/src/middle/privacy.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! outside their scopes. This pass will also generate a set of exported items
33
//! which are available for use externally when compiled as a library.
44
use crate::ty::{TyCtxt, Visibility};
5-
use rustc_data_structures::fx::FxHashMap;
5+
use rustc_data_structures::fx::{FxIndexMap, IndexEntry};
66
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
77
use rustc_hir::def::DefKind;
88
use rustc_macros::HashStable;
@@ -90,7 +90,7 @@ impl EffectiveVisibility {
9090
/// Holds a map of effective visibilities for reachable HIR nodes.
9191
#[derive(Clone, Debug)]
9292
pub struct EffectiveVisibilities<Id = LocalDefId> {
93-
map: FxHashMap<Id, EffectiveVisibility>,
93+
map: FxIndexMap<Id, EffectiveVisibility>,
9494
}
9595

9696
impl EffectiveVisibilities {
@@ -130,9 +130,8 @@ impl EffectiveVisibilities {
130130
eff_vis: &EffectiveVisibility,
131131
tcx: TyCtxt<'_>,
132132
) {
133-
use std::collections::hash_map::Entry;
134133
match self.map.entry(def_id) {
135-
Entry::Occupied(mut occupied) => {
134+
IndexEntry::Occupied(mut occupied) => {
136135
let old_eff_vis = occupied.get_mut();
137136
for l in Level::all_levels() {
138137
let vis_at_level = eff_vis.at_level(l);
@@ -145,7 +144,7 @@ impl EffectiveVisibilities {
145144
}
146145
old_eff_vis
147146
}
148-
Entry::Vacant(vacant) => vacant.insert(*eff_vis),
147+
IndexEntry::Vacant(vacant) => vacant.insert(*eff_vis),
149148
};
150149
}
151150

0 commit comments

Comments
 (0)
Please sign in to comment.