Skip to content

Commit c59e93c

Browse files
committed
Address PR feedback
1 parent 8d5977d commit c59e93c

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

Diff for: compiler/rustc_middle/src/mir/syntax.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,14 @@ pub enum BinOp {
14451445
/// The `>` operator (greater than)
14461446
Gt,
14471447
/// The `<=>` operator (three-way comparison, like `Ord::cmp`)
1448+
///
1449+
/// This is supported only on the integer types and `char`, always returning
1450+
/// [`rustc_hir::LangItem::OrderingEnum`] (aka [`std::cmp::Ordering`]).
1451+
///
1452+
/// [`Rvalue::BinaryOp`]`(BinOp::Cmp, A, B)` returns
1453+
/// - `Ordering::Less` (`-1_i8`, as a Scalar) if `A < B`
1454+
/// - `Ordering::Equal` (`0_i8`, as a Scalar) if `A == B`
1455+
/// - `Ordering::Greater` (`+1_i8`, as a Scalar) if `A > B`
14481456
Cmp,
14491457
/// The `ptr.offset` operator
14501458
Offset,

Diff for: library/core/src/cmp.rs

+3
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ pub struct AssertParamIsEq<T: Eq + ?Sized> {
376376
/// ```
377377
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
378378
#[stable(feature = "rust1", since = "1.0.0")]
379+
// This is a lang item only so that `BinOp::Cmp` in MIR can return it.
380+
// It has no special behaviour, but does require that the three variants
381+
// `Less`/`Equal`/`Greater` remain `-1_i8`/`0_i8`/`+1_i8` respectively.
379382
#[cfg_attr(not(bootstrap), lang = "Ordering")]
380383
#[repr(i8)]
381384
pub enum Ordering {

Diff for: library/core/tests/intrinsics.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,26 @@ fn test_const_deallocate_at_runtime() {
103103
#[cfg(not(bootstrap))]
104104
#[test]
105105
fn test_three_way_compare_in_const_contexts() {
106-
use core::cmp::Ordering::*;
106+
use core::cmp::Ordering::{self, *};
107107
use core::intrinsics::three_way_compare;
108108

109-
const {
110-
assert!(Less as i8 == three_way_compare(123_u16, 456) as _);
111-
assert!(Equal as i8 == three_way_compare(456_u16, 456) as _);
112-
assert!(Greater as i8 == three_way_compare(789_u16, 456) as _);
113-
assert!(Less as i8 == three_way_compare('A', 'B') as _);
114-
assert!(Equal as i8 == three_way_compare('B', 'B') as _);
115-
assert!(Greater as i8 == three_way_compare('C', 'B') as _);
116-
assert!(Less as i8 == three_way_compare(-123_i16, 456) as _);
117-
assert!(Equal as i8 == three_way_compare(456_i16, 456) as _);
118-
assert!(Greater as i8 == three_way_compare(123_i16, -456) as _);
119-
}
109+
const UNSIGNED_LESS: Ordering = three_way_compare(123_u16, 456);
110+
const UNSIGNED_EQUAL: Ordering = three_way_compare(456_u16, 456);
111+
const UNSIGNED_GREATER: Ordering = three_way_compare(789_u16, 456);
112+
const CHAR_LESS: Ordering = three_way_compare('A', 'B');
113+
const CHAR_EQUAL: Ordering = three_way_compare('B', 'B');
114+
const CHAR_GREATER: Ordering = three_way_compare('C', 'B');
115+
const SIGNED_LESS: Ordering = three_way_compare(123_i64, 456);
116+
const SIGNED_EQUAL: Ordering = three_way_compare(456_i64, 456);
117+
const SIGNED_GREATER: Ordering = three_way_compare(789_i64, 456);
118+
119+
assert_eq!(UNSIGNED_LESS, Less);
120+
assert_eq!(UNSIGNED_EQUAL, Equal);
121+
assert_eq!(UNSIGNED_GREATER, Greater);
122+
assert_eq!(CHAR_LESS, Less);
123+
assert_eq!(CHAR_EQUAL, Equal);
124+
assert_eq!(CHAR_GREATER, Greater);
125+
assert_eq!(SIGNED_LESS, Less);
126+
assert_eq!(SIGNED_EQUAL, Equal);
127+
assert_eq!(SIGNED_GREATER, Greater);
120128
}

0 commit comments

Comments
 (0)