Skip to content

Commit a79c06a

Browse files
Document requirements for HashStable implementations better.
1 parent eed1e1e commit a79c06a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/librustc_data_structures/stable_hasher.rs

+24
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,30 @@ impl<W> Hasher for StableHasher<W> {
140140

141141
/// Something that implements `HashStable<CTX>` can be hashed in a way that is
142142
/// stable across multiple compilation sessions.
143+
///
144+
/// Note that `HashStable` imposes rather more strict requirements than usual
145+
/// hash functions:
146+
///
147+
/// - Stable hashes are sometimes used as identifiers. Therefore they must
148+
/// conform to the corresponding `PartialEq` implementations:
149+
///
150+
/// - `x == y` implies `hash_stable(x) == hash_stable(y)`, and
151+
/// - `x != y` implies `hash_stable(x) != hash_stable(y)`.
152+
///
153+
/// That second condition is usually not required for hash functions
154+
/// (e.g. `Hash`). In practice this means that `hash_stable` must feed any
155+
/// information into the hasher that a `PartialEq` comparision takes into
156+
/// account. See [#49300](https://github.com/rust-lang/rust/issues/49300)
157+
/// for an example where violating this invariant has caused trouble in the
158+
/// past.
159+
///
160+
/// - `hash_stable()` must be independent of the current
161+
/// compilation session. E.g. they must not hash memory addresses or other
162+
/// things that are "randomly" assigned per compilation session.
163+
///
164+
/// - `hash_stable()` must be independent of the host architecture. The
165+
/// `StableHasher` takes care of endianness and `isize`/`usize` platform
166+
/// differences.
143167
pub trait HashStable<CTX> {
144168
fn hash_stable<W: StableHasherResult>(&self,
145169
hcx: &mut CTX,

0 commit comments

Comments
 (0)