Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm committed Mar 25, 2024
1 parent 8d5977d commit c59e93c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/mir/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,14 @@ pub enum BinOp {
/// The `>` operator (greater than)
Gt,
/// The `<=>` operator (three-way comparison, like `Ord::cmp`)
///
/// This is supported only on the integer types and `char`, always returning
/// [`rustc_hir::LangItem::OrderingEnum`] (aka [`std::cmp::Ordering`]).
///
/// [`Rvalue::BinaryOp`]`(BinOp::Cmp, A, B)` returns
/// - `Ordering::Less` (`-1_i8`, as a Scalar) if `A < B`
/// - `Ordering::Equal` (`0_i8`, as a Scalar) if `A == B`
/// - `Ordering::Greater` (`+1_i8`, as a Scalar) if `A > B`
Cmp,
/// The `ptr.offset` operator
Offset,
Expand Down
3 changes: 3 additions & 0 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ pub struct AssertParamIsEq<T: Eq + ?Sized> {
/// ```
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
#[stable(feature = "rust1", since = "1.0.0")]
// This is a lang item only so that `BinOp::Cmp` in MIR can return it.
// It has no special behaviour, but does require that the three variants
// `Less`/`Equal`/`Greater` remain `-1_i8`/`0_i8`/`+1_i8` respectively.
#[cfg_attr(not(bootstrap), lang = "Ordering")]
#[repr(i8)]
pub enum Ordering {
Expand Down
32 changes: 20 additions & 12 deletions library/core/tests/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,26 @@ fn test_const_deallocate_at_runtime() {
#[cfg(not(bootstrap))]
#[test]
fn test_three_way_compare_in_const_contexts() {
use core::cmp::Ordering::*;
use core::cmp::Ordering::{self, *};
use core::intrinsics::three_way_compare;

const {
assert!(Less as i8 == three_way_compare(123_u16, 456) as _);
assert!(Equal as i8 == three_way_compare(456_u16, 456) as _);
assert!(Greater as i8 == three_way_compare(789_u16, 456) as _);
assert!(Less as i8 == three_way_compare('A', 'B') as _);
assert!(Equal as i8 == three_way_compare('B', 'B') as _);
assert!(Greater as i8 == three_way_compare('C', 'B') as _);
assert!(Less as i8 == three_way_compare(-123_i16, 456) as _);
assert!(Equal as i8 == three_way_compare(456_i16, 456) as _);
assert!(Greater as i8 == three_way_compare(123_i16, -456) as _);
}
const UNSIGNED_LESS: Ordering = three_way_compare(123_u16, 456);
const UNSIGNED_EQUAL: Ordering = three_way_compare(456_u16, 456);
const UNSIGNED_GREATER: Ordering = three_way_compare(789_u16, 456);
const CHAR_LESS: Ordering = three_way_compare('A', 'B');
const CHAR_EQUAL: Ordering = three_way_compare('B', 'B');
const CHAR_GREATER: Ordering = three_way_compare('C', 'B');
const SIGNED_LESS: Ordering = three_way_compare(123_i64, 456);
const SIGNED_EQUAL: Ordering = three_way_compare(456_i64, 456);
const SIGNED_GREATER: Ordering = three_way_compare(789_i64, 456);

assert_eq!(UNSIGNED_LESS, Less);
assert_eq!(UNSIGNED_EQUAL, Equal);
assert_eq!(UNSIGNED_GREATER, Greater);
assert_eq!(CHAR_LESS, Less);
assert_eq!(CHAR_EQUAL, Equal);
assert_eq!(CHAR_GREATER, Greater);
assert_eq!(SIGNED_LESS, Less);
assert_eq!(SIGNED_EQUAL, Equal);
assert_eq!(SIGNED_GREATER, Greater);
}

0 comments on commit c59e93c

Please sign in to comment.