Skip to content

Commit

Permalink
Use derivative for Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Oct 31, 2023
1 parent 8eb932d commit 8b4fa0f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 168 deletions.
12 changes: 2 additions & 10 deletions compiler/rustc_type_ir/src/canonical.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt;
use std::hash;
use std::hash::Hash;
use std::ops::ControlFlow;

use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
Expand All @@ -14,7 +14,7 @@ use crate::{HashStableContext, Interner, TyEncoder, UniverseIndex};
/// variables have been rewritten to "canonical vars". These are
/// numbered starting from 0 in order of first appearance.
#[derive(derivative::Derivative)]
#[derivative(Clone(bound = "V: Clone"))]
#[derivative(Clone(bound = "V: Clone"), Hash(bound = "V: Hash"))]
pub struct Canonical<I: Interner, V> {
pub value: V,
pub max_universe: UniverseIndex,
Expand Down Expand Up @@ -61,14 +61,6 @@ impl<I: Interner, V> Canonical<I, V> {
}
}

impl<I: Interner, V: hash::Hash> hash::Hash for Canonical<I, V> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.value.hash(state);
self.max_universe.hash(state);
self.variables.hash(state);
}
}

impl<CTX: HashStableContext, I: Interner, V: HashStable<CTX>> HashStable<CTX> for Canonical<I, V>
where
I::CanonicalVars: HashStable<CTX>,
Expand Down
23 changes: 2 additions & 21 deletions compiler/rustc_type_ir/src/const_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use rustc_data_structures::stable_hasher::HashStable;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_serialize::{Decodable, Decoder, Encodable};
use std::fmt;
use std::hash;

use crate::{
DebruijnIndex, DebugWithInfcx, HashStableContext, InferCtxtLike, Interner, TyDecoder,
Expand All @@ -18,7 +17,8 @@ use self::ConstKind::*;
PartialOrd(bound = ""),
PartialOrd = "feature_allow_slow_enum",
Ord(bound = ""),
Ord = "feature_allow_slow_enum"
Ord = "feature_allow_slow_enum",
Hash(bound = "")
)]
pub enum ConstKind<I: Interner> {
/// A const generic parameter.
Expand Down Expand Up @@ -63,25 +63,6 @@ const fn const_kind_discriminant<I: Interner>(value: &ConstKind<I>) -> usize {
}
}

impl<I: Interner> hash::Hash for ConstKind<I> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
const_kind_discriminant(self).hash(state);
match self {
Param(p) => p.hash(state),
Infer(i) => i.hash(state),
Bound(d, b) => {
d.hash(state);
b.hash(state);
}
Placeholder(p) => p.hash(state),
Unevaluated(u) => u.hash(state),
Value(v) => v.hash(state),
Error(e) => e.hash(state),
Expr(e) => e.hash(state),
}
}
}

impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for ConstKind<I>
where
I::ParamConst: HashStable<CTX>,
Expand Down
50 changes: 2 additions & 48 deletions compiler/rustc_type_ir/src/predicate_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_serialize::Decoder;
use rustc_serialize::{Decodable, Encodable};
use std::fmt;
use std::hash;
use std::ops::ControlFlow;

use crate::fold::{FallibleTypeFolder, TypeFoldable};
Expand All @@ -13,7 +12,7 @@ use crate::{TyDecoder, TyEncoder};
/// A clause is something that can appear in where bounds or be inferred
/// by implied bounds.
#[derive(derivative::Derivative)]
#[derivative(Clone(bound = ""))]
#[derivative(Clone(bound = ""), Hash(bound = ""))]
pub enum ClauseKind<I: Interner> {
/// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be
/// the `Self` type of the trait reference and `A`, `B`, and `C`
Expand Down Expand Up @@ -82,24 +81,6 @@ fn clause_kind_discriminant<I: Interner>(value: &ClauseKind<I>) -> usize {
}
}

impl<I: Interner> hash::Hash for ClauseKind<I> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
clause_kind_discriminant(self).hash(state);
match self {
ClauseKind::Trait(p) => p.hash(state),
ClauseKind::RegionOutlives(p) => p.hash(state),
ClauseKind::TypeOutlives(p) => p.hash(state),
ClauseKind::Projection(p) => p.hash(state),
ClauseKind::ConstArgHasType(c, t) => {
c.hash(state);
t.hash(state);
}
ClauseKind::WellFormed(t) => t.hash(state),
ClauseKind::ConstEvaluatable(c) => c.hash(state),
}
}
}

impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for ClauseKind<I>
where
I::Ty: HashStable<CTX>,
Expand Down Expand Up @@ -238,7 +219,7 @@ where
}

#[derive(derivative::Derivative)]
#[derivative(Clone(bound = ""))]
#[derivative(Clone(bound = ""), Hash(bound = ""))]
pub enum PredicateKind<I: Interner> {
/// Prove a clause
Clause(ClauseKind<I>),
Expand Down Expand Up @@ -329,33 +310,6 @@ fn predicate_kind_discriminant<I: Interner>(value: &PredicateKind<I>) -> usize {
}
}

