From 333c11433b537f7763f6a262655aadc61e58e600 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 9 Nov 2019 22:57:25 +0100 Subject: [PATCH] Derive HashStable in librustc_target. --- src/librustc/ty/layout.rs | 152 --------------------------------- src/librustc_target/abi/mod.rs | 67 +++++++++++++-- 2 files changed, 59 insertions(+), 160 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 972452601ddd..1400d5fb899a 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -2327,158 +2327,6 @@ where } } -impl<'a> HashStable> for Variants { - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - use crate::ty::layout::Variants::*; - mem::discriminant(self).hash_stable(hcx, hasher); - - match *self { - Single { index } => { - index.hash_stable(hcx, hasher); - } - Multiple { - ref discr, - ref discr_kind, - discr_index, - ref variants, - } => { - discr.hash_stable(hcx, hasher); - discr_kind.hash_stable(hcx, hasher); - discr_index.hash_stable(hcx, hasher); - variants.hash_stable(hcx, hasher); - } - } - } -} - -impl<'a> HashStable> for DiscriminantKind { - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - use crate::ty::layout::DiscriminantKind::*; - mem::discriminant(self).hash_stable(hcx, hasher); - - match *self { - Tag => {} - Niche { - dataful_variant, - ref niche_variants, - niche_start, - } => { - dataful_variant.hash_stable(hcx, hasher); - niche_variants.start().hash_stable(hcx, hasher); - niche_variants.end().hash_stable(hcx, hasher); - niche_start.hash_stable(hcx, hasher); - } - } - } -} - -impl<'a> HashStable> for FieldPlacement { - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - use crate::ty::layout::FieldPlacement::*; - mem::discriminant(self).hash_stable(hcx, hasher); - - match *self { - Union(count) => { - count.hash_stable(hcx, hasher); - } - Array { count, stride } => { - count.hash_stable(hcx, hasher); - stride.hash_stable(hcx, hasher); - } - Arbitrary { ref offsets, ref memory_index } => { - offsets.hash_stable(hcx, hasher); - memory_index.hash_stable(hcx, hasher); - } - } - } -} - -impl<'a> HashStable> for VariantIdx { - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - self.as_u32().hash_stable(hcx, hasher) - } -} - -impl<'a> HashStable> for Abi { - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - use crate::ty::layout::Abi::*; - mem::discriminant(self).hash_stable(hcx, hasher); - - match *self { - Uninhabited => {} - Scalar(ref value) => { - value.hash_stable(hcx, hasher); - } - ScalarPair(ref a, ref b) => { - a.hash_stable(hcx, hasher); - b.hash_stable(hcx, hasher); - } - Vector { ref element, count } => { - element.hash_stable(hcx, hasher); - count.hash_stable(hcx, hasher); - } - Aggregate { sized } => { - sized.hash_stable(hcx, hasher); - } - } - } -} - -impl<'a> HashStable> for Scalar { - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - let Scalar { value, ref valid_range } = *self; - value.hash_stable(hcx, hasher); - valid_range.start().hash_stable(hcx, hasher); - valid_range.end().hash_stable(hcx, hasher); - } -} - -impl_stable_hash_for!(struct crate::ty::layout::Niche { - offset, - scalar -}); - -impl_stable_hash_for!(struct crate::ty::layout::LayoutDetails { - variants, - fields, - abi, - largest_niche, - size, - align -}); - -impl_stable_hash_for!(enum crate::ty::layout::Integer { - I8, - I16, - I32, - I64, - I128 -}); - -impl_stable_hash_for!(enum crate::ty::layout::Primitive { - Int(integer, signed), - F32, - F64, - Pointer -}); - -impl_stable_hash_for!(struct crate::ty::layout::AbiAndPrefAlign { - abi, - pref -}); - -impl<'tcx> HashStable> for Align { - fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) { - self.bytes().hash_stable(hcx, hasher); - } -} - -impl<'tcx> HashStable> for Size { - fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) { - self.bytes().hash_stable(hcx, hasher); - } -} - impl<'a, 'tcx> HashStable> for LayoutError<'tcx> { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { use crate::ty::layout::LayoutError::*; diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 2d7e05037ba0..ff13218831c0 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -6,6 +6,8 @@ use crate::spec::Target; use std::ops::{Add, Deref, Sub, Mul, AddAssign, Range, RangeInclusive}; use rustc_index::vec::{Idx, IndexVec}; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; +use rustc_macros::HashStable_Generic; use syntax_pos::Span; pub mod call; @@ -246,6 +248,12 @@ pub struct Size { raw: u64 } +impl HashStable for Size { + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + self.bytes().hash_stable(hcx, hasher); + } +} + impl Size { pub const ZERO: Size = Self::from_bytes(0); @@ -369,6 +377,12 @@ pub struct Align { pow2: u8, } +impl HashStable for Align { + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + self.bytes().hash_stable(hcx, hasher); + } +} + impl Align { pub fn from_bits(bits: u64) -> Result { Align::from_bytes(Size::from_bits(bits).bytes()) @@ -422,7 +436,8 @@ impl Align { } /// A pair of aligments, ABI-mandated and preferred. -#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)] +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, + RustcEncodable, RustcDecodable, HashStable_Generic)] pub struct AbiAndPrefAlign { pub abi: Align, pub pref: Align, @@ -452,7 +467,7 @@ impl AbiAndPrefAlign { } /// Integers, also used for enum discriminants. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, HashStable_Generic)] pub enum Integer { I8, I16, @@ -533,7 +548,7 @@ impl Integer { } /// Fundamental unit of memory access and layout. -#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)] pub enum Primitive { /// The `bool` is the signedness of the `Integer` type. /// @@ -608,6 +623,15 @@ pub struct Scalar { pub valid_range: RangeInclusive, } +impl HashStable for Scalar { + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + let Scalar { value, ref valid_range } = *self; + value.hash_stable(hcx, hasher); + valid_range.start().hash_stable(hcx, hasher); + valid_range.end().hash_stable(hcx, hasher); + } +} + impl Scalar { pub fn is_bool(&self) -> bool { if let Int(I8, _) = self.value { @@ -636,7 +660,7 @@ impl Scalar { } /// Describes how the fields of a type are located in memory. -#[derive(PartialEq, Eq, Hash, Debug)] +#[derive(PartialEq, Eq, Hash, Debug, HashStable_Generic)] pub enum FieldPlacement { /// All fields start at no offset. The `usize` is the field count. /// @@ -752,7 +776,7 @@ impl FieldPlacement { /// Describes how values of the type are passed by target ABIs, /// in terms of categories of C types there are ABI rules for. -#[derive(Clone, PartialEq, Eq, Hash, Debug)] +#[derive(Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)] pub enum Abi { Uninhabited, Scalar(Scalar), @@ -803,7 +827,13 @@ rustc_index::newtype_index! { pub struct VariantIdx { .. } } -#[derive(PartialEq, Eq, Hash, Debug)] +impl HashStable for VariantIdx { + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + self.as_u32().hash_stable(hcx, hasher) + } +} + +#[derive(PartialEq, Eq, Hash, Debug, HashStable_Generic)] pub enum Variants { /// Single enum variants, structs/tuples, unions, and all non-ADTs. Single { @@ -842,7 +872,28 @@ pub enum DiscriminantKind { }, } -#[derive(Clone, PartialEq, Eq, Hash, Debug)] +impl HashStable for DiscriminantKind { + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + use DiscriminantKind::*; + std::mem::discriminant(self).hash_stable(hcx, hasher); + + match *self { + Tag => {} + Niche { + dataful_variant, + ref niche_variants, + niche_start, + } => { + dataful_variant.hash_stable(hcx, hasher); + niche_variants.start().hash_stable(hcx, hasher); + niche_variants.end().hash_stable(hcx, hasher); + niche_start.hash_stable(hcx, hasher); + } + } + } +} + +#[derive(Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)] pub struct Niche { pub offset: Size, pub scalar: Scalar, @@ -906,7 +957,7 @@ impl Niche { } } -#[derive(PartialEq, Eq, Hash, Debug)] +#[derive(PartialEq, Eq, Hash, Debug, HashStable_Generic)] pub struct LayoutDetails { pub variants: Variants, pub fields: FieldPlacement,