Skip to content

Commit b1b0a15

Browse files
committed
Auto merge of #83819 - AngelicosPhosphoros:issue-73338-fix-partial-eq-impl, r=Mark-Simulacrum
Optimize jumps in PartialOrd le Closes #73338 This change stops default implementation of `le()` method of PartialOrd from generating jumps.
2 parents 015d2bc + ed0d8fa commit b1b0a15

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

library/core/src/cmp.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,8 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
981981
#[must_use]
982982
#[stable(feature = "rust1", since = "1.0.0")]
983983
fn le(&self, other: &Rhs) -> bool {
984-
matches!(self.partial_cmp(other), Some(Less | Equal))
984+
// Pattern `Some(Less | Eq)` optimizes worse than negating `None | Some(Greater)`.
985+
!matches!(self.partial_cmp(other), None | Some(Greater))
985986
}
986987

987988
/// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This test checks that comparison operation
2+
// generated by #[derive(PartialOrd)]
3+
// doesn't contain jumps for C enums
4+
5+
// compile-flags: -Copt-level=3
6+
7+
#![crate_type="lib"]
8+
9+
#[repr(u32)]
10+
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd)]
11+
pub enum Foo {
12+
Zero,
13+
One,
14+
Two,
15+
}
16+
17+
#[no_mangle]
18+
pub fn compare_less(a: Foo, b: Foo)->bool{
19+
// CHECK-NOT: br {{.*}}
20+
a < b
21+
}
22+
23+
#[no_mangle]
24+
pub fn compare_le(a: Foo, b: Foo)->bool{
25+
// CHECK-NOT: br {{.*}}
26+
a <= b
27+
}
28+
29+
#[no_mangle]
30+
pub fn compare_ge(a: Foo, b: Foo)->bool{
31+
// CHECK-NOT: br {{.*}}
32+
a >= b
33+
}
34+
35+
#[no_mangle]
36+
pub fn compare_greater(a: Foo, b: Foo)->bool{
37+
// CHECK-NOT: br {{.*}}
38+
a > b
39+
}

0 commit comments

Comments
 (0)