Skip to content

Commit e9ffb12

Browse files
committed
Auto merge of rust-lang#117184 - compiler-errors:derivative, r=<try>
[DO NOT COMMIT] Use derivative to simplify impls in `rustc_type_ir` Gotta keep things like `Copy` impl around since they've got a ton of bounds. r? `@ghost`
2 parents 6f65201 + b1a97f6 commit e9ffb12

File tree

8 files changed

+50
-509
lines changed

8 files changed

+50
-509
lines changed

Cargo.lock

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

1014+
[[package]]
1015+
name = "derivative"
1016+
version = "2.2.0"
1017+
source = "git+https://github.com/compiler-errors/rust-derivative.git?branch=fast-discriminant#fd1434ceff14441827f68505bc7f13e6d4eec6da"
1018+
dependencies = [
1019+
"proc-macro2",
1020+
"quote",
1021+
"syn 1.0.109",
1022+
]
1023+
10141024
[[package]]
10151025
name = "derive_builder"
10161026
version = "0.12.0"
@@ -4671,6 +4681,7 @@ name = "rustc_type_ir"
46714681
version = "0.0.0"
46724682
dependencies = [
46734683
"bitflags 1.3.2",
4684+
"derivative",
46744685
"rustc_data_structures",
46754686
"rustc_index",
46764687
"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 = { git = "https://github.com/compiler-errors/rust-derivative.git", branch = "fast-discriminant" }

compiler/rustc_type_ir/src/const_kind.rs

+9-82
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use rustc_data_structures::stable_hasher::HashStable;
22
use rustc_data_structures::stable_hasher::StableHasher;
33
use rustc_serialize::{Decodable, Decoder, Encodable};
4-
use std::cmp::Ordering;
54
use std::fmt;
6-
use std::hash;
75

86
use crate::{
97
DebruijnIndex, DebugWithInfcx, HashStableContext, InferCtxtLike, Interner, TyDecoder,
@@ -13,7 +11,15 @@ use crate::{
1311
use self::ConstKind::*;
1412

1513
/// Represents a constant in Rust.
16-
// #[derive(derive_more::From)]
14+
#[derive(derivative::Derivative)]
15+
#[derivative(
16+
Clone(bound = ""),
17+
PartialEq(bound = ""),
18+
Eq(bound = ""),
19+
PartialOrd(bound = ""),
20+
Ord(bound = ""),
21+
Hash(bound = "")
22+
)]
1723
pub enum ConstKind<I: Interner> {
1824
/// A const generic parameter.
1925
Param(I::ParamConst),
@@ -57,25 +63,6 @@ const fn const_kind_discriminant<I: Interner>(value: &ConstKind<I>) -> usize {
5763
}
5864
}
5965

60-
impl<I: Interner> hash::Hash for ConstKind<I> {
61-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
62-
const_kind_discriminant(self).hash(state);
63-
match self {
64-
Param(p) => p.hash(state),
65-
Infer(i) => i.hash(state),
66-
Bound(d, b) => {
67-
d.hash(state);
68-
b.hash(state);
69-
}
70-
Placeholder(p) => p.hash(state),
71-
Unevaluated(u) => u.hash(state),
72-
Value(v) => v.hash(state),
73-
Error(e) => e.hash(state),
74-
Expr(e) => e.hash(state),
75-
}
76-
}
77-
}
78-
7966
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for ConstKind<I>
8067
where
8168
I::ParamConst: HashStable<CTX>,
@@ -166,66 +153,6 @@ where
166153
}
167154
}
168155

