Skip to content

Commit 6d7822f

Browse files
Use derivative for PartialOrd/ord
1 parent 698db85 commit 6d7822f

File tree

6 files changed

+36
-121
lines changed

6 files changed

+36
-121
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
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"
@@ -4671,6 +4682,7 @@ name = "rustc_type_ir"
46714682
version = "0.0.0"
46724683
dependencies = [
46734684
"bitflags 1.3.2",
4685+
"derivative",
46744686
"rustc_data_structures",
46754687
"rustc_index",
46764688
"rustc_macros",

compiler/rustc_type_ir/Cargo.toml

Lines changed: 1 addition & 0 deletions
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/const_kind.rs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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;
65
use std::hash;
76

@@ -13,7 +12,13 @@ use crate::{
1312
use self::ConstKind::*;
1413

1514
/// Represents a constant in Rust.
16-
// #[derive(derive_more::From)]
15+
#[derive(derivative::Derivative)]
16+
#[derivative(
17+
PartialOrd(bound = ""),
18+
PartialOrd = "feature_allow_slow_enum",
19+
Ord(bound = ""),
20+
Ord = "feature_allow_slow_enum"
21+
)]
1722
pub enum ConstKind<I: Interner> {
1823
/// A const generic parameter.
1924
Param(I::ParamConst),
@@ -166,33 +171,6 @@ where
166171
}
167172
}
168173

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-
196174
impl<I: Interner> PartialEq for ConstKind<I> {
197175
fn eq(&self, other: &Self) -> bool {
198176
match (self, other) {

compiler/rustc_type_ir/src/region_kind.rs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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;
65
use std::hash;
76

@@ -118,6 +117,13 @@ use self::RegionKind::*;
118117
/// [1]: https://smallcultfollowing.com/babysteps/blog/2013/10/29/intermingled-parameter-lists/
119118
/// [2]: https://smallcultfollowing.com/babysteps/blog/2013/11/04/intermingled-parameter-lists/
120119
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
120+
#[derive(derivative::Derivative)]
121+
#[derivative(
122+
PartialOrd(bound = ""),
123+
PartialOrd = "feature_allow_slow_enum",
124+
Ord(bound = ""),
125+
Ord = "feature_allow_slow_enum"
126+
)]
121127
pub enum RegionKind<I: Interner> {
122128
/// Region bound in a type or fn declaration which will be
123129
/// substituted 'early' -- that is, at the same time when type
@@ -222,38 +228,6 @@ impl<I: Interner> PartialEq for RegionKind<I> {
222228
// This is manually implemented because a derive would require `I: Eq`
223229
impl<I: Interner> Eq for RegionKind<I> {}
224230

225-
// This is manually implemented because a derive would require `I: PartialOrd`
226-
impl<I: Interner> PartialOrd for RegionKind<I> {
227-
#[inline]
228-
fn partial_cmp(&self, other: &RegionKind<I>) -> Option<Ordering> {
229-
Some(self.cmp(other))
230-
}
231-
}
232-
233-
// This is manually implemented because a derive would require `I: Ord`
234-
impl<I: Interner> Ord for RegionKind<I> {
235-
#[inline]
236-
fn cmp(&self, other: &RegionKind<I>) -> Ordering {
237-
regionkind_discriminant(self).cmp(&regionkind_discriminant(other)).then_with(|| {
238-
match (self, other) {
239-
(ReEarlyBound(a_r), ReEarlyBound(b_r)) => a_r.cmp(b_r),
240-
(ReLateBound(a_d, a_r), ReLateBound(b_d, b_r)) => {
241-
a_d.cmp(b_d).then_with(|| a_r.cmp(b_r))
242-
}
243-
(ReFree(a_r), ReFree(b_r)) => a_r.cmp(b_r),
244-
(ReStatic, ReStatic) => Ordering::Equal,
245-
(ReVar(a_r), ReVar(b_r)) => a_r.cmp(b_r),
246-
(RePlaceholder(a_r), RePlaceholder(b_r)) => a_r.cmp(b_r),
247-
(ReErased, ReErased) => Ordering::Equal,
248-
_ => {
249-
debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}");
250-
Ordering::Equal
251-
}
252-
}
253-
})
254-
}
255-
}
256-
257231
// This is manually implemented because a derive would require `I: Hash`
258232
impl<I: Interner> hash::Hash for RegionKind<I> {
259233
fn hash<H: hash::Hasher>(&self, state: &mut H) -> () {

compiler/rustc_type_ir/src/ty_kind.rs

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
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::cmp::Ordering;
76
use std::mem::discriminant;
87
use std::{fmt, hash};
98

@@ -114,6 +113,13 @@ pub enum AliasKind {
114113
/// Types written by the user start out as `hir::TyKind` and get
115114
/// converted to this representation using `AstConv::ast_ty_to_ty`.
116115
#[rustc_diagnostic_item = "IrTyKind"]
116+
#[derive(derivative::Derivative)]
117+
#[derivative(
118+
PartialOrd(bound = ""),
119+
PartialOrd = "feature_allow_slow_enum",
120+
Ord(bound = ""),
121+
Ord = "feature_allow_slow_enum"
122+
)]
117123
pub enum TyKind<I: Interner> {
118124
/// The primitive boolean type. Written as `bool`.
119125
Bool,
@@ -410,64 +416,6 @@ impl<I: Interner> PartialEq for TyKind<I> {
410416
// This is manually implemented because a derive would require `I: Eq`
411417
impl<I: Interner> Eq for TyKind<I> {}
412418

413-
// This is manually implemented because a derive would require `I: PartialOrd`
414-
impl<I: Interner> PartialOrd for TyKind<I> {
415-
#[inline]
416-
fn partial_cmp(&self, other: &TyKind<I>) -> Option<Ordering> {
417-
Some(self.cmp(other))
418-
}
419-
}
420-
421-
// This is manually implemented because a derive would require `I: Ord`
422-
impl<I: Interner> Ord for TyKind<I> {
423-
#[inline]
424-
fn cmp(&self, other: &TyKind<I>) -> Ordering {
425-
tykind_discriminant(self).cmp(&tykind_discriminant(other)).then_with(|| {
426-
match (self, other) {
427-
(Int(a_i), Int(b_i)) => a_i.cmp(b_i),
428-
(Uint(a_u), Uint(b_u)) => a_u.cmp(b_u),
429-
(Float(a_f), Float(b_f)) => a_f.cmp(b_f),
430-
(Adt(a_d, a_s), Adt(b_d, b_s)) => a_d.cmp(b_d).then_with(|| a_s.cmp(b_s)),
431-
(Foreign(a_d), Foreign(b_d)) => a_d.cmp(b_d),
432-
(Array(a_t, a_c), Array(b_t, b_c)) => a_t.cmp(b_t).then_with(|| a_c.cmp(b_c)),
433-
(Slice(a_t), Slice(b_t)) => a_t.cmp(b_t),
434-
(RawPtr(a_t), RawPtr(b_t)) => a_t.cmp(b_t),
435-
(Ref(a_r, a_t, a_m), Ref(b_r, b_t, b_m)) => {
436-
a_r.cmp(b_r).then_with(|| a_t.cmp(b_t).then_with(|| a_m.cmp(b_m)))
437-
}
438-
(FnDef(a_d, a_s), FnDef(b_d, b_s)) => a_d.cmp(b_d).then_with(|| a_s.cmp(b_s)),
439-
(FnPtr(a_s), FnPtr(b_s)) => a_s.cmp(b_s),
440-
(Dynamic(a_p, a_r, a_repr), Dynamic(b_p, b_r, b_repr)) => {
441-
a_p.cmp(b_p).then_with(|| a_r.cmp(b_r).then_with(|| a_repr.cmp(b_repr)))
442-
}
443-
(Closure(a_p, a_s), Closure(b_p, b_s)) => a_p.cmp(b_p).then_with(|| a_s.cmp(b_s)),
444-
(Coroutine(a_d, a_s, a_m), Coroutine(b_d, b_s, b_m)) => {
445-
a_d.cmp(b_d).then_with(|| a_s.cmp(b_s).then_with(|| a_m.cmp(b_m)))
446-
}
447-
(
448-
CoroutineWitness(a_d, a_s),
449-
CoroutineWitness(b_d, b_s),
450-
) => match Ord::cmp(a_d, b_d) {
451-
Ordering::Equal => Ord::cmp(a_s, b_s),
452-
cmp => cmp,
453-
},
454-
(Tuple(a_t), Tuple(b_t)) => a_t.cmp(b_t),
455-
(Alias(a_i, a_p), Alias(b_i, b_p)) => a_i.cmp(b_i).then_with(|| a_p.cmp(b_p)),
456-
(Param(a_p), Param(b_p)) => a_p.cmp(b_p),
457-
(Bound(a_d, a_b), Bound(b_d, b_b)) => a_d.cmp(b_d).then_with(|| a_b.cmp(b_b)),
458-
(Placeholder(a_p), Placeholder(b_p)) => a_p.cmp(b_p),
459-
(Infer(a_t), Infer(b_t)) => a_t.cmp(b_t),
460-
(Error(a_e), Error(b_e)) => a_e.cmp(b_e),
461-
(Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => Ordering::Equal,
462-
_ => {
463-
debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = {self:?}, other = {other:?}");
464-
Ordering::Equal
465-
}
466-
}
467-
})
468-
}
469-
}
470-
471419
// This is manually implemented because a derive would require `I: Hash`
472420
impl<I: Interner> hash::Hash for TyKind<I> {
473421
fn hash<__H: hash::Hasher>(&self, state: &mut __H) -> () {
@@ -614,6 +562,7 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
614562
}
615563
}
616564
}
565+
617566
// This is manually implemented because a derive would require `I: Debug`
618567
impl<I: Interner> fmt::Debug for TyKind<I> {
619568
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

src/tools/tidy/src/deps.rs

Lines changed: 1 addition & 0 deletions
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)