]> git.lizzy.rs Git - rust.git/commit
Auto merge of #103659 - clubby789:improve-partialord-derive, r=nagisa
authorbors <bors@rust-lang.org>
Sat, 28 Jan 2023 22:11:11 +0000 (22:11 +0000)
committerbors <bors@rust-lang.org>
Sat, 28 Jan 2023 22:11:11 +0000 (22:11 +0000)
commit9f82651a5fa4b1d96f55ce5507dd2aa204c7fb61
treeabfc4e7c71dc1de1731ca4f985203059d644078b
parent1e225413a21fa69570bd3fefea9eb05e33f8b917
parent2883148e60603ce42ed296b9d9f1913083748f3a
Auto merge of #103659 - clubby789:improve-partialord-derive, r=nagisa

Special-case deriving `PartialOrd` for enums with dataless variants

I was able to get slightly better codegen by flipping the derived `PartialOrd` logic for two-variant enums.  I also tried to document the implementation of the derive macro to make the special-case logic a little clearer.
```rs
#[derive(PartialEq, PartialOrd)]
pub enum A<T> {
    A,
    B(T)
}
```
```diff
impl<T: ::core::cmp::PartialOrd> ::core::cmp::PartialOrd for A<T> {
   #[inline]
   fn partial_cmp(
       &self,
       other: &A<T>,
   ) -> ::core::option::Option<::core::cmp::Ordering> {
       let __self_tag = ::core::intrinsics::discriminant_value(self);
       let __arg1_tag = ::core::intrinsics::discriminant_value(other);
-      match ::core::cmp::PartialOrd::partial_cmp(&__self_tag, &__arg1_tag) {
-          ::core::option::Option::Some(::core::cmp::Ordering::Equal) => {
-              match (self, other) {
-                  (A::B(__self_0), A::B(__arg1_0)) => {
-                      ::core::cmp::PartialOrd::partial_cmp(__self_0, __arg1_0)
-                  }
-                  _ => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
-              }
+      match (self, other) {
+          (A::B(__self_0), A::B(__arg1_0)) => {
+              ::core::cmp::PartialOrd::partial_cmp(__self_0, __arg1_0)
           }
-          cmp => cmp,
+          _ => ::core::cmp::PartialOrd::partial_cmp(&__self_tag, &__arg1_tag),
       }
   }
}
```
Godbolt: [Current](https://godbolt.org/z/GYjEzG1T8), [New](https://godbolt.org/z/GoK78qx15)
I'm not sure how common a case comparing two enums like this (such as `Option`) is, and if it's worth the slowdown of adding a special case to the derive. If it causes overall regressions it might be worth just manually implementing this for `Option`.
compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
tests/ui/deriving/deriving-all-codegen.stdout