169-
impl<I: Interner> PartialOrd for ConstKind<I> {
170-
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
171-
Some(self.cmp(other))
172-
}
173-
}
174-
175-
impl<I: Interner> Ord for ConstKind<I> {
176-
fn cmp(&self, other: &Self) -> Ordering {
177-
const_kind_discriminant(self)
178-
.cmp(&const_kind_discriminant(other))
179-
.then_with(|| match (self, other) {
180-
(Param(p1), Param(p2)) => p1.cmp(p2),
181-
(Infer(i1), Infer(i2)) => i1.cmp(i2),
182-
(Bound(d1, b1), Bound(d2, b2)) => d1.cmp(d2).then_with(|| b1.cmp(b2)),
183-
(Placeholder(p1), Placeholder(p2)) => p1.cmp(p2),
184-
(Unevaluated(u1), Unevaluated(u2)) => u1.cmp(u2),
185-
(Value(v1), Value(v2)) => v1.cmp(v2),
186-
(Error(e1), Error(e2)) => e1.cmp(e2),
187-
(Expr(e1), Expr(e2)) => e1.cmp(e2),
188-
_ => {
189-
debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = {self:?}, other = {other:?}");
190-
Ordering::Equal
191-
}
192-
})
193-
}
194-
}
195-
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-
214-
impl<I: Interner> Clone for ConstKind<I> {
215-
fn clone(&self) -> Self {
216-
match self {
217-
Param(arg0) => Param(arg0.clone()),
218-
Infer(arg0) => Infer(arg0.clone()),
219-
Bound(arg0, arg1) => Bound(arg0.clone(), arg1.clone()),
220-
Placeholder(arg0) => Placeholder(arg0.clone()),
221-
Unevaluated(arg0) => Unevaluated(arg0.clone()),
222-
Value(arg0) => Value(arg0.clone()),
223-
Error(arg0) => Error(arg0.clone()),
224-
Expr(arg0) => Expr(arg0.clone()),
225-
}
226-
}
227-
}
228-
229156
impl<I: Interner> fmt::Debug for ConstKind<I> {
230157
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
231158
WithInfcx::with_no_infcx(self).fmt(f)

compiler/rustc_type_ir/src/predicate_kind.rs

+4-117
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};
@@ -12,6 +11,8 @@ use crate::{TyDecoder, TyEncoder};
1211

1312
/// A clause is something that can appear in where bounds or be inferred
1413
/// by implied bounds.
14+
#[derive(derivative::Derivative)]
15+
#[derivative(Clone(bound = ""), PartialEq(bound = ""), Eq(bound = ""), Hash(bound = ""))]
1516
pub enum ClauseKind<I: Interner> {
1617
/// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be
1718
/// the `Self` type of the trait reference and `A`, `B`, and `C`
@@ -39,20 +40,6 @@ pub enum ClauseKind<I: Interner> {
3940
ConstEvaluatable(I::Const),
4041
}
4142

42-
impl<I: Interner> Clone for ClauseKind<I> {
43-
fn clone(&self) -> Self {
44-
match self {
45-
Self::Trait(arg0) => Self::Trait(arg0.clone()),
46-
Self::RegionOutlives(arg0) => Self::RegionOutlives(arg0.clone()),
47-
Self::TypeOutlives(arg0) => Self::TypeOutlives(arg0.clone()),
48-
Self::Projection(arg0) => Self::Projection(arg0.clone()),
49-
Self::ConstArgHasType(arg0, arg1) => Self::ConstArgHasType(arg0.clone(), arg1.clone()),
50-
Self::WellFormed(arg0) => Self::WellFormed(arg0.clone()),
51-
Self::ConstEvaluatable(arg0) => Self::ConstEvaluatable(arg0.clone()),
52-
}
53-
}
54-
}
55-
5643
impl<I: Interner> Copy for ClauseKind<I>
5744
where
5845
I::Ty: Copy,
@@ -65,23 +52,6 @@ where
6552
{
6653
}
6754

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-
8555
fn clause_kind_discriminant<I: Interner>(value: &ClauseKind<I>) -> usize {
8656
match value {
8757
ClauseKind::Trait(_) => 0,
@@ -94,24 +64,6 @@ fn clause_kind_discriminant<I: Interner>(value: &ClauseKind<I>) -> usize {
9464
}
9565
}
9666

97-
impl<I: Interner> hash::Hash for ClauseKind<I> {
98-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
99-
clause_kind_discriminant(self).hash(state);
100-
match self {
101-
ClauseKind::Trait(p) => p.hash(state),
102-
ClauseKind::RegionOutlives(p) => p.hash(state),
103-
ClauseKind::TypeOutlives(p) => p.hash(state),
104-
ClauseKind::Projection(p) => p.hash(state),
105-
ClauseKind::ConstArgHasType(c, t) => {
106-
c.hash(state);
107-
t.hash(state);
108-
}
109-
ClauseKind::WellFormed(t) => t.hash(state),
110-
ClauseKind::ConstEvaluatable(c) => c.hash(state),
111-
}
112-
}
113-
}
114-
11567
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for ClauseKind<I>
11668
where
11769
I::Ty: HashStable<CTX>,
@@ -249,6 +201,8 @@ where
249201
}
250202
}
251203

