Derive HashStable for Allocation

Requires a manual implementation for Relocations since dereferencing to
SortedMap is not always implemented but that one is far more trivial.
Added fields could otherwise be silently forgotten since private fields
make destructing outside the module possible only with `..` pattern
which would then also be applicable to newly added public fields.
This commit is contained in:
Andreas Molzer 2019-08-17 19:49:51 +02:00
parent 9b9eecf964
commit 2228b3f086
2 changed files with 20 additions and 19 deletions

View file

@ -168,31 +168,21 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::AllocId {
}
}
// Allocations treat their relocations specially
impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::Allocation {
// `Relocations` with default type parameters is a sorted map.
impl<'a, Tag> HashStable<StableHashingContext<'a>>
for mir::interpret::Relocations<Tag>
where
Tag: HashStable<StableHashingContext<'a>>,
{
fn hash_stable<W: StableHasherResult>(
&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>,
) {
let mir::interpret::Allocation {
relocations, align, mutability, size,
extra: _,
.. /* private bytes and undef_mask */
} = self;
let bytes = self.inspect_with_undef_and_ptr_outside_interpreter(0..self.len());
let undef_mask = self.undef_mask();
bytes.hash_stable(hcx, hasher);
relocations.len().hash_stable(hcx, hasher);
for reloc in relocations.iter() {
self.len().hash_stable(hcx, hasher);
for reloc in self.iter() {
reloc.hash_stable(hcx, hasher);
}
undef_mask.hash_stable(hcx, hasher);
size.hash_stable(hcx, hasher);
align.hash_stable(hcx, hasher);
mutability.hash_stable(hcx, hasher);
}
}

View file

@ -13,7 +13,18 @@ use rustc_data_structures::sorted_map::SortedMap;
use rustc_target::abi::HasDataLayout;
use std::borrow::Cow;
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
#[derive(
Clone,
Debug,
Eq,
PartialEq,
PartialOrd,
Ord,
Hash,
RustcEncodable,
RustcDecodable,
HashStable,
)]
pub struct Allocation<Tag=(),Extra=()> {
/// The actual bytes of the allocation.
/// Note that the bytes of a pointer represent the offset of the pointer.