diff --git a/Cargo.lock b/Cargo.lock index ff21d5f8c0861..a46ab1c952020 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1011,6 +1011,17 @@ dependencies = [ "syn 2.0.29", ] +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "derive_builder" version = "0.12.0" @@ -4671,6 +4682,7 @@ name = "rustc_type_ir" version = "0.0.0" dependencies = [ "bitflags 1.3.2", + "derivative", "rustc_data_structures", "rustc_index", "rustc_macros", diff --git a/compiler/rustc_type_ir/Cargo.toml b/compiler/rustc_type_ir/Cargo.toml index c4008e9b61296..055f406942868 100644 --- a/compiler/rustc_type_ir/Cargo.toml +++ b/compiler/rustc_type_ir/Cargo.toml @@ -12,3 +12,4 @@ rustc_serialize = { path = "../rustc_serialize" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_macros = { path = "../rustc_macros" } smallvec = { version = "1.8.1", features = ["union", "may_dangle"] } +derivative = "2.2.0" diff --git a/compiler/rustc_type_ir/src/const_kind.rs b/compiler/rustc_type_ir/src/const_kind.rs index a40c41583af5c..02519c9620046 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; @@ -13,7 +12,13 @@ use crate::{ use self::ConstKind::*; /// Represents a constant in Rust. -// #[derive(derive_more::From)] +#[derive(derivative::Derivative)] +#[derivative( + PartialOrd(bound = ""), + PartialOrd = "feature_allow_slow_enum", + Ord(bound = ""), + Ord = "feature_allow_slow_enum" +)] pub enum ConstKind { /// A const generic parameter. Param(I::ParamConst), @@ -166,33 +171,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 19576ea58f1f0..5b15b30c618b2 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; @@ -118,6 +117,13 @@ use self::RegionKind::*; /// [1]: https://smallcultfollowing.com/babysteps/blog/2013/10/29/intermingled-parameter-lists/ /// [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( + 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 @@ -222,38 +228,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 b542547589ac4..b7fce5c2c0801 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}; @@ -114,6 +113,13 @@ pub enum AliasKind { /// Types written by the user start out as `hir::TyKind` and get /// converted to this representation using `AstConv::ast_ty_to_ty`. #[rustc_diagnostic_item = "IrTyKind"] +#[derive(derivative::Derivative)] +#[derivative( + 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, @@ -410,64 +416,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) -> () { @@ -614,6 +562,7 @@ impl DebugWithInfcx for TyKind { } } } + // This is manually implemented because a derive would require `I: Debug` impl fmt::Debug for TyKind { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs index f89faacc2d17b..4b12e9172af7f 100644 --- a/src/tools/tidy/src/deps.rs +++ b/src/tools/tidy/src/deps.rs @@ -142,6 +142,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[ "darling_core", "darling_macro", "datafrog", + "derivative", "derive_more", "derive_setters", "digest",