Skip to content

Use derivative for PartialOrd/Ord in rustc_type_ir #117409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_type_ir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
36 changes: 7 additions & 29 deletions compiler/rustc_type_ir/src/const_kind.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<I: Interner> {
/// A const generic parameter.
Param(I::ParamConst),
Expand Down Expand Up @@ -166,33 +171,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) {
Expand Down
40 changes: 7 additions & 33 deletions compiler/rustc_type_ir/src/region_kind.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<I: Interner> {
/// Region bound in a type or fn declaration which will be
/// substituted 'early' -- that is, at the same time when type
Expand Down Expand Up @@ -222,38 +228,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(&regionkind_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) -> () {
Expand Down
67 changes: 8 additions & 59 deletions compiler/rustc_type_ir/src/ty_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<I: Interner> {
/// The primitive boolean type. Written as `bool`.
Bool,
Expand Down Expand Up @@ -410,64 +416,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) -> () {
Expand Down Expand Up @@ -614,6 +562,7 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
}
}
}

// This is manually implemented because a derive would require `I: Debug`
impl<I: Interner> fmt::Debug for TyKind<I> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
1 change: 1 addition & 0 deletions src/tools/tidy/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"darling_core",
"darling_macro",
"datafrog",
"derivative",
"derive_more",
"derive_setters",
"digest",
Expand Down