Skip to content

Commit d0281bc

Browse files
committed
Implement StableHash for BitSet and BitMatrix via Hash
This fixes an issue where bit sets / bit matrices the same word content but a different domain size would receive the same hash.
1 parent d496cca commit d0281bc

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

compiler/rustc_data_structures/src/stable_hasher.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,14 @@ where
476476
}
477477

478478
impl<I: vec::Idx, CTX> HashStable<CTX> for bit_set::BitSet<I> {
479-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
480-
self.words().hash_stable(ctx, hasher);
479+
fn hash_stable(&self, _ctx: &mut CTX, hasher: &mut StableHasher) {
480+
::std::hash::Hash::hash(self, hasher);
481481
}
482482
}
483483

484484
impl<R: vec::Idx, C: vec::Idx, CTX> HashStable<CTX> for bit_set::BitMatrix<R, C> {
485-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
486-
self.words().hash_stable(ctx, hasher);
485+
fn hash_stable(&self, _ctx: &mut CTX, hasher: &mut StableHasher) {
486+
::std::hash::Hash::hash(self, hasher);
487487
}
488488
}
489489

compiler/rustc_data_structures/src/stable_hasher/tests.rs

+27
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,30 @@ fn test_hash_isize() {
7171

7272
assert_eq!(h.finalize(), expected);
7373
}
74+
75+
fn hash<T: HashStable<()>>(t: &T) -> u128 {
76+
let mut h = StableHasher::new();
77+
let ctx = &mut ();
78+
t.hash_stable(ctx, &mut h);
79+
h.finish()
80+
}
81+
82+
// Check that bit set hash includes the domain size.
83+
#[test]
84+
fn test_hash_bit_set() {
85+
use rustc_index::bit_set::BitSet;
86+
let a: BitSet<usize> = BitSet::new_empty(1);
87+
let b: BitSet<usize> = BitSet::new_empty(2);
88+
assert_ne!(a, b);
89+
assert_ne!(hash(&a), hash(&b));
90+
}
91+
92+
// Check that bit matrix hash includes the matrix dimensions.
93+
#[test]
94+
fn test_hash_bit_matrix() {
95+
use rustc_index::bit_set::BitMatrix;
96+
let a: BitMatrix<usize, usize> = BitMatrix::new(1, 1);
97+
let b: BitMatrix<usize, usize> = BitMatrix::new(1, 2);
98+
assert_ne!(a, b);
99+
assert_ne!(hash(&a), hash(&b));
100+
}

0 commit comments

Comments
 (0)