Skip to content

Commit 69b6829

Browse files
committed
Auto merge of rust-lang#117408 - compiler-errors:derive-eq, r=<try>
Use derivative for `PartialEq`/`Eq` in `rustc_type_ir` r? `@ghost` splitting this out for perf...
2 parents 6d1fc53 + d746ded commit 69b6829

File tree

8 files changed

+27
-147
lines changed

8 files changed

+27
-147
lines changed

Cargo.lock

+12
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,17 @@ dependencies = [
10111011
"syn 2.0.29",
10121012
]
10131013

1014+
[[package]]
1015+
name = "derivative"
1016+
version = "2.2.0"
1017+
source = "registry+https://github.com/rust-lang/crates.io-index"
1018+
checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b"
1019+
dependencies = [
1020+
"proc-macro2",
1021+
"quote",
1022+
"syn 1.0.109",
1023+
]
1024+
10141025
[[package]]
10151026
name = "derive_builder"
10161027
version = "0.12.0"
@@ -4668,6 +4679,7 @@ name = "rustc_type_ir"
46684679
version = "0.0.0"
46694680
dependencies = [
46704681
"bitflags 1.3.2",
4682+
"derivative",
46714683
"rustc_data_structures",
46724684
"rustc_index",
46734685
"rustc_macros",

compiler/rustc_type_ir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ rustc_serialize = { path = "../rustc_serialize" }
1212
rustc_data_structures = { path = "../rustc_data_structures" }
1313
rustc_macros = { path = "../rustc_macros" }
1414
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
15+
derivative = "2.2.0"

compiler/rustc_type_ir/src/canonical.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use crate::{HashStableContext, Interner, TyEncoder, UniverseIndex};
1313
/// A "canonicalized" type `V` is one where all free inference
1414
/// variables have been rewritten to "canonical vars". These are
1515
/// numbered starting from 0 in order of first appearance.
16+
#[derive(derivative::Derivative)]
17+
#[derivative(PartialEq(bound = "V: PartialEq"), Eq(bound = "V: Eq"))]
1618
pub struct Canonical<I: Interner, V> {
1719
pub value: V,
1820
pub max_universe: UniverseIndex,
@@ -78,16 +80,6 @@ where
7880
}
7981
}
8082

81-
impl<I: Interner, V: Eq> Eq for Canonical<I, V> {}
82-
83-
impl<I: Interner, V: PartialEq> PartialEq for Canonical<I, V> {
84-
fn eq(&self, other: &Self) -> bool {
85-
self.value == other.value
86-
&& self.max_universe == other.max_universe
87-
&& self.variables == other.variables
88-
}
89-
}
90-
9183
impl<I: Interner, V: fmt::Display> fmt::Display for Canonical<I, V> {
9284
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9385
write!(

compiler/rustc_type_ir/src/const_kind.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use crate::{
1313
use self::ConstKind::*;
1414

1515
/// Represents a constant in Rust.
16-
// #[derive(derive_more::From)]
16+
#[derive(derivative::Derivative)]
17+
#[derivative(PartialEq(bound = ""), Eq(bound = ""))]
1718
pub enum ConstKind<I: Interner> {
1819
/// A const generic parameter.
1920
Param(I::ParamConst),
@@ -193,24 +194,6 @@ impl<I: Interner> Ord for ConstKind<I> {
193194
}
194195
}
195196

196-
impl<I: Interner> PartialEq for ConstKind<I> {
197-
fn eq(&self, other: &Self) -> bool {
198-
match (self, other) {
199-
(Param(l0), Param(r0)) => l0 == r0,
200-
(Infer(l0), Infer(r0)) => l0 == r0,
201-
(Bound(l0, l1), Bound(r0, r1)) => l0 == r0 && l1 == r1,
202-
(Placeholder(l0), Placeholder(r0)) => l0 == r0,
203-
(Unevaluated(l0), Unevaluated(r0)) => l0 == r0,
204-
(Value(l0), Value(r0)) => l0 == r0,
205-
(Error(l0), Error(r0)) => l0 == r0,
206-
(Expr(l0), Expr(r0)) => l0 == r0,
207-
_ => false,
208-
}
209-
}
210-
}
211-
212-
impl<I: Interner> Eq for ConstKind<I> {}
213-
214197
impl<I: Interner> Clone for ConstKind<I> {
215198
fn clone(&self) -> Self {
216199
match self {

compiler/rustc_type_ir/src/predicate_kind.rs

+4-38
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use crate::{TyDecoder, TyEncoder};
1212

1313
/// A clause is something that can appear in where bounds or be inferred
1414
/// by implied bounds.
15+
#[derive(derivative::Derivative)]
16+
#[derivative(PartialEq(bound = ""), Eq(bound = ""))]
1517
pub enum ClauseKind<I: Interner> {
1618
/// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be
1719
/// the `Self` type of the trait reference and `A`, `B`, and `C`
@@ -65,23 +67,6 @@ where
6567
{
6668
}
6769

68-
impl<I: Interner> PartialEq for ClauseKind<I> {
69-
fn eq(&self, other: &Self) -> bool {
70-
match (self, other) {
71-
(Self::Trait(l0), Self::Trait(r0)) => l0 == r0,
72-
(Self::RegionOutlives(l0), Self::RegionOutlives(r0)) => l0 == r0,
73-
(Self::TypeOutlives(l0), Self::TypeOutlives(r0)) => l0 == r0,
74-
(Self::Projection(l0), Self::Projection(r0)) => l0 == r0,
75-
(Self::ConstArgHasType(l0, l1), Self::ConstArgHasType(r0, r1)) => l0 == r0 && l1 == r1,
76-
(Self::WellFormed(l0), Self::WellFormed(r0)) => l0 == r0,
77-
(Self::ConstEvaluatable(l0), Self::ConstEvaluatable(r0)) => l0 == r0,
78-
_ => false,
79-
}
80-
}
81-
}
82-
83-
impl<I: Interner> Eq for ClauseKind<I> {}
84-
8570
fn clause_kind_discriminant<I: Interner>(value: &ClauseKind<I>) -> usize {
8671
match value {
8772
ClauseKind::Trait(_) => 0,
@@ -249,6 +234,8 @@ where
249234
}
250235
}
251236

237+
#[derive(derivative::Derivative)]
238+
#[derivative(PartialEq(bound = ""), Eq(bound = ""))]
252239
pub enum PredicateKind<I: Interner> {
253240
/// Prove a clause
254241
Clause(ClauseKind<I>),
@@ -324,27 +311,6 @@ impl<I: Interner> Clone for PredicateKind<I> {
324311
}
325312
}
326313

327-
impl<I: Interner> PartialEq for PredicateKind<I> {
328-
fn eq(&self, other: &Self) -> bool {
329-
match (self, other) {
330-
(Self::Clause(l0), Self::Clause(r0)) => l0 == r0,
331-
(Self::ObjectSafe(l0), Self::ObjectSafe(r0)) => l0 == r0,
332-
(Self::ClosureKind(l0, l1, l2), Self::ClosureKind(r0, r1, r2)) => {
333-
l0 == r0 && l1 == r1 && l2 == r2
334-
}
335-
(Self::Subtype(l0), Self::Subtype(r0)) => l0 == r0,
336-
(Self::Coerce(l0), Self::Coerce(r0)) => l0 == r0,
337-
(Self::ConstEquate(l0, l1), Self::ConstEquate(r0, r1)) => l0 == r0 && l1 == r1,
338-
(Self::AliasRelate(l0, l1, l2), Self::AliasRelate(r0, r1, r2)) => {
339-
l0 == r0 && l1 == r1 && l2 == r2
340-
}
341-
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
342-
}
343-
}
344-
}
345-
346-
impl<I: Interner> Eq for PredicateKind<I> {}
347-
348314
fn predicate_kind_discriminant<I: Interner>(value: &PredicateKind<I>) -> usize {
349315
match value {
350316
PredicateKind::Clause(_) => 0,

compiler/rustc_type_ir/src/region_kind.rs

+2-28
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ use self::RegionKind::*;
118118
/// [1]: https://smallcultfollowing.com/babysteps/blog/2013/10/29/intermingled-parameter-lists/
119119
/// [2]: https://smallcultfollowing.com/babysteps/blog/2013/11/04/intermingled-parameter-lists/
120120
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
121+
#[derive(derivative::Derivative)]
122+
#[derivative(PartialEq(bound = ""), Eq(bound = ""))]
121123
pub enum RegionKind<I: Interner> {
122124
/// Region bound in a type or fn declaration which will be
123125
/// substituted 'early' -- that is, at the same time when type
@@ -194,34 +196,6 @@ impl<I: Interner> Clone for RegionKind<I> {
194196
}
195197
}
196198

197-
// This is manually implemented because a derive would require `I: PartialEq`
198-
impl<I: Interner> PartialEq for RegionKind<I> {
199-
#[inline]
200-
fn eq(&self, other: &RegionKind<I>) -> bool {
201-
regionkind_discriminant(self) == regionkind_discriminant(other)
202-
&& match (self, other) {
203-
(ReEarlyBound(a_r), ReEarlyBound(b_r)) => a_r == b_r,
204-
(ReLateBound(a_d, a_r), ReLateBound(b_d, b_r)) => a_d == b_d && a_r == b_r,
205-
(ReFree(a_r), ReFree(b_r)) => a_r == b_r,
206-
(ReStatic, ReStatic) => true,
207-
(ReVar(a_r), ReVar(b_r)) => a_r == b_r,
208-
(RePlaceholder(a_r), RePlaceholder(b_r)) => a_r == b_r,
209-
(ReErased, ReErased) => true,
210-
(ReError(_), ReError(_)) => true,
211-
_ => {
212-
debug_assert!(
213-
false,
214-
"This branch must be unreachable, maybe the match is missing an arm? self = {self:?}, other = {other:?}"
215-
);
216-
true
217-
}
218-
}
219-
}
220-
}
221-
222-
// This is manually implemented because a derive would require `I: Eq`
223-
impl<I: Interner> Eq for RegionKind<I> {}
224-
225199
// This is manually implemented because a derive would require `I: PartialOrd`
226200
impl<I: Interner> PartialOrd for RegionKind<I> {
227201
#[inline]

compiler/rustc_type_ir/src/ty_kind.rs

+3-52
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ pub enum AliasKind {
114114
/// Types written by the user start out as `hir::TyKind` and get
115115
/// converted to this representation using `AstConv::ast_ty_to_ty`.
116116
#[rustc_diagnostic_item = "IrTyKind"]
117+
#[derive(derivative::Derivative)]
118+
#[derivative(PartialEq(bound = ""), Eq(bound = ""))]
117119
pub enum TyKind<I: Interner> {
118120
/// The primitive boolean type. Written as `bool`.
119121
Bool,
@@ -358,58 +360,6 @@ impl<I: Interner> Clone for TyKind<I> {
358360
}
359361
}
360362

361-
// This is manually implemented because a derive would require `I: PartialEq`
362-
impl<I: Interner> PartialEq for TyKind<I> {
363-
#[inline]
364-
fn eq(&self, other: &TyKind<I>) -> bool {
365-
// You might expect this `match` to be preceded with this:
366-
//
367-
// tykind_discriminant(self) == tykind_discriminant(other) &&
368-
//
369-
// but the data patterns in practice are such that a comparison
370-
// succeeds 99%+ of the time, and it's faster to omit it.
371-
match (self, other) {
372-
(Int(a_i), Int(b_i)) => a_i == b_i,
373-
(Uint(a_u), Uint(b_u)) => a_u == b_u,
374-
(Float(a_f), Float(b_f)) => a_f == b_f,
375-
(Adt(a_d, a_s), Adt(b_d, b_s)) => a_d == b_d && a_s == b_s,
376-
(Foreign(a_d), Foreign(b_d)) => a_d == b_d,
377-
(Array(a_t, a_c), Array(b_t, b_c)) => a_t == b_t && a_c == b_c,
378-
(Slice(a_t), Slice(b_t)) => a_t == b_t,
379-
(RawPtr(a_t), RawPtr(b_t)) => a_t == b_t,
380-
(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,
381-
(FnDef(a_d, a_s), FnDef(b_d, b_s)) => a_d == b_d && a_s == b_s,
382-
(FnPtr(a_s), FnPtr(b_s)) => a_s == b_s,
383-
(Dynamic(a_p, a_r, a_repr), Dynamic(b_p, b_r, b_repr)) => {
384-
a_p == b_p && a_r == b_r && a_repr == b_repr
385-
}
386-
(Closure(a_d, a_s), Closure(b_d, b_s)) => a_d == b_d && a_s == b_s,
387-
(Coroutine(a_d, a_s, a_m), Coroutine(b_d, b_s, b_m)) => {
388-
a_d == b_d && a_s == b_s && a_m == b_m
389-
}
390-
(CoroutineWitness(a_d, a_s), CoroutineWitness(b_d, b_s)) => a_d == b_d && a_s == b_s,
391-
(Tuple(a_t), Tuple(b_t)) => a_t == b_t,
392-
(Alias(a_i, a_p), Alias(b_i, b_p)) => a_i == b_i && a_p == b_p,
393-
(Param(a_p), Param(b_p)) => a_p == b_p,
394-
(Bound(a_d, a_b), Bound(b_d, b_b)) => a_d == b_d && a_b == b_b,
395-
(Placeholder(a_p), Placeholder(b_p)) => a_p == b_p,
396-
(Infer(a_t), Infer(b_t)) => a_t == b_t,
397-
(Error(a_e), Error(b_e)) => a_e == b_e,
398-
(Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => true,
399-
_ => {
400-
debug_assert!(
401-
tykind_discriminant(self) != tykind_discriminant(other),
402-
"This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}"
403-
);
404-
false
405-
}
406-
}
407-
}
408-
}
409-
410-
// This is manually implemented because a derive would require `I: Eq`
411-
impl<I: Interner> Eq for TyKind<I> {}
412-
413363
// This is manually implemented because a derive would require `I: PartialOrd`
414364
impl<I: Interner> PartialOrd for TyKind<I> {
415365
#[inline]
@@ -614,6 +564,7 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
614564
}
615565
}
616566
}
567+
617568
// This is manually implemented because a derive would require `I: Debug`
618569
impl<I: Interner> fmt::Debug for TyKind<I> {
619570
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

src/tools/tidy/src/deps.rs

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
142142
"darling_core",
143143
"darling_macro",
144144
"datafrog",
145+
"derivative",
145146
"derive_more",
146147
"derive_setters",
147148
"digest",

0 commit comments

Comments
 (0)