Skip to content

Commit 8b4fa0f

Browse files
Use derivative for Hash
1 parent 8eb932d commit 8b4fa0f

File tree

5 files changed

+11
-168
lines changed

5 files changed

+11
-168
lines changed

Diff for: compiler/rustc_type_ir/src/canonical.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::fmt;
2-
use std::hash;
2+
use std::hash::Hash;
33
use std::ops::ControlFlow;
44

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

64-
impl<I: Interner, V: hash::Hash> hash::Hash for Canonical<I, V> {
65-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
66-
self.value.hash(state);
67-
self.max_universe.hash(state);
68-
self.variables.hash(state);
69-
}
70-
}
71-
7264
impl<CTX: HashStableContext, I: Interner, V: HashStable<CTX>> HashStable<CTX> for Canonical<I, V>
7365
where
7466
I::CanonicalVars: HashStable<CTX>,

Diff for: compiler/rustc_type_ir/src/const_kind.rs

+2-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use rustc_data_structures::stable_hasher::HashStable;
22
use rustc_data_structures::stable_hasher::StableHasher;
33
use rustc_serialize::{Decodable, Decoder, Encodable};
44
use std::fmt;
5-
use std::hash;
65

76
use crate::{
87
DebruijnIndex, DebugWithInfcx, HashStableContext, InferCtxtLike, Interner, TyDecoder,
@@ -18,7 +17,8 @@ use self::ConstKind::*;
1817
PartialOrd(bound = ""),
1918
PartialOrd = "feature_allow_slow_enum",
2019
Ord(bound = ""),
21-
Ord = "feature_allow_slow_enum"
20+
Ord = "feature_allow_slow_enum",
21+
Hash(bound = "")
2222
)]
2323
pub enum ConstKind<I: Interner> {
2424
/// A const generic parameter.
@@ -63,25 +63,6 @@ const fn const_kind_discriminant<I: Interner>(value: &ConstKind<I>) -> usize {
6363
}
6464
}
6565

66-
impl<I: Interner> hash::Hash for ConstKind<I> {
67-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
68-
const_kind_discriminant(self).hash(state);
69-
match self {
70-
Param(p) => p.hash(state),
71-
Infer(i) => i.hash(state),
72-
Bound(d, b) => {
73-
d.hash(state);
74-
b.hash(state);
75-
}
76-
Placeholder(p) => p.hash(state),
77-
Unevaluated(u) => u.hash(state),
78-
Value(v) => v.hash(state),
79-
Error(e) => e.hash(state),
80-
Expr(e) => e.hash(state),
81-
}
82-
}
83-
}
84-
8566
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for ConstKind<I>
8667
where
8768
I::ParamConst: HashStable<CTX>,

Diff for: compiler/rustc_type_ir/src/predicate_kind.rs

+2-48
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
22
use rustc_serialize::Decoder;
33
use rustc_serialize::{Decodable, Encodable};
44
use std::fmt;
5-
use std::hash;
65
use std::ops::ControlFlow;
76

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

85-
impl<I: Interner> hash::Hash for ClauseKind<I> {
86-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
87-
clause_kind_discriminant(self).hash(state);
88-
match self {
89-
ClauseKind::Trait(p) => p.hash(state),
90-
ClauseKind::RegionOutlives(p) => p.hash(state),
91-
ClauseKind::TypeOutlives(p) => p.hash(state),
92-
ClauseKind::Projection(p) => p.hash(state),
93-
ClauseKind::ConstArgHasType(c, t) => {
94-
c.hash(state);
95-
t.hash(state);
96-
}
97-
ClauseKind::WellFormed(t) => t.hash(state),
98-
ClauseKind::ConstEvaluatable(c) => c.hash(state),
99-
}
100-
}
101-
}
102-
10384
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for ClauseKind<I>
10485
where
10586
I::Ty: HashStable<CTX>,
@@ -238,7 +219,7 @@ where
238219
}
239220

240221
#[derive(derivative::Derivative)]
241-
#[derivative(Clone(bound = ""))]
222+
#[derivative(Clone(bound = ""), Hash(bound = ""))]
242223
pub enum PredicateKind<I: Interner> {
243224
/// Prove a clause
244225
Clause(ClauseKind<I>),
@@ -329,33 +310,6 @@ fn predicate_kind_discriminant<I: Interner>(value: &PredicateKind<I>) -> usize {
329310
}
330311
}
331312

332-
impl<I: Interner> hash::Hash for PredicateKind<I> {
333-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
334-
predicate_kind_discriminant(self).hash(state);
335-
match self {
336-
PredicateKind::Clause(p) => p.hash(state),
337-
PredicateKind::ObjectSafe(d) => d.hash(state),
338-
PredicateKind::ClosureKind(d, g, k) => {
339-
d.hash(state);
340-
g.hash(state);
341-
k.hash(state);
342-
}
343-
PredicateKind::Subtype(p) => p.hash(state),
344-
PredicateKind::Coerce(p) => p.hash(state),
345-
PredicateKind::ConstEquate(c1, c2) => {
346-
c1.hash(state);
347-
c2.hash(state);
348-
}
349-
PredicateKind::Ambiguous => {}
350-
PredicateKind::AliasRelate(t1, t2, r) => {
351-
t1.hash(state);
352-
t2.hash(state);
353-
r.hash(state);
354-
}
355-
}
356-
}
357-
}
358-
359313
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for PredicateKind<I>
360314
where
361315
I::DefId: HashStable<CTX>,

Diff for: compiler/rustc_type_ir/src/region_kind.rs

+2-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use rustc_data_structures::stable_hasher::HashStable;
22
use rustc_data_structures::stable_hasher::StableHasher;
33
use rustc_serialize::{Decodable, Decoder, Encodable};
44
use std::fmt;
5-
use std::hash;
65

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

216-
// This is manually implemented because a derive would require `I: Hash`
217-
impl<I: Interner> hash::Hash for RegionKind<I> {
218-
fn hash<H: hash::Hasher>(&self, state: &mut H) -> () {
219-
regionkind_discriminant(self).hash(state);
220-
match self {
221-
ReEarlyBound(r) => r.hash(state),
222-
ReLateBound(d, r) => {
223-
d.hash(state);
224-
r.hash(state)
225-
}
226-
ReFree(r) => r.hash(state),
227-
ReStatic => (),
228-
ReVar(r) => r.hash(state),
229-
RePlaceholder(r) => r.hash(state),
230-
ReErased => (),
231-
ReError(_) => (),
232-
}
233-
}
234-
}
235-
236216
impl<I: Interner> DebugWithInfcx<I> for RegionKind<I> {
237217
fn fmt<Infcx: InferCtxtLike<Interner = I>>(
238218
this: WithInfcx<'_, Infcx, &Self>,

Diff for: compiler/rustc_type_ir/src/ty_kind.rs

+3-67
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
44
use rustc_data_structures::unify::{EqUnifyValue, UnifyKey};
55
use rustc_serialize::{Decodable, Decoder, Encodable};
6+
use std::fmt;
67
use std::mem::discriminant;
7-
use std::{fmt, hash};
88

99
use crate::HashStableContext;
1010
use crate::Interner;
@@ -119,7 +119,8 @@ pub enum AliasKind {
119119
PartialOrd(bound = ""),
120120
PartialOrd = "feature_allow_slow_enum",
121121
Ord(bound = ""),
122-
Ord = "feature_allow_slow_enum"
122+
Ord = "feature_allow_slow_enum",
123+
Hash(bound = "")
123124
)]
124125
pub enum TyKind<I: Interner> {
125126
/// The primitive boolean type. Written as `bool`.
@@ -383,71 +384,6 @@ impl<I: Interner> PartialEq for TyKind<I> {
383384
// This is manually implemented because a derive would require `I: Eq`
384385
impl<I: Interner> Eq for TyKind<I> {}
385386

386-
// This is manually implemented because a derive would require `I: Hash`
387-
impl<I: Interner> hash::Hash for TyKind<I> {
388-
fn hash<__H: hash::Hasher>(&self, state: &mut __H) -> () {
389-
tykind_discriminant(self).hash(state);
390-
match self {
391-
Int(i) => i.hash(state),
392-
Uint(u) => u.hash(state),
393-
Float(f) => f.hash(state),
394-
Adt(d, s) => {
395-
d.hash(state);
396-
s.hash(state)
397-
}
398-
Foreign(d) => d.hash(state),
399-
Array(t, c) => {
400-
t.hash(state);
401-
c.hash(state)
402-
}
403-
Slice(t) => t.hash(state),
404-
RawPtr(t) => t.hash(state),
405-
Ref(r, t, m) => {
406-
r.hash(state);
407-
t.hash(state);
408-
m.hash(state)
409-
}
410-
FnDef(d, s) => {
411-
d.hash(state);
412-
s.hash(state)
413-
}
414-
FnPtr(s) => s.hash(state),
415-
Dynamic(p, r, repr) => {
416-
p.hash(state);
417-
r.hash(state);
418-
repr.hash(state)
419-
}
420-
Closure(d, s) => {
421-
d.hash(state);
422-
s.hash(state)
423-
}
424-
Coroutine(d, s, m) => {
425-
d.hash(state);
426-
s.hash(state);
427-
m.hash(state)
428-
}
429-
CoroutineWitness(d, s) => {
430-
d.hash(state);
431-
s.hash(state);
432-
}
433-
Tuple(t) => t.hash(state),
434-
Alias(i, p) => {
435-
i.hash(state);
436-
p.hash(state);
437-
}
438-
Param(p) => p.hash(state),
439-
Bound(d, b) => {
440-
d.hash(state);
441-
b.hash(state)
442-
}
443-
Placeholder(p) => p.hash(state),
444-
Infer(t) => t.hash(state),
445-
Error(e) => e.hash(state),
446-
Bool | Char | Str | Never => (),
447-
}
448-
}
449-
}
450-
451387
impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
452388
fn fmt<Infcx: InferCtxtLike<Interner = I>>(
453389
this: WithInfcx<'_, Infcx, &Self>,

0 commit comments

Comments
 (0)