204+
#[derive(derivative::Derivative)]
205+
#[derivative(Clone(bound = ""), PartialEq(bound = ""), Eq(bound = ""), Hash(bound = ""))]
252206
pub enum PredicateKind<I: Interner> {
253207
/// Prove a clause
254208
Clause(ClauseKind<I>),
@@ -305,46 +259,6 @@ where
305259
{
306260
}
307261

308-
impl<I: Interner> Clone for PredicateKind<I> {
309-
fn clone(&self) -> Self {
310-
match self {
311-
Self::Clause(arg0) => Self::Clause(arg0.clone()),
312-
Self::ObjectSafe(arg0) => Self::ObjectSafe(arg0.clone()),
313-
Self::ClosureKind(arg0, arg1, arg2) => {
314-
Self::ClosureKind(arg0.clone(), arg1.clone(), arg2.clone())
315-
}
316-
Self::Subtype(arg0) => Self::Subtype(arg0.clone()),
317-
Self::Coerce(arg0) => Self::Coerce(arg0.clone()),
318-
Self::ConstEquate(arg0, arg1) => Self::ConstEquate(arg0.clone(), arg1.clone()),
319-
Self::Ambiguous => Self::Ambiguous,
320-
Self::AliasRelate(arg0, arg1, arg2) => {
321-
Self::AliasRelate(arg0.clone(), arg1.clone(), arg2.clone())
322-
}
323-
}
324-
}
325-
}
326-
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-
348262
fn predicate_kind_discriminant<I: Interner>(value: &PredicateKind<I>) -> usize {
349263
match value {
350264
PredicateKind::Clause(_) => 0,
@@ -358,33 +272,6 @@ fn predicate_kind_discriminant<I: Interner>(value: &PredicateKind<I>) -> usize {
358272
}
359273
}
360274

361-
impl<I: Interner> hash::Hash for PredicateKind<I> {
362-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
363-
predicate_kind_discriminant(self).hash(state);
364-
match self {
365-
PredicateKind::Clause(p) => p.hash(state),
366-
PredicateKind::ObjectSafe(d) => d.hash(state),
367-
PredicateKind::ClosureKind(d, g, k) => {
368-
d.hash(state);
369-
g.hash(state);
370-
k.hash(state);
371-
}
372-
PredicateKind::Subtype(p) => p.hash(state),
373-
PredicateKind::Coerce(p) => p.hash(state),
374-
PredicateKind::ConstEquate(c1, c2) => {
375-
c1.hash(state);
376-
c2.hash(state);
377-
}
378-
PredicateKind::Ambiguous => {}
379-
PredicateKind::AliasRelate(t1, t2, r) => {
380-
t1.hash(state);
381-
t2.hash(state);
382-
r.hash(state);
383-
}
384-
}
385-
}
386-
}
387-
388275
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for PredicateKind<I>
389276
where
390277
I::DefId: HashStable<CTX>,

0 commit comments

Comments
 (0)