Use derivative for PartialOrd/ord
This commit is contained in:
parent
de83057ac4
commit
8eb932dcf0
3 changed files with 21 additions and 123 deletions
|
|
@ -1,7 +1,6 @@
|
|||
use rustc_data_structures::stable_hasher::HashStable;
|
||||
use rustc_data_structures::stable_hasher::StableHasher;
|
||||
use rustc_serialize::{Decodable, Decoder, Encodable};
|
||||
use std::cmp::Ordering;
|
||||
use std::fmt;
|
||||
use std::hash;
|
||||
|
||||
|
|
@ -14,7 +13,13 @@ use self::ConstKind::*;
|
|||
|
||||
/// Represents a constant in Rust.
|
||||
#[derive(derivative::Derivative)]
|
||||
#[derivative(Clone(bound = ""))]
|
||||
#[derivative(
|
||||
Clone(bound = ""),
|
||||
PartialOrd(bound = ""),
|
||||
PartialOrd = "feature_allow_slow_enum",
|
||||
Ord(bound = ""),
|
||||
Ord = "feature_allow_slow_enum"
|
||||
)]
|
||||
pub enum ConstKind<I: Interner> {
|
||||
/// A const generic parameter.
|
||||
Param(I::ParamConst),
|
||||
|
|
@ -167,33 +172,6 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<I: Interner> PartialOrd for ConstKind<I> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Interner> Ord for ConstKind<I> {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
const_kind_discriminant(self)
|
||||
.cmp(&const_kind_discriminant(other))
|
||||
.then_with(|| match (self, other) {
|
||||
(Param(p1), Param(p2)) => p1.cmp(p2),
|
||||
(Infer(i1), Infer(i2)) => i1.cmp(i2),
|
||||
(Bound(d1, b1), Bound(d2, b2)) => d1.cmp(d2).then_with(|| b1.cmp(b2)),
|
||||
(Placeholder(p1), Placeholder(p2)) => p1.cmp(p2),
|
||||
(Unevaluated(u1), Unevaluated(u2)) => u1.cmp(u2),
|
||||
(Value(v1), Value(v2)) => v1.cmp(v2),
|
||||
(Error(e1), Error(e2)) => e1.cmp(e2),
|
||||
(Expr(e1), Expr(e2)) => e1.cmp(e2),
|
||||
_ => {
|
||||
debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = {self:?}, other = {other:?}");
|
||||
Ordering::Equal
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Interner> PartialEq for ConstKind<I> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
use rustc_data_structures::stable_hasher::HashStable;
|
||||
use rustc_data_structures::stable_hasher::StableHasher;
|
||||
use rustc_serialize::{Decodable, Decoder, Encodable};
|
||||
use std::cmp::Ordering;
|
||||
use std::fmt;
|
||||
use std::hash;
|
||||
|
||||
|
|
@ -119,7 +118,13 @@ use self::RegionKind::*;
|
|||
/// [2]: https://smallcultfollowing.com/babysteps/blog/2013/11/04/intermingled-parameter-lists/
|
||||
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
|
||||
#[derive(derivative::Derivative)]
|
||||
#[derivative(Clone(bound = ""))]
|
||||
#[derivative(
|
||||
Clone(bound = ""),
|
||||
PartialOrd(bound = ""),
|
||||
PartialOrd = "feature_allow_slow_enum",
|
||||
Ord(bound = ""),
|
||||
Ord = "feature_allow_slow_enum"
|
||||
)]
|
||||
pub enum RegionKind<I: Interner> {
|
||||
/// Region bound in a type or fn declaration which will be
|
||||
/// substituted 'early' -- that is, at the same time when type
|
||||
|
|
@ -208,38 +213,6 @@ impl<I: Interner> PartialEq for RegionKind<I> {
|
|||
// This is manually implemented because a derive would require `I: Eq`
|
||||
impl<I: Interner> Eq for RegionKind<I> {}
|
||||
|
||||
// This is manually implemented because a derive would require `I: PartialOrd`
|
||||
impl<I: Interner> PartialOrd for RegionKind<I> {
|
||||
#[inline]
|
||||
fn partial_cmp(&self, other: &RegionKind<I>) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
// This is manually implemented because a derive would require `I: Ord`
|
||||
impl<I: Interner> Ord for RegionKind<I> {
|
||||
#[inline]
|
||||
fn cmp(&self, other: &RegionKind<I>) -> Ordering {
|
||||
regionkind_discriminant(self).cmp(®ionkind_discriminant(other)).then_with(|| {
|
||||
match (self, other) {
|
||||
(ReEarlyBound(a_r), ReEarlyBound(b_r)) => a_r.cmp(b_r),
|
||||
(ReLateBound(a_d, a_r), ReLateBound(b_d, b_r)) => {
|
||||
a_d.cmp(b_d).then_with(|| a_r.cmp(b_r))
|
||||
}
|
||||
(ReFree(a_r), ReFree(b_r)) => a_r.cmp(b_r),
|
||||
(ReStatic, ReStatic) => Ordering::Equal,
|
||||
(ReVar(a_r), ReVar(b_r)) => a_r.cmp(b_r),
|
||||
(RePlaceholder(a_r), RePlaceholder(b_r)) => a_r.cmp(b_r),
|
||||
(ReErased, ReErased) => Ordering::Equal,
|
||||
_ => {
|
||||
debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}");
|
||||
Ordering::Equal
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// This is manually implemented because a derive would require `I: Hash`
|
||||
impl<I: Interner> hash::Hash for RegionKind<I> {
|
||||
fn hash<H: hash::Hasher>(&self, state: &mut H) -> () {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
||||
use rustc_data_structures::unify::{EqUnifyValue, UnifyKey};
|
||||
use rustc_serialize::{Decodable, Decoder, Encodable};
|
||||
use std::cmp::Ordering;
|
||||
use std::mem::discriminant;
|
||||
use std::{fmt, hash};
|
||||
|
||||
|
|
@ -115,7 +114,13 @@ pub enum AliasKind {
|
|||
/// converted to this representation using `AstConv::ast_ty_to_ty`.
|
||||
#[rustc_diagnostic_item = "IrTyKind"]
|
||||
#[derive(derivative::Derivative)]
|
||||
#[derivative(Clone(bound = ""))]
|
||||
#[derivative(
|
||||
Clone(bound = ""),
|
||||
PartialOrd(bound = ""),
|
||||
PartialOrd = "feature_allow_slow_enum",
|
||||
Ord(bound = ""),
|
||||
Ord = "feature_allow_slow_enum"
|
||||
)]
|
||||
pub enum TyKind<I: Interner> {
|
||||
/// The primitive boolean type. Written as `bool`.
|
||||
Bool,
|
||||
|
|
@ -378,64 +383,6 @@ impl<I: Interner> PartialEq for TyKind<I> {
|
|||
// This is manually implemented because a derive would require `I: Eq`
|
||||
impl<I: Interner> Eq for TyKind<I> {}
|
||||
|
||||
// This is manually implemented because a derive would require `I: PartialOrd`
|
||||
impl<I: Interner> PartialOrd for TyKind<I> {
|
||||
#[inline]
|
||||
fn partial_cmp(&self, other: &TyKind<I>) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
// This is manually implemented because a derive would require `I: Ord`
|
||||
impl<I: Interner> Ord for TyKind<I> {
|
||||
#[inline]
|
||||
fn cmp(&self, other: &TyKind<I>) -> Ordering {
|
||||
tykind_discriminant(self).cmp(&tykind_discriminant(other)).then_with(|| {
|
||||
match (self, other) {
|
||||
(Int(a_i), Int(b_i)) => a_i.cmp(b_i),
|
||||
(Uint(a_u), Uint(b_u)) => a_u.cmp(b_u),
|
||||
(Float(a_f), Float(b_f)) => a_f.cmp(b_f),
|
||||
(Adt(a_d, a_s), Adt(b_d, b_s)) => a_d.cmp(b_d).then_with(|| a_s.cmp(b_s)),
|
||||
(Foreign(a_d), Foreign(b_d)) => a_d.cmp(b_d),
|
||||
(Array(a_t, a_c), Array(b_t, b_c)) => a_t.cmp(b_t).then_with(|| a_c.cmp(b_c)),
|
||||
(Slice(a_t), Slice(b_t)) => a_t.cmp(b_t),
|
||||
(RawPtr(a_t), RawPtr(b_t)) => a_t.cmp(b_t),
|
||||
(Ref(a_r, a_t, a_m), Ref(b_r, b_t, b_m)) => {
|
||||
a_r.cmp(b_r).then_with(|| a_t.cmp(b_t).then_with(|| a_m.cmp(b_m)))
|
||||
}
|
||||
(FnDef(a_d, a_s), FnDef(b_d, b_s)) => a_d.cmp(b_d).then_with(|| a_s.cmp(b_s)),
|
||||
(FnPtr(a_s), FnPtr(b_s)) => a_s.cmp(b_s),
|
||||
(Dynamic(a_p, a_r, a_repr), Dynamic(b_p, b_r, b_repr)) => {
|
||||
a_p.cmp(b_p).then_with(|| a_r.cmp(b_r).then_with(|| a_repr.cmp(b_repr)))
|
||||
}
|
||||
(Closure(a_p, a_s), Closure(b_p, b_s)) => a_p.cmp(b_p).then_with(|| a_s.cmp(b_s)),
|
||||
(Coroutine(a_d, a_s, a_m), Coroutine(b_d, b_s, b_m)) => {
|
||||
a_d.cmp(b_d).then_with(|| a_s.cmp(b_s).then_with(|| a_m.cmp(b_m)))
|
||||
}
|
||||
(
|
||||
CoroutineWitness(a_d, a_s),
|
||||
CoroutineWitness(b_d, b_s),
|
||||
) => match Ord::cmp(a_d, b_d) {
|
||||
Ordering::Equal => Ord::cmp(a_s, b_s),
|
||||
cmp => cmp,
|
||||
},
|
||||
(Tuple(a_t), Tuple(b_t)) => a_t.cmp(b_t),
|
||||
(Alias(a_i, a_p), Alias(b_i, b_p)) => a_i.cmp(b_i).then_with(|| a_p.cmp(b_p)),
|
||||
(Param(a_p), Param(b_p)) => a_p.cmp(b_p),
|
||||
(Bound(a_d, a_b), Bound(b_d, b_b)) => a_d.cmp(b_d).then_with(|| a_b.cmp(b_b)),
|
||||
(Placeholder(a_p), Placeholder(b_p)) => a_p.cmp(b_p),
|
||||
(Infer(a_t), Infer(b_t)) => a_t.cmp(b_t),
|
||||
(Error(a_e), Error(b_e)) => a_e.cmp(b_e),
|
||||
(Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => Ordering::Equal,
|
||||
_ => {
|
||||
debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = {self:?}, other = {other:?}");
|
||||
Ordering::Equal
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// This is manually implemented because a derive would require `I: Hash`
|
||||
impl<I: Interner> hash::Hash for TyKind<I> {
|
||||
fn hash<__H: hash::Hasher>(&self, state: &mut __H) -> () {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue