Skip to content

Commit 333c114

Browse files
committed
Derive HashStable in librustc_target.
1 parent 2ba84c6 commit 333c114

File tree

2 files changed

+59
-160
lines changed

2 files changed

+59
-160
lines changed

src/librustc/ty/layout.rs

Lines changed: 0 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,158 +2327,6 @@ where
23272327
}
23282328
}
23292329

2330-
impl<'a> HashStable<StableHashingContext<'a>> for Variants {
2331-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
2332-
use crate::ty::layout::Variants::*;
2333-
mem::discriminant(self).hash_stable(hcx, hasher);
2334-
2335-
match *self {
2336-
Single { index } => {
2337-
index.hash_stable(hcx, hasher);
2338-
}
2339-
Multiple {
2340-
ref discr,
2341-
ref discr_kind,
2342-
discr_index,
2343-
ref variants,
2344-
} => {
2345-
discr.hash_stable(hcx, hasher);
2346-
discr_kind.hash_stable(hcx, hasher);
2347-
discr_index.hash_stable(hcx, hasher);
2348-
variants.hash_stable(hcx, hasher);
2349-
}
2350-
}
2351-
}
2352-
}
2353-
2354-
impl<'a> HashStable<StableHashingContext<'a>> for DiscriminantKind {
2355-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
2356-
use crate::ty::layout::DiscriminantKind::*;
2357-
mem::discriminant(self).hash_stable(hcx, hasher);
2358-
2359-
match *self {
2360-
Tag => {}
2361-
Niche {
2362-
dataful_variant,
2363-
ref niche_variants,
2364-
niche_start,
2365-
} => {
2366-
dataful_variant.hash_stable(hcx, hasher);
2367-
niche_variants.start().hash_stable(hcx, hasher);
2368-
niche_variants.end().hash_stable(hcx, hasher);
2369-
niche_start.hash_stable(hcx, hasher);
2370-
}
2371-
}
2372-
}
2373-
}
2374-
2375-
impl<'a> HashStable<StableHashingContext<'a>> for FieldPlacement {
2376-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
2377-
use crate::ty::layout::FieldPlacement::*;
2378-
mem::discriminant(self).hash_stable(hcx, hasher);
2379-
2380-
match *self {
2381-
Union(count) => {
2382-
count.hash_stable(hcx, hasher);
2383-
}
2384-
Array { count, stride } => {
2385-
count.hash_stable(hcx, hasher);
2386-
stride.hash_stable(hcx, hasher);
2387-
}
2388-
Arbitrary { ref offsets, ref memory_index } => {
2389-
offsets.hash_stable(hcx, hasher);
2390-
memory_index.hash_stable(hcx, hasher);
2391-
}
2392-
}
2393-
}
2394-
}
2395-
2396-
impl<'a> HashStable<StableHashingContext<'a>> for VariantIdx {
2397-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
2398-
self.as_u32().hash_stable(hcx, hasher)
2399-
}
2400-
}
2401-
2402-
impl<'a> HashStable<StableHashingContext<'a>> for Abi {
2403-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
2404-
use crate::ty::layout::Abi::*;
2405-
mem::discriminant(self).hash_stable(hcx, hasher);
2406-
2407-
match *self {
2408-
Uninhabited => {}
2409-
Scalar(ref value) => {
2410-
value.hash_stable(hcx, hasher);
2411-
}
2412-
ScalarPair(ref a, ref b) => {
2413-
a.hash_stable(hcx, hasher);
2414-
b.hash_stable(hcx, hasher);
2415-
}
2416-
Vector { ref element, count } => {
2417-
element.hash_stable(hcx, hasher);
2418-
count.hash_stable(hcx, hasher);
2419-
}
2420-
Aggregate { sized } => {
2421-
sized.hash_stable(hcx, hasher);
2422-
}
2423-
}
2424-
}
2425-
}
2426-
2427-
impl<'a> HashStable<StableHashingContext<'a>> for Scalar {
2428-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
2429-
let Scalar { value, ref valid_range } = *self;
2430-
value.hash_stable(hcx, hasher);
2431-
valid_range.start().hash_stable(hcx, hasher);
2432-
valid_range.end().hash_stable(hcx, hasher);
2433-
}
2434-
}
2435-
2436-
impl_stable_hash_for!(struct crate::ty::layout::Niche {
2437-
offset,
2438-
scalar
2439-
});
2440-
2441-
impl_stable_hash_for!(struct crate::ty::layout::LayoutDetails {
2442-
variants,
2443-
fields,
2444-
abi,
2445-
largest_niche,
2446-
size,
2447-
align
2448-
});
2449-
2450-
impl_stable_hash_for!(enum crate::ty::layout::Integer {
2451-
I8,
2452-
I16,
2453-
I32,
2454-
I64,
2455-
I128
2456-
});
2457-
2458-
impl_stable_hash_for!(enum crate::ty::layout::Primitive {
2459-
Int(integer, signed),
2460-
F32,
2461-
F64,
2462-
Pointer
2463-
});
2464-
2465-
impl_stable_hash_for!(struct crate::ty::layout::AbiAndPrefAlign {
2466-
abi,
2467-
pref
2468-
});
2469-
2470-
impl<'tcx> HashStable<StableHashingContext<'tcx>> for Align {
2471-
fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
2472-
self.bytes().hash_stable(hcx, hasher);
2473-
}
2474-
}
2475-
2476-
impl<'tcx> HashStable<StableHashingContext<'tcx>> for Size {
2477-
fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
2478-
self.bytes().hash_stable(hcx, hasher);
2479-
}
2480-
}
2481-
24822330
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for LayoutError<'tcx> {
24832331
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
24842332
use crate::ty::layout::LayoutError::*;

