Remove Clone impl for StableHashingContext.

`HashStable::hash_stable` takes a `&mut Hcx`. In contrast,
`ToStableHashKey::to_stable_hash_key` takes a `&Hcx`. But there are
some places where the latter calls the former, and due to the mismatch a
`clone` call is required to get a mutable `StableHashingContext`.

This commit changes `to_stable_hash_key` to instead take a `&mut Hcx`.
This eliminates the mismatch, the need for the clones, and the need for
the `Clone` impls.
This commit is contained in:
Nicholas Nethercote
2026-04-02 16:06:59 +11:00
parent 55e86c9968
commit ff1795fe49
21 changed files with 59 additions and 59 deletions
@@ -50,7 +50,7 @@ pub trait HashStable<Hcx> {
/// bringing maps into a predictable order before hashing them.
pub trait ToStableHashKey<Hcx> {
type KeyType: Ord + Sized + HashStable<Hcx>;
fn to_stable_hash_key(&self, hcx: &Hcx) -> Self::KeyType;
fn to_stable_hash_key(&self, hcx: &mut Hcx) -> Self::KeyType;
}
/// Trait for marking a type as having a sort order that is
@@ -427,7 +427,7 @@ impl StableOrd for String {
impl<Hcx> ToStableHashKey<Hcx> for String {
type KeyType = String;
#[inline]
fn to_stable_hash_key(&self, _: &Hcx) -> Self::KeyType {
fn to_stable_hash_key(&self, _: &mut Hcx) -> Self::KeyType {
self.clone()
}
}
@@ -435,7 +435,7 @@ fn to_stable_hash_key(&self, _: &Hcx) -> Self::KeyType {
impl<Hcx, T1: ToStableHashKey<Hcx>, T2: ToStableHashKey<Hcx>> ToStableHashKey<Hcx> for (T1, T2) {
type KeyType = (T1::KeyType, T2::KeyType);
#[inline]
fn to_stable_hash_key(&self, hcx: &Hcx) -> Self::KeyType {
fn to_stable_hash_key(&self, hcx: &mut Hcx) -> Self::KeyType {
(self.0.to_stable_hash_key(hcx), self.1.to_stable_hash_key(hcx))
}
}
+12 -8
View File
@@ -143,7 +143,7 @@ pub fn copied(self) -> UnordItems<T, impl Iterator<Item = T>> {
impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
#[inline]
pub fn into_sorted<Hcx>(self, hcx: &Hcx) -> Vec<T>
pub fn into_sorted<Hcx>(self, hcx: &mut Hcx) -> Vec<T>
where
T: ToStableHashKey<Hcx>,
{
@@ -168,7 +168,7 @@ pub fn into_sorted_stable_ord_by_key<K, C>(self, project_to_key: C) -> Vec<T>
}
#[inline]
pub fn collect_sorted<Hcx, C>(self, hcx: &Hcx, cache_sort_key: bool) -> C
pub fn collect_sorted<Hcx, C>(self, hcx: &mut Hcx, cache_sort_key: bool) -> C
where
T: ToStableHashKey<Hcx>,
C: FromIterator<T> + BorrowMut<[T]>,
@@ -315,7 +315,7 @@ pub fn into_items(self) -> UnordItems<V, impl Iterator<Item = V>> {
/// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
/// for `V` is expensive (e.g. a `DefId -> DefPathHash` lookup).
#[inline]
pub fn to_sorted<Hcx>(&self, hcx: &Hcx, cache_sort_key: bool) -> Vec<&V>
pub fn to_sorted<Hcx>(&self, hcx: &mut Hcx, cache_sort_key: bool) -> Vec<&V>
where
V: ToStableHashKey<Hcx>,
{
@@ -357,7 +357,7 @@ pub fn into_sorted_stable_ord(self) -> Vec<V>
/// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
/// for `V` is expensive (e.g. a `DefId -> DefPathHash` lookup).
#[inline]
pub fn into_sorted<Hcx>(self, hcx: &Hcx, cache_sort_key: bool) -> Vec<V>
pub fn into_sorted<Hcx>(self, hcx: &mut Hcx, cache_sort_key: bool) -> Vec<V>
where
V: ToStableHashKey<Hcx>,
{
@@ -555,7 +555,7 @@ pub fn keys(&self) -> UnordItems<&K, impl Iterator<Item = &K>> {
/// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
/// for `K` is expensive (e.g. a `DefId -> DefPathHash` lookup).
#[inline]
pub fn to_sorted<Hcx>(&self, hcx: &Hcx, cache_sort_key: bool) -> Vec<(&K, &V)>
pub fn to_sorted<Hcx>(&self, hcx: &mut Hcx, cache_sort_key: bool) -> Vec<(&K, &V)>
where
K: ToStableHashKey<Hcx>,
{
@@ -582,7 +582,7 @@ pub fn to_sorted_stable_ord(&self) -> Vec<(&K, &V)>
/// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
/// for `K` is expensive (e.g. a `DefId -> DefPathHash` lookup).
#[inline]
pub fn into_sorted<Hcx>(self, hcx: &Hcx, cache_sort_key: bool) -> Vec<(K, V)>
pub fn into_sorted<Hcx>(self, hcx: &mut Hcx, cache_sort_key: bool) -> Vec<(K, V)>
where
K: ToStableHashKey<Hcx>,
{
@@ -610,7 +610,11 @@ pub fn into_sorted_stable_ord(self) -> Vec<(K, V)>
/// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
/// for `K` is expensive (e.g. a `DefId -> DefPathHash` lookup).
#[inline]
pub fn values_sorted<Hcx>(&self, hcx: &Hcx, cache_sort_key: bool) -> impl Iterator<Item = &V>
pub fn values_sorted<Hcx>(
&self,
hcx: &mut Hcx,
cache_sort_key: bool,
) -> impl Iterator<Item = &V>
where
K: ToStableHashKey<Hcx>,
{
@@ -710,7 +714,7 @@ fn hash_stable(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
#[inline]
fn to_sorted_vec<Hcx, T, K, I>(
hcx: &Hcx,
hcx: &mut Hcx,
iter: I,
cache_sort_key: bool,
extract_key: fn(&T) -> &K,