impl<I: Interner> hash::Hash for PredicateKind<I> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
predicate_kind_discriminant(self).hash(state);
match self {
PredicateKind::Clause(p) => p.hash(state),
PredicateKind::ObjectSafe(d) => d.hash(state),
PredicateKind::ClosureKind(d, g, k) => {
d.hash(state);
g.hash(state);
k.hash(state);
}
PredicateKind::Subtype(p) => p.hash(state),
PredicateKind::Coerce(p) => p.hash(state),
PredicateKind::ConstEquate(c1, c2) => {
c1.hash(state);
c2.hash(state);
}
PredicateKind::Ambiguous => {}
PredicateKind::AliasRelate(t1, t2, r) => {
t1.hash(state);
t2.hash(state);
r.hash(state);
}
}
}
}

impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for PredicateKind<I>
where
I::DefId: HashStable<CTX>,
Expand Down
24 changes: 2 additions & 22 deletions compiler/rustc_type_ir/src/region_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use rustc_data_structures::stable_hasher::HashStable;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_serialize::{Decodable, Decoder, Encodable};
use std::fmt;
use std::hash;

use crate::{
DebruijnIndex, DebugWithInfcx, HashStableContext, InferCtxtLike, Interner, TyDecoder,
Expand Down Expand Up @@ -123,7 +122,8 @@ use self::RegionKind::*;
PartialOrd(bound = ""),
PartialOrd = "feature_allow_slow_enum",
Ord(bound = ""),
Ord = "feature_allow_slow_enum"
Ord = "feature_allow_slow_enum",
Hash(bound = "")
)]
pub enum RegionKind<I: Interner> {
/// Region bound in a type or fn declaration which will be
Expand Down Expand Up @@ -213,26 +213,6 @@ impl<I: Interner> PartialEq for RegionKind<I> {
// This is manually implemented because a derive would require `I: Eq`
impl<I: Interner> Eq for RegionKind<I> {}

// This is manually implemented because a derive would require `I: Hash`
impl<I: Interner> hash::Hash for RegionKind<I> {
fn hash<H: hash::Hasher>(&self, state: &mut H) -> () {
regionkind_discriminant(self).hash(state);
match self {
ReEarlyBound(r) => r.hash(state),
ReLateBound(d, r) => {
d.hash(state);
r.hash(state)
}
ReFree(r) => r.hash(state),
ReStatic => (),
ReVar(r) => r.hash(state),
RePlaceholder(r) => r.hash(state),
ReErased => (),
ReError(_) => (),
}
}
}

impl<I: Interner> DebugWithInfcx<I> for RegionKind<I> {
fn fmt<Infcx: InferCtxtLike<Interner = I>>(
this: WithInfcx<'_, Infcx, &Self>,
Expand Down
70 changes: 3 additions & 67 deletions compiler/rustc_type_ir/src/ty_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::unify::{EqUnifyValue, UnifyKey};
use rustc_serialize::{Decodable, Decoder, Encodable};
use std::fmt;
use std::mem::discriminant;
use std::{fmt, hash};

use crate::HashStableContext;
use crate::Interner;
Expand Down Expand Up @@ -119,7 +119,8 @@ pub enum AliasKind {
PartialOrd(bound = ""),
PartialOrd = "feature_allow_slow_enum",
Ord(bound = ""),
Ord = "feature_allow_slow_enum"
Ord = "feature_allow_slow_enum",
Hash(bound = "")
)]
pub enum TyKind<I: Interner> {
/// The primitive boolean type. Written as `bool`.
Expand Down Expand Up @@ -383,71 +384,6 @@ impl<I: Interner> PartialEq for TyKind<I> {
// This is manually implemented because a derive would require `I: Eq`
impl<I: Interner> Eq for TyKind<I> {}

// This is manually implemented because a derive would require `I: Hash`
impl<I: Interner> hash::Hash for TyKind<I> {
fn hash<__H: hash::Hasher>(&self, state: &mut __H) -> () {
tykind_discriminant(self).hash(state);
match self {
Int(i) => i.hash(state),
Uint(u) => u.hash(state),
Float(f) => f.hash(state),
Adt(d, s) => {
d.hash(state);
s.hash(state)
}
Foreign(d) => d.hash(state),
Array(t, c) => {
t.hash(state);
c.hash(state)
}
Slice(t) => t.hash(state),
RawPtr(t) => t.hash(state),
Ref(r, t, m) => {
r.hash(state);
t.hash(state);
m.hash(state)
}
FnDef(d, s) => {
d.hash(state);
s.hash(state)
}
FnPtr(s) => s.hash(state),
Dynamic(p, r, repr) => {
p.hash(state);
r.hash(state);
repr.hash(state)
}
Closure(d, s) => {
d.hash(state);
s.hash(state)
}
Coroutine(d, s, m) => {
d.hash(state);
s.hash(state);
m.hash(state)
}
CoroutineWitness(d, s) => {
d.hash(state);
s.hash(state);
}
Tuple(t) => t.hash(state),
Alias(i, p) => {
i.hash(state);
p.hash(state);
}
Param(p) => p.hash(state),
Bound(d, b) => {
d.hash(state);
b.hash(state)
}
Placeholder(p) => p.hash(state),
Infer(t) => t.hash(state),
Error(e) => e.hash(state),
Bool | Char | Str | Never => (),
}
}
}

impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
fn fmt<Infcx: InferCtxtLike<Interner = I>>(
this: WithInfcx<'_, Infcx, &Self>,
Expand Down

0 comments on commit 8b4fa0f

Please sign in to comment.