From 8eb932dcf07753f532c12ae880359f8f392728b0 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 30 Oct 2023 17:22:55 +0000 Subject: [PATCH] Use derivative for PartialOrd/ord --- compiler/rustc_type_ir/src/const_kind.rs | 36 +++--------- compiler/rustc_type_ir/src/region_kind.rs | 41 +++----------- compiler/rustc_type_ir/src/ty_kind.rs | 67 +++-------------------- 3 files changed, 21 insertions(+), 123 deletions(-) diff --git a/compiler/rustc_type_ir/src/const_kind.rs b/compiler/rustc_type_ir/src/const_kind.rs index 3e0162328eefb..3c986f64d38d7 100644 --- a/compiler/rustc_type_ir/src/const_kind.rs +++ b/compiler/rustc_type_ir/src/const_kind.rs @@ -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 { /// A const generic parameter. Param(I::ParamConst), @@ -167,33 +172,6 @@ where } } -impl PartialOrd for ConstKind { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -impl Ord for ConstKind { - 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 PartialEq for ConstKind { fn eq(&self, other: &Self) -> bool { match (self, other) { diff --git a/compiler/rustc_type_ir/src/region_kind.rs b/compiler/rustc_type_ir/src/region_kind.rs index 66115adefeaf1..8dc1cc0800584 100644 --- a/compiler/rustc_type_ir/src/region_kind.rs +++ b/compiler/rustc_type_ir/src/region_kind.rs @@ -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 { /// 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 PartialEq for RegionKind { // This is manually implemented because a derive would require `I: Eq` impl Eq for RegionKind {} -// This is manually implemented because a derive would require `I: PartialOrd` -impl PartialOrd for RegionKind { - #[inline] - fn partial_cmp(&self, other: &RegionKind) -> Option { - Some(self.cmp(other)) - } -} - -// This is manually implemented because a derive would require `I: Ord` -impl Ord for RegionKind { - #[inline] - fn cmp(&self, other: &RegionKind) -> 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 hash::Hash for RegionKind { fn hash(&self, state: &mut H) -> () { diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index 39116d33c3634..f180ceb7b0710 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -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 { /// The primitive boolean type. Written as `bool`. Bool, @@ -378,64 +383,6 @@ impl PartialEq for TyKind { // This is manually implemented because a derive would require `I: Eq` impl Eq for TyKind {} -// This is manually implemented because a derive would require `I: PartialOrd` -impl PartialOrd for TyKind { - #[inline] - fn partial_cmp(&self, other: &TyKind) -> Option { - Some(self.cmp(other)) - } -} - -// This is manually implemented because a derive would require `I: Ord` -impl Ord for TyKind { - #[inline] - fn cmp(&self, other: &TyKind) -> 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 hash::Hash for TyKind { fn hash<__H: hash::Hasher>(&self, state: &mut __H) -> () {