Skip to content

Commit 5dbc503

Browse files
committed
Avoid is_some_and so the generic MIR is simpler
1 parent 9cc52bc commit 5dbc503

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

library/core/src/cmp.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ pub enum Ordering {
397397
}
398398

399399
impl Ordering {
400+
#[inline]
401+
const fn as_raw(self) -> i8 {
402+
// FIXME(const-hack): just use `PartialOrd` against `Equal` once that's const
403+
crate::intrinsics::discriminant_value(&self)
404+
}
405+
400406
/// Returns `true` if the ordering is the `Equal` variant.
401407
///
402408
/// # Examples
@@ -413,7 +419,7 @@ impl Ordering {
413419
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
414420
#[stable(feature = "ordering_helpers", since = "1.53.0")]
415421
pub const fn is_eq(self) -> bool {
416-
matches!(self, Equal)
422+
self.as_raw() == 0
417423
}
418424

419425
/// Returns `true` if the ordering is not the `Equal` variant.
@@ -432,7 +438,7 @@ impl Ordering {
432438
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
433439
#[stable(feature = "ordering_helpers", since = "1.53.0")]
434440
pub const fn is_ne(self) -> bool {
435-
!matches!(self, Equal)
441+
self.as_raw() != 0
436442
}
437443

438444
/// Returns `true` if the ordering is the `Less` variant.
@@ -451,7 +457,7 @@ impl Ordering {
451457
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
452458
#[stable(feature = "ordering_helpers", since = "1.53.0")]
453459
pub const fn is_lt(self) -> bool {
454-
matches!(self, Less)
460+
self.as_raw() < 0
455461
}
456462

457463
/// Returns `true` if the ordering is the `Greater` variant.
@@ -470,7 +476,7 @@ impl Ordering {
470476
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
471477
#[stable(feature = "ordering_helpers", since = "1.53.0")]
472478
pub const fn is_gt(self) -> bool {
473-
matches!(self, Greater)
479+
self.as_raw() > 0
474480
}
475481

476482
/// Returns `true` if the ordering is either the `Less` or `Equal` variant.
@@ -489,7 +495,7 @@ impl Ordering {
489495
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
490496
#[stable(feature = "ordering_helpers", since = "1.53.0")]
491497
pub const fn is_le(self) -> bool {
492-
!matches!(self, Greater)
498+
self.as_raw() <= 0
493499
}
494500

495501
/// Returns `true` if the ordering is either the `Greater` or `Equal` variant.
@@ -508,7 +514,7 @@ impl Ordering {
508514
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
509515
#[stable(feature = "ordering_helpers", since = "1.53.0")]
510516
pub const fn is_ge(self) -> bool {
511-
!matches!(self, Less)
517+
self.as_raw() >= 0
512518
}
513519

514520
/// Reverses the `Ordering`.
@@ -1369,7 +1375,9 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
13691375
#[stable(feature = "rust1", since = "1.0.0")]
13701376
#[rustc_diagnostic_item = "cmp_partialord_lt"]
13711377
fn lt(&self, other: &Rhs) -> bool {
1372-
self.partial_cmp(other).is_some_and(Ordering::is_lt)
1378+
// FIXME(#137901): ideally these would just use `is_some_and`,
1379+
// but at the time of writing that doesn't inline in the generic MIR.
1380+
if let Some(c) = self.partial_cmp(other) { c.is_lt() } else { false }
13731381
}
13741382

13751383
/// Tests less than or equal to (for `self` and `other`) and is used by the
@@ -1387,7 +1395,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
13871395
#[stable(feature = "rust1", since = "1.0.0")]
13881396
#[rustc_diagnostic_item = "cmp_partialord_le"]
13891397
fn le(&self, other: &Rhs) -> bool {
1390-
self.partial_cmp(other).is_some_and(Ordering::is_le)
1398+
if let Some(c) = self.partial_cmp(other) { c.is_le() } else { false }
13911399
}
13921400

13931401
/// Tests greater than (for `self` and `other`) and is used by the `>`
@@ -1405,7 +1413,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
14051413
#[stable(feature = "rust1", since = "1.0.0")]
14061414
#[rustc_diagnostic_item = "cmp_partialord_gt"]
14071415
fn gt(&self, other: &Rhs) -> bool {
1408-
self.partial_cmp(other).is_some_and(Ordering::is_gt)
1416+
if let Some(c) = self.partial_cmp(other) { c.is_gt() } else { false }
14091417
}
14101418

14111419
/// Tests greater than or equal to (for `self` and `other`) and is used by
@@ -1423,7 +1431,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
14231431
#[stable(feature = "rust1", since = "1.0.0")]
14241432
#[rustc_diagnostic_item = "cmp_partialord_ge"]
14251433
fn ge(&self, other: &Rhs) -> bool {
1426-
self.partial_cmp(other).is_some_and(Ordering::is_ge)
1434+
if let Some(c) = self.partial_cmp(other) { c.is_ge() } else { false }
14271435
}
14281436
}
14291437

tests/mir-opt/pre-codegen/derived_ord.demo_le.PreCodegen.after.mir

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,34 @@ fn demo_le(_1: &MultiField, _2: &MultiField) -> bool {
66
let mut _0: bool;
77
scope 1 (inlined <MultiField as PartialOrd>::le) {
88
let mut _11: std::option::Option<std::cmp::Ordering>;
9-
scope 2 (inlined Option::<std::cmp::Ordering>::is_some_and::<fn(std::cmp::Ordering) -> bool {std::cmp::Ordering::is_le}>) {
9+
scope 2 {
1010
let _12: std::cmp::Ordering;
11-
let mut _13: (std::cmp::Ordering,);
12-
scope 3 {
11+
scope 3 (inlined std::cmp::Ordering::is_le) {
12+
let mut _13: i8;
13+
scope 4 (inlined std::cmp::Ordering::as_raw) {
14+
}
1315
}
14-
}
15-
scope 4 (inlined <MultiField as PartialOrd>::partial_cmp) {
16-
let mut _6: std::option::Option<std::cmp::Ordering>;
17-
let mut _7: i8;
18-
scope 5 {
19-
}
20-
scope 6 (inlined std::cmp::impls::<impl PartialOrd for char>::partial_cmp) {
21-
let mut _3: char;
22-
let mut _4: char;
23-
let mut _5: std::cmp::Ordering;
24-
}
25-
scope 7 (inlined std::cmp::impls::<impl PartialOrd for i16>::partial_cmp) {
26-
let mut _8: i16;
27-
let mut _9: i16;
28-
let mut _10: std::cmp::Ordering;
16+
scope 5 (inlined <MultiField as PartialOrd>::partial_cmp) {
17+
let mut _6: std::option::Option<std::cmp::Ordering>;
18+
let mut _7: i8;
19+
scope 6 {
20+
}
21+
scope 7 (inlined std::cmp::impls::<impl PartialOrd for char>::partial_cmp) {
22+
let mut _3: char;
23+
let mut _4: char;
24+
let mut _5: std::cmp::Ordering;
25+
}
26+
scope 8 (inlined std::cmp::impls::<impl PartialOrd for i16>::partial_cmp) {
27+
let mut _8: i16;
28+
let mut _9: i16;
29+
let mut _10: std::cmp::Ordering;
30+
}
2931
}
3032
}
3133
}
3234

3335
bb0: {
36+
StorageLive(_12);
3437
StorageLive(_11);
3538
StorageLive(_5);
3639
StorageLive(_7);
@@ -59,29 +62,24 @@ fn demo_le(_1: &MultiField, _2: &MultiField) -> bool {
5962
StorageDead(_10);
6063
StorageDead(_7);
6164
StorageDead(_5);
62-
StorageLive(_12);
6365
goto -> bb3;
6466
}
6567

6668
bb2: {
6769
_11 = copy _6;
6870
StorageDead(_7);
6971
StorageDead(_5);
70-
StorageLive(_12);
7172
goto -> bb3;
7273
}
7374

7475
bb3: {
75-
_12 = move ((_11 as Some).0: std::cmp::Ordering);
76+
_12 = copy ((_11 as Some).0: std::cmp::Ordering);
7677
StorageLive(_13);
77-
_13 = (copy _12,);
78-
_0 = <fn(std::cmp::Ordering) -> bool {std::cmp::Ordering::is_le} as FnOnce<(std::cmp::Ordering,)>>::call_once(std::cmp::Ordering::is_le, move _13) -> [return: bb4, unwind unreachable];
79-
}
80-
81-
bb4: {
78+
_13 = discriminant(_12);
79+
_0 = Le(move _13, const 0_i8);
8280
StorageDead(_13);
83-
StorageDead(_12);
8481
StorageDead(_11);
82+
StorageDead(_12);
8583
return;
8684
}
8785
}

0 commit comments

Comments
 (0)