File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed
src/librustc_data_structures Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff 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.
143167pub trait HashStable < CTX > {
144168 fn hash_stable < W : StableHasherResult > ( & self ,
145169 hcx : & mut CTX ,
You can’t perform that action at this time.
0 commit comments