Skip to content

Commit 87e8639

Browse files
committed
Auto merge of rust-lang#91903 - tmiasko:bit-set-hash, r=jackh726
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.
2 parents 46171fa + d0281bc commit 87e8639

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

compiler/rustc_data_structures/src/stable_hasher.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -478,14 +478,14 @@ where
478478
}
479479

480480
impl<I: vec::Idx, CTX> HashStable<CTX> for bit_set::BitSet<I> {
481-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
482-
self.words().hash_stable(ctx, hasher);
481+
fn hash_stable(&self, _ctx: &mut CTX, hasher: &mut StableHasher) {
482+
::std::hash::Hash::hash(self, hasher);
483483
}
484484
}
485485

486486
impl<R: vec::Idx, C: vec::Idx, CTX> HashStable<CTX> for bit_set::BitMatrix<R, C> {
487-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
488-
self.words().hash_stable(ctx, hasher);
487+
fn hash_stable(&self, _ctx: &mut CTX, hasher: &mut StableHasher) {
488+
::std::hash::Hash::hash(self, hasher);
489489
}
490490
}
491491

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+
}

compiler/rustc_index/src/bit_set.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ macro_rules! bit_relations_inherent_impls {
8787
/// to or greater than the domain size. All operations that involve two bitsets
8888
/// will panic if the bitsets have differing domain sizes.
8989
///
90-
#[derive(Eq, PartialEq, Decodable, Encodable)]
90+
#[derive(Eq, PartialEq, Hash, Decodable, Encodable)]
9191
pub struct BitSet<T> {
9292
domain_size: usize,
9393
words: Vec<Word>,
@@ -987,7 +987,7 @@ impl<T: Idx> GrowableBitSet<T> {
987987
///
988988
/// All operations that involve a row and/or column index will panic if the
989989
/// index exceeds the relevant bound.
990-
#[derive(Clone, Eq, PartialEq, Decodable, Encodable)]
990+
#[derive(Clone, Eq, PartialEq, Hash, Decodable, Encodable)]
991991
pub struct BitMatrix<R: Idx, C: Idx> {
992992
num_rows: usize,
993993
num_columns: usize,

0 commit comments

Comments
 (0)