Skip to content

Commit 4843835

Browse files
Use derivative for PartialEq/Eq
1 parent 50be229 commit 4843835

File tree

5 files changed

+17
-152
lines changed

5 files changed

+17
-152
lines changed

compiler/rustc_type_ir/src/canonical.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ 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"), Hash(bound = "V: Hash"))]
17+
#[derivative(
18+
Clone(bound = "V: Clone"),
19+
Hash(bound = "V: Hash"),
20+
PartialEq(bound = "V: PartialEq"),
21+
Eq(bound = "V: Eq")
22+
)]
1823
pub struct Canonical<I: Interner, V> {
1924
pub value: V,
2025
pub max_universe: UniverseIndex,
@@ -72,16 +77,6 @@ where
7277
}
7378
}
7479

75-
impl<I: Interner, V: Eq> Eq for Canonical<I, V> {}
76-
77-
impl<I: Interner, V: PartialEq> PartialEq for Canonical<I, V> {
78-
fn eq(&self, other: &Self) -> bool {
79-
self.value == other.value
80-
&& self.max_universe == other.max_universe
81-
&& self.variables == other.variables
82-
}
83-
}
84-
8580
impl<I: Interner, V: fmt::Display> fmt::Display for Canonical<I, V> {
8681
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8782
write!(

compiler/rustc_type_ir/src/const_kind.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use self::ConstKind::*;
1818
PartialOrd = "feature_allow_slow_enum",
1919
Ord(bound = ""),
2020
Ord = "feature_allow_slow_enum",
21-
Hash(bound = "")
21+
Hash(bound = ""),
22+
PartialEq(bound = ""),
23+
Eq(bound = "")
2224
)]
2325
pub enum ConstKind<I: Interner> {
2426
/// A const generic parameter.
@@ -153,24 +155,6 @@ where
153155
}
154156
}
155157

