Auto merge of #83819 - AngelicosPhosphoros:issue-73338-fix-partial-eq-impl, r=Mark-Simulacrum
Optimize jumps in PartialOrd le Closes https://github.com/rust-lang/rust/issues/73338 This change stops default implementation of `le()` method of PartialOrd from generating jumps.
This commit is contained in:
commit
b1b0a1597c
@ -981,7 +981,8 @@ fn lt(&self, other: &Rhs) -> bool {
|
||||
#[must_use]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
fn le(&self, other: &Rhs) -> bool {
|
||||
matches!(self.partial_cmp(other), Some(Less | Equal))
|
||||
// Pattern `Some(Less | Eq)` optimizes worse than negating `None | Some(Greater)`.
|
||||
!matches!(self.partial_cmp(other), None | Some(Greater))
|
||||
}
|
||||
|
||||
/// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
|
||||
|
39
src/test/codegen/issue-73338-effecient-cmp.rs
Normal file
39
src/test/codegen/issue-73338-effecient-cmp.rs
Normal file
@ -0,0 +1,39 @@
|
||||
// This test checks that comparison operation
|
||||
// generated by #[derive(PartialOrd)]
|
||||
// doesn't contain jumps for C enums
|
||||
|
||||
// compile-flags: -Copt-level=3
|
||||
|
||||
#![crate_type="lib"]
|
||||
|
||||
#[repr(u32)]
|
||||
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd)]
|
||||
pub enum Foo {
|
||||
Zero,
|
||||
One,
|
||||
Two,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn compare_less(a: Foo, b: Foo)->bool{
|
||||
// CHECK-NOT: br {{.*}}
|
||||
a < b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn compare_le(a: Foo, b: Foo)->bool{
|
||||
// CHECK-NOT: br {{.*}}
|
||||
a <= b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn compare_ge(a: Foo, b: Foo)->bool{
|
||||
// CHECK-NOT: br {{.*}}
|
||||
a >= b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn compare_greater(a: Foo, b: Foo)->bool{
|
||||
// CHECK-NOT: br {{.*}}
|
||||
a > b
|
||||
}
|
Loading…
Reference in New Issue
Block a user