Skip to content

Commit 8eb932d

Browse files
Use derivative for PartialOrd/ord
1 parent de83057 commit 8eb932d

File tree

3 files changed

+21
-123
lines changed

3 files changed

+21
-123
lines changed

compiler/rustc_type_ir/src/const_kind.rs

+7-29
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

@@ -14,7 +13,13 @@ use self::ConstKind::*;
1413

1514
/// Represents a constant in Rust.
1615
#[derive(derivative::Derivative)]
17-
#[derivative(Clone(bound = ""))]
16+
#[derivative(
17+
Clone(bound = ""),
18+
PartialOrd(bound = ""),
19+
PartialOrd = "feature_allow_slow_enum",
20+
Ord(bound = ""),
21+
Ord = "feature_allow_slow_enum"
22+
)]
1823
pub enum ConstKind<I: Interner> {
1924
/// A const generic parameter.
2025
Param(I::ParamConst),
@@ -167,33 +172,6 @@ where
167172
}
168173
}
169174

170-
impl<I: Interner> PartialOrd for ConstKind<I> {
171-
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
172-
Some(self.cmp(other))
173-
}
174-
}
175-
176-
impl<I: Interner> Ord for ConstKind<I> {
177-
fn cmp(&self, other: &Self) -> Ordering {
178-
const_kind_discriminant(self)
179-
.cmp(&const_kind_discriminant(other))
180-
.then_with(|| match (self, other) {
181-
(Param(p1), Param(p2)) => p1.cmp(p2),
182-
(Infer(i1), Infer(i2)) => i1.cmp(i2),
183-
(Bound(d1, b1), Bound(d2, b2)) => d1.cmp(d2).then_with(|| b1.cmp(b2)),
184-
(Placeholder(p1), Placeholder(p2)) => p1.cmp(p2),
185-
(Unevaluated(u1), Unevaluated(u2)) => u1.cmp(u2),
186-
(Value(v1), Value(v2)) => v1.cmp(v2),
187-
(Error(e1), Error(e2)) => e1.cmp(e2),
188-
(Expr(e1), Expr(e2)) => e1.cmp(e2),
189-
_ => {
190-
debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = {self:?}, other = {other:?}");
191-
Ordering::Equal
192-
}
193-
})
194-
}
195-
}
196-
197175
impl<I: Interner> PartialEq for ConstKind<I> {
198176
fn eq(&self, other: &Self) -> bool {
199177
match (self, other) {

compiler/rustc_type_ir/src/region_kind.rs

+7-34
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

@@ -119,7 +118,13 @@ use self::RegionKind::*;
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
121120
#[derive(derivative::Derivative)]
122-
#[derivative(Clone(bound = ""))]
121+
#[derivative(
122+
Clone(bound = ""),
123+
PartialOrd(bound = ""),
124+
PartialOrd = "feature_allow_slow_enum",
125+
Ord(bound = ""),
126+
Ord = "feature_allow_slow_enum"
127+
)]
123128
pub enum RegionKind<I: Interner> {
124129
/// Region bound in a type or fn declaration which will be
125130
/// substituted 'early' -- that is, at the same time when type
@@ -208,38 +213,6 @@ impl<I: Interner> PartialEq for RegionKind<I> {
208213
// This is manually implemented because a derive would require `I: Eq`
209214
impl<I: Interner> Eq for RegionKind<I> {}
210215

211-
// This is manually implemented because a derive would require `I: PartialOrd`
212-
impl<I: Interner> PartialOrd for RegionKind<I> {
213-
#[inline]
214-
fn partial_cmp(&self, other: &RegionKind<I>) -> Option<Ordering> {
215-
Some(self.cmp(other))
216-
}
217-
}
218-
219-
// This is manually implemented because a derive would require `I: Ord`
220-
impl<I: Interner> Ord for RegionKind<I> {
221-
#[inline]
222-
fn cmp(&self, other: &RegionKind<I>) -> Ordering {
223-
regionkind_discriminant(self).cmp(&regionkind_discriminant(other)).then_with(|| {
224-
match (self, other) {
225-
(ReEarlyBound(a_r), ReEarlyBound(b_r)) => a_r.cmp(b_r),
226-
(ReLateBound(a_d, a_r), ReLateBound(b_d, b_r)) => {
227-
a_d.cmp(b_d).then_with(|| a_r.cmp(b_r))
228-
}
229-
(ReFree(a_r), ReFree(b_r)) => a_r.cmp(b_r),
230-
(ReStatic, ReStatic) => Ordering::Equal,
231-
(ReVar(a_r), ReVar(b_r)) => a_r.cmp(b_r),
232-
(RePlaceholder(a_r), RePlaceholder(b_r)) => a_r.cmp(b_r),
233-
(ReErased, ReErased) => Ordering::Equal,
234-
_ => {
235-
debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}");
236-
Ordering::Equal
237-
}
238-
}
239-
})
240-
}
241-
}
242-
243216
// This is manually implemented because a derive would require `I: Hash`
244217
impl<I: Interner> hash::Hash for RegionKind<I> {
245218
fn hash<H: hash::Hasher>(&self, state: &mut H) -> () {

compiler/rustc_type_ir/src/ty_kind.rs

+7-60
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

@@ -115,7 +114,13 @@ pub enum AliasKind {
115114
/// converted to this representation using `AstConv::ast_ty_to_ty`.
116115
#[rustc_diagnostic_item = "IrTyKind"]
117116
#[derive(derivative::Derivative)]
118-
#[derivative(Clone(bound = ""))]
117+
#[derivative(
118+
Clone(bound = ""),
119+
PartialOrd(bound = ""),
120+
PartialOrd = "feature_allow_slow_enum",
121+
Ord(bound = ""),
122+
Ord = "feature_allow_slow_enum"
123+
)]
119124
pub enum TyKind<I: Interner> {
120125
/// The primitive boolean type. Written as `bool`.
121126
Bool,
@@ -378,64 +383,6 @@ impl<I: Interner> PartialEq for TyKind<I> {
378383
// This is manually implemented because a derive would require `I: Eq`
379384
impl<I: Interner> Eq for TyKind<I> {}
380385

381-
// This is manually implemented because a derive would require `I: PartialOrd`
382-
impl<I: Interner> PartialOrd for TyKind<I> {
383-
#[inline]
384-
fn partial_cmp(&self, other: &TyKind<I>) -> Option<Ordering> {
385-
Some(self.cmp(other))
386-
}
387-
}
388-
389-
// This is manually implemented because a derive would require `I: Ord`
390-
impl<I: Interner> Ord for TyKind<I> {
391-
#[inline]
392-
fn cmp(&self, other: &TyKind<I>) -> Ordering {
393-
tykind_discriminant(self).cmp(&tykind_discriminant(other)).then_with(|| {
394-
match (self, other) {
395-
(Int(a_i), Int(b_i)) => a_i.cmp(b_i),
396-
(Uint(a_u), Uint(b_u)) => a_u.cmp(b_u),
397-
(Float(a_f), Float(b_f)) => a_f.cmp(b_f),
398-
(Adt(a_d, a_s), Adt(b_d, b_s)) => a_d.cmp(b_d).then_with(|| a_s.cmp(b_s)),
399-
(Foreign(a_d), Foreign(b_d)) => a_d.cmp(b_d),
400-
(Array(a_t, a_c), Array(b_t, b_c)) => a_t.cmp(b_t).then_with(|| a_c.cmp(b_c)),
401-
(Slice(a_t), Slice(b_t)) => a_t.cmp(b_t),
402-
(RawPtr(a_t), RawPtr(b_t)) => a_t.cmp(b_t),
403-
(Ref(a_r, a_t, a_m), Ref(b_r, b_t, b_m)) => {
404-
a_r.cmp(b_r).then_with(|| a_t.cmp(b_t).then_with(|| a_m.cmp(b_m)))
405-
}
406-
(FnDef(a_d, a_s), FnDef(b_d, b_s)) => a_d.cmp(b_d).then_with(|| a_s.cmp(b_s)),
407-
(FnPtr(a_s), FnPtr(b_s)) => a_s.cmp(b_s),
408-
(Dynamic(a_p, a_r, a_repr), Dynamic(b_p, b_r, b_repr)) => {
409-
a_p.cmp(b_p).then_with(|| a_r.cmp(b_r).then_with(|| a_repr.cmp(b_repr)))
410-
}
411-
(Closure(a_p, a_s), Closure(b_p, b_s)) => a_p.cmp(b_p).then_with(|| a_s.cmp(b_s)),
412-
(Coroutine(a_d, a_s, a_m), Coroutine(b_d, b_s, b_m)) => {
413-
a_d.cmp(b_d).then_with(|| a_s.cmp(b_s).then_with(|| a_m.cmp(b_m)))
414-
}
415-
(
416-
CoroutineWitness(a_d, a_s),
417-
CoroutineWitness(b_d, b_s),
418-
) => match Ord::cmp(a_d, b_d) {
419-
Ordering::Equal => Ord::cmp(a_s, b_s),
420-
cmp => cmp,
421-
},
422-
(Tuple(a_t), Tuple(b_t)) => a_t.cmp(b_t),
423-
(Alias(a_i, a_p), Alias(b_i, b_p)) => a_i.cmp(b_i).then_with(|| a_p.cmp(b_p)),
424-
(Param(a_p), Param(b_p)) => a_p.cmp(b_p),
425-
(Bound(a_d, a_b), Bound(b_d, b_b)) => a_d.cmp(b_d).then_with(|| a_b.cmp(b_b)),
426-
(Placeholder(a_p), Placeholder(b_p)) => a_p.cmp(b_p),
427-
(Infer(a_t), Infer(b_t)) => a_t.cmp(b_t),
428-
(Error(a_e), Error(b_e)) => a_e.cmp(b_e),
429-
(Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => Ordering::Equal,
430-
_ => {
431-
debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = {self:?}, other = {other:?}");
432-
Ordering::Equal
433-
}
434-
}
435-
})
436-
}
437-
}
438-
439386
// This is manually implemented because a derive would require `I: Hash`
440387
impl<I: Interner> hash::Hash for TyKind<I> {
441388
fn hash<__H: hash::Hasher>(&self, state: &mut __H) -> () {

0 commit comments

Comments
 (0)