156-
impl<I: Interner> PartialEq for ConstKind<I> {
157-
fn eq(&self, other: &Self) -> bool {
158-
match (self, other) {
159-
(Param(l0), Param(r0)) => l0 == r0,
160-
(Infer(l0), Infer(r0)) => l0 == r0,
161-
(Bound(l0, l1), Bound(r0, r1)) => l0 == r0 && l1 == r1,
162-
(Placeholder(l0), Placeholder(r0)) => l0 == r0,
163-
(Unevaluated(l0), Unevaluated(r0)) => l0 == r0,
164-
(Value(l0), Value(r0)) => l0 == r0,
165-
(Error(l0), Error(r0)) => l0 == r0,
166-
(Expr(l0), Expr(r0)) => l0 == r0,
167-
_ => false,
168-
}
169-
}
170-
}
171-
172-
impl<I: Interner> Eq for ConstKind<I> {}
173-
174158
impl<I: Interner> fmt::Debug for ConstKind<I> {
175159
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
176160
WithInfcx::with_no_infcx(self).fmt(f)

compiler/rustc_type_ir/src/predicate_kind.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{TyDecoder, TyEncoder};
1212
/// A clause is something that can appear in where bounds or be inferred
1313
/// by implied bounds.
1414
#[derive(derivative::Derivative)]
15-
#[derivative(Clone(bound = ""), Hash(bound = ""))]
15+
#[derivative(Clone(bound = ""), Hash(bound = ""), PartialEq(bound = ""), Eq(bound = ""))]
1616
pub enum ClauseKind<I: Interner> {
1717
/// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be
1818
/// the `Self` type of the trait reference and `A`, `B`, and `C`
@@ -52,23 +52,6 @@ where
5252
{
5353
}
5454

55-
impl<I: Interner> PartialEq for ClauseKind<I> {
56-
fn eq(&self, other: &Self) -> bool {
57-
match (self, other) {
58-
(Self::Trait(l0), Self::Trait(r0)) => l0 == r0,
59-
(Self::RegionOutlives(l0), Self::RegionOutlives(r0)) => l0 == r0,
60-
(Self::TypeOutlives(l0), Self::TypeOutlives(r0)) => l0 == r0,
61-
(Self::Projection(l0), Self::Projection(r0)) => l0 == r0,
62-
(Self::ConstArgHasType(l0, l1), Self::ConstArgHasType(r0, r1)) => l0 == r0 && l1 == r1,
63-
(Self::WellFormed(l0), Self::WellFormed(r0)) => l0 == r0,
64-
(Self::ConstEvaluatable(l0), Self::ConstEvaluatable(r0)) => l0 == r0,
65-
_ => false,
66-
}
67-
}
68-
}
69-
70-
impl<I: Interner> Eq for ClauseKind<I> {}
71-
7255
fn clause_kind_discriminant<I: Interner>(value: &ClauseKind<I>) -> usize {
7356
match value {
7457
ClauseKind::Trait(_) => 0,
@@ -219,7 +202,7 @@ where
219202
}
220203

221204
#[derive(derivative::Derivative)]
222-
#[derivative(Clone(bound = ""), Hash(bound = ""))]
205+
#[derivative(Clone(bound = ""), Hash(bound = ""), PartialEq(bound = ""), Eq(bound = ""))]
223206
pub enum PredicateKind<I: Interner> {
224207
/// Prove a clause
225208
Clause(ClauseKind<I>),
@@ -276,27 +259,6 @@ where
276259
{
277260
}
278261

279-
impl<I: Interner> PartialEq for PredicateKind<I> {
280-
fn eq(&self, other: &Self) -> bool {
281-
match (self, other) {
282-
(Self::Clause(l0), Self::Clause(r0)) => l0 == r0,
283-
(Self::ObjectSafe(l0), Self::ObjectSafe(r0)) => l0 == r0,
284-
(Self::ClosureKind(l0, l1, l2), Self::ClosureKind(r0, r1, r2)) => {
285-
l0 == r0 && l1 == r1 && l2 == r2
286-
}
287-
(Self::Subtype(l0), Self::Subtype(r0)) => l0 == r0,
288-
(Self::Coerce(l0), Self::Coerce(r0)) => l0 == r0,
289-
(Self::ConstEquate(l0, l1), Self::ConstEquate(r0, r1)) => l0 == r0 && l1 == r1,
290-
(Self::AliasRelate(l0, l1, l2), Self::AliasRelate(r0, r1, r2)) => {
291-
l0 == r0 && l1 == r1 && l2 == r2
292-
}
293-
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
294-
}
295-
}
296-
}
297-
298-
impl<I: Interner> Eq for PredicateKind<I> {}
299-
300262
fn predicate_kind_discriminant<I: Interner>(value: &PredicateKind<I>) -> usize {
301263
match value {
302264
PredicateKind::Clause(_) => 0,

compiler/rustc_type_ir/src/region_kind.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ use self::RegionKind::*;
123123
PartialOrd = "feature_allow_slow_enum",
124124
Ord(bound = ""),
125125
Ord = "feature_allow_slow_enum",
126-
Hash(bound = "")
126+
Hash(bound = ""),
127+
PartialEq(bound = ""),
128+
Eq(bound = "")
127129
)]
128130
pub enum RegionKind<I: Interner> {
129131
/// Region bound in a type or fn declaration which will be
@@ -185,34 +187,6 @@ where
185187
{
186188
}
187189

188-
// This is manually implemented because a derive would require `I: PartialEq`
189-
impl<I: Interner> PartialEq for RegionKind<I> {
190-
#[inline]
191-
fn eq(&self, other: &RegionKind<I>) -> bool {
192-
regionkind_discriminant(self) == regionkind_discriminant(other)
193-
&& match (self, other) {
194-
(ReEarlyBound(a_r), ReEarlyBound(b_r)) => a_r == b_r,
195-
(ReLateBound(a_d, a_r), ReLateBound(b_d, b_r)) => a_d == b_d && a_r == b_r,
196-
(ReFree(a_r), ReFree(b_r)) => a_r == b_r,
197-
(ReStatic, ReStatic) => true,
198-
(ReVar(a_r), ReVar(b_r)) => a_r == b_r,
199-
(RePlaceholder(a_r), RePlaceholder(b_r)) => a_r == b_r,
200-
(ReErased, ReErased) => true,
201-
(ReError(_), ReError(_)) => true,
202-
_ => {
203-
debug_assert!(
204-
false,
205-
"This branch must be unreachable, maybe the match is missing an arm? self = {self:?}, other = {other:?}"
206-
);
207-
true
208-
}
209-
}
210-
}
211-
}
212-
213-
// This is manually implemented because a derive would require `I: Eq`
214-
impl<I: Interner> Eq for RegionKind<I> {}
215-
216190
impl<I: Interner> DebugWithInfcx<I> for RegionKind<I> {
217191
fn fmt<Infcx: InferCtxtLike<Interner = I>>(
218192
this: WithInfcx<'_, Infcx, &Self>,

compiler/rustc_type_ir/src/ty_kind.rs

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ pub enum AliasKind {
120120
PartialOrd = "feature_allow_slow_enum",
121121
Ord(bound = ""),
122122
Ord = "feature_allow_slow_enum",
123-
Hash(bound = "")
123+
Hash(bound = ""),
124+
PartialEq(bound = ""),
125+
Eq(bound = "")
124126
)]
125127
pub enum TyKind<I: Interner> {
126128
/// The primitive boolean type. Written as `bool`.
@@ -332,58 +334,6 @@ const fn tykind_discriminant<I: Interner>(value: &TyKind<I>) -> usize {
332334
}
333335
}
334336

335-
// This is manually implemented because a derive would require `I: PartialEq`
336-
impl<I: Interner> PartialEq for TyKind<I> {
337-
#[inline]
338-
fn eq(&self, other: &TyKind<I>) -> bool {
339-
// You might expect this `match` to be preceded with this:
340-
//
341-
// tykind_discriminant(self) == tykind_discriminant(other) &&
342-
//
343-
// but the data patterns in practice are such that a comparison
344-
// succeeds 99%+ of the time, and it's faster to omit it.
345-
match (self, other) {
346-
(Int(a_i), Int(b_i)) => a_i == b_i,
347-
(Uint(a_u), Uint(b_u)) => a_u == b_u,
348-
(Float(a_f), Float(b_f)) => a_f == b_f,
349-
(Adt(a_d, a_s), Adt(b_d, b_s)) => a_d == b_d && a_s == b_s,
350-
(Foreign(a_d), Foreign(b_d)) => a_d == b_d,
351-
(Array(a_t, a_c), Array(b_t, b_c)) => a_t == b_t && a_c == b_c,
352-
(Slice(a_t), Slice(b_t)) => a_t == b_t,
353-
(RawPtr(a_t), RawPtr(b_t)) => a_t == b_t,
354-
(Ref(a_r, a_t, a_m), Ref(b_r, b_t, b_m)) => a_r == b_r && a_t == b_t && a_m == b_m,
355-
(FnDef(a_d, a_s), FnDef(b_d, b_s)) => a_d == b_d && a_s == b_s,
356-
(FnPtr(a_s), FnPtr(b_s)) => a_s == b_s,
357-
(Dynamic(a_p, a_r, a_repr), Dynamic(b_p, b_r, b_repr)) => {
358-
a_p == b_p && a_r == b_r && a_repr == b_repr
359-
}
360-
(Closure(a_d, a_s), Closure(b_d, b_s)) => a_d == b_d && a_s == b_s,
361-
(Coroutine(a_d, a_s, a_m), Coroutine(b_d, b_s, b_m)) => {
362-
a_d == b_d && a_s == b_s && a_m == b_m
363-
}
364-
(CoroutineWitness(a_d, a_s), CoroutineWitness(b_d, b_s)) => a_d == b_d && a_s == b_s,
365-
(Tuple(a_t), Tuple(b_t)) => a_t == b_t,
366-
(Alias(a_i, a_p), Alias(b_i, b_p)) => a_i == b_i && a_p == b_p,
367-
(Param(a_p), Param(b_p)) => a_p == b_p,
368-
(Bound(a_d, a_b), Bound(b_d, b_b)) => a_d == b_d && a_b == b_b,
369-
(Placeholder(a_p), Placeholder(b_p)) => a_p == b_p,
370-
(Infer(a_t), Infer(b_t)) => a_t == b_t,
371-
(Error(a_e), Error(b_e)) => a_e == b_e,
372-
(Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => true,
373-
_ => {
374-
debug_assert!(
375-
tykind_discriminant(self) != tykind_discriminant(other),
376-
"This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}"
377-
);
378-
false
379-
}
380-
}
381-
}
382-
}
383-
384-
// This is manually implemented because a derive would require `I: Eq`
385-
impl<I: Interner> Eq for TyKind<I> {}
386-
387337
impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
388338
fn fmt<Infcx: InferCtxtLike<Interner = I>>(
389339
this: WithInfcx<'_, Infcx, &Self>,

0 commit comments

Comments
 (0)