Derive `PartialOrd` via `Ord` when deriving both on a concrete type
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Today, if you do https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=1ba35655ac2a5a415dc94df6a2b04988
#[derive(Copy, Clone)]
struct Foo(i32, u32);
it expands to
#[automatically_derived]
impl ::core::marker::Copy for Foo { }
#[automatically_derived]
impl ::core::clone::Clone for Foo {
#[inline]
fn clone(&self) -> Foo {
let _: ::core::clone::AssertParamIsClone<i32>;
let _: ::core::clone::AssertParamIsClone<u32>;
*self
}
}
That's wonderful -- it's simpler than a bunch of clone calls, and likely codegens better too.
However, if you try
#[derive(Ord, PartialOrd)]
struct Foo(i32, u32);
you just get
#[automatically_derived]
impl ::core::cmp::Ord for Foo {
#[inline]
fn cmp(&self, other: &Foo) -> ::core::cmp::Ordering {
match ::core::cmp::Ord::cmp(&self.0, &other.0) {
::core::cmp::Ordering::Equal =>
::core::cmp::Ord::cmp(&self.1, &other.1),
cmp => cmp,
}
}
}
#[automatically_derived]
impl ::core::cmp::PartialOrd for Foo {
#[inline]
fn partial_cmp(&self, other: &Foo)
-> ::core::option::Option<::core::cmp::Ordering> {
match ::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0) {
::core::option::Option::Some(::core::cmp::Ordering::Equal) =>
::core::cmp::PartialOrd::partial_cmp(&self.1, &other.1),
cmp => cmp,
}
}
}
which is unfortunate, because all those options take more time to compile and are harder for backends to optimize away.
It would be nice if, instead, it expanded to something like
#[automatically_derived]
impl ::core::cmp::Ord for Foo {
#[inline]
fn cmp(&self, other: &Foo) -> ::core::cmp::Ordering {
match ::core::cmp::Ord::cmp(&self.0, &other.0) {
::core::cmp::Ordering::Equal =>
::core::cmp::Ord::cmp(&self.1, &other.1),
cmp => cmp,
}
}
}
#[automatically_derived]
impl ::core::cmp::PartialOrd for Foo {
#[inline]
fn partial_cmp(&self, other: &Foo)
-> ::core::option::Option<::core::cmp::Ordering> {
let _: ::core::cmp::AssertParamIsPartialOrd<i32>;
let _: ::core::cmp::AssertParamIsPartialOrd<u32>;
Some(::core::cmp::Ord::cmp(self, other))
}
}
Re-using the logic from Ord and making it easier for the optimizer to know that Foo::partial_cmp never actually returns None.
Like with Clone, this must only be done for fully concrete (non-generic) types, where the fact that Ord is also derived is enough to guarantee that we can call Ord from PartialOrd (as there will be a compiler error in the derived Ord if not).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the linked Rust Playground example and comparing the derived Ord and PartialOrd implementations shown in the issue. Done means concrete types deriving both traits reuse Ord::cmp for PartialOrd while preserving the required trait-bound checks and behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100