src/librustc_target/abi/mod.rs

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use crate::spec::Target;
66
use std::ops::{Add, Deref, Sub, Mul, AddAssign, Range, RangeInclusive};
77

88
use rustc_index::vec::{Idx, IndexVec};
9+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
10+
use rustc_macros::HashStable_Generic;
911
use syntax_pos::Span;
1012

1113
pub mod call;
@@ -246,6 +248,12 @@ pub struct Size {
246248
raw: u64
247249
}
248250

251+
impl<CTX> HashStable<CTX> for Size {
252+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
253+
self.bytes().hash_stable(hcx, hasher);
254+
}
255+
}
256+
249257
impl Size {
250258
pub const ZERO: Size = Self::from_bytes(0);
251259

@@ -369,6 +377,12 @@ pub struct Align {
369377
pow2: u8,
370378
}
371379

380+
impl<CTX> HashStable<CTX> for Align {
381+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
382+
self.bytes().hash_stable(hcx, hasher);
383+
}
384+
}
385+
372386
impl Align {
373387
pub fn from_bits(bits: u64) -> Result<Align, String> {
374388
Align::from_bytes(Size::from_bits(bits).bytes())
@@ -422,7 +436,8 @@ impl Align {
422436
}
423437

424438
/// A pair of aligments, ABI-mandated and preferred.
425-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
439+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug,
440+
RustcEncodable, RustcDecodable, HashStable_Generic)]
426441
pub struct AbiAndPrefAlign {
427442
pub abi: Align,
428443
pub pref: Align,
@@ -452,7 +467,7 @@ impl AbiAndPrefAlign {
452467
}
453468

454469
/// Integers, also used for enum discriminants.
455-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
470+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, HashStable_Generic)]
456471
pub enum Integer {
457472
I8,
458473
I16,
@@ -533,7 +548,7 @@ impl Integer {
533548
}
534549

535550
/// Fundamental unit of memory access and layout.
536-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
551+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
537552
pub enum Primitive {
538553
/// The `bool` is the signedness of the `Integer` type.
539554
///
@@ -608,6 +623,15 @@ pub struct Scalar {
608623
pub valid_range: RangeInclusive<u128>,
609624
}
610625

626+
impl<CTX> HashStable<CTX> for Scalar {
627+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
628+
let Scalar { value, ref valid_range } = *self;
629+
value.hash_stable(hcx, hasher);
630+
valid_range.start().hash_stable(hcx, hasher);
631+
valid_range.end().hash_stable(hcx, hasher);
632+
}
633+
}
634+
611635
impl Scalar {
612636
pub fn is_bool(&self) -> bool {
613637
if let Int(I8, _) = self.value {
@@ -636,7 +660,7 @@ impl Scalar {
636660
}
637661

638662
/// Describes how the fields of a type are located in memory.
639-
#[derive(PartialEq, Eq, Hash, Debug)]
663+
#[derive(PartialEq, Eq, Hash, Debug, HashStable_Generic)]
640664
pub enum FieldPlacement {
641665
/// All fields start at no offset. The `usize` is the field count.
642666
///
@@ -752,7 +776,7 @@ impl FieldPlacement {
752776

753777
/// Describes how values of the type are passed by target ABIs,
754778
/// in terms of categories of C types there are ABI rules for.
755-
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
779+
#[derive(Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
756780
pub enum Abi {
757781
Uninhabited,
758782
Scalar(Scalar),
@@ -803,7 +827,13 @@ rustc_index::newtype_index! {
803827
pub struct VariantIdx { .. }
804828
}
805829

806-
#[derive(PartialEq, Eq, Hash, Debug)]
830+
impl<CTX> HashStable<CTX> for VariantIdx {
831+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
832+
self.as_u32().hash_stable(hcx, hasher)
833+
}
834+
}
835+
836+
#[derive(PartialEq, Eq, Hash, Debug, HashStable_Generic)]
807837
pub enum Variants {
808838
/// Single enum variants, structs/tuples, unions, and all non-ADTs.
809839
Single {
@@ -842,7 +872,28 @@ pub enum DiscriminantKind {
842872
},
843873
}
844874

845-
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
875+
impl<CTX> HashStable<CTX> for DiscriminantKind {
876+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
877+
use DiscriminantKind::*;
878+
std::mem::discriminant(self).hash_stable(hcx, hasher);
879+
880+
match *self {
881+
Tag => {}
882+
Niche {
883+
dataful_variant,
884+
ref niche_variants,
885+
niche_start,
886+
} => {
887+
dataful_variant.hash_stable(hcx, hasher);
888+
niche_variants.start().hash_stable(hcx, hasher);
889+
niche_variants.end().hash_stable(hcx, hasher);
890+
niche_start.hash_stable(hcx, hasher);
891+
}
892+
}
893+
}
894+
}
895+
896+
#[derive(Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
846897
pub struct Niche {
847898
pub offset: Size,
848899
pub scalar: Scalar,
@@ -906,7 +957,7 @@ impl Niche {
906957
}
907958
}
908959

909-
#[derive(PartialEq, Eq, Hash, Debug)]
960+
#[derive(PartialEq, Eq, Hash, Debug, HashStable_Generic)]
910961
pub struct LayoutDetails {
911962
pub variants: Variants,
912963
pub fields: FieldPlacement,

0 commit comments

Comments
 (0)