Skip to content

Commit

Permalink
Rollup merge of rust-lang#92390 - fee1-dead-contrib:const_cmp, r=oli-obk
Browse files Browse the repository at this point in the history
Constify a few `(Partial)Ord` impls

Only a few `impl`s are constified for now, as rust-lang#92257 has not landed in the bootstrap compiler yet and quite a few impls would need that fix.

This unblocks rust-lang#92228, which unblocks marking iterator methods as `default_method_body_is_const`.
  • Loading branch information
Dylan-DPC authored Jul 25, 2022
2 parents dc2d232 + 65fca6d commit 5509e42
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
74 changes: 55 additions & 19 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#![stable(feature = "rust1", since = "1.0.0")]

use crate::marker::Destruct;

use self::Ordering::*;

/// Trait for equality comparisons which are [partial equivalence
Expand Down Expand Up @@ -603,7 +605,8 @@ impl Ordering {
pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T);

#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
impl<T: PartialOrd> PartialOrd for Reverse<T> {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<T: ~const PartialOrd> const PartialOrd for Reverse<T> {
#[inline]
fn partial_cmp(&self, other: &Reverse<T>) -> Option<Ordering> {
other.0.partial_cmp(&self.0)
Expand Down Expand Up @@ -761,6 +764,7 @@ impl<T: Clone> Clone for Reverse<T> {
#[doc(alias = ">=")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "Ord"]
#[const_trait]
pub trait Ord: Eq + PartialOrd<Self> {
/// This method returns an [`Ordering`] between `self` and `other`.
///
Expand Down Expand Up @@ -796,8 +800,15 @@ pub trait Ord: Eq + PartialOrd<Self> {
fn max(self, other: Self) -> Self
where
Self: Sized,
Self: ~const Destruct,
{
max_by(self, other, Ord::cmp)
// HACK(fee1-dead): go back to using `self.max_by(other, Ord::cmp)`
// when trait methods are allowed to be used when a const closure is
// expected.
match self.cmp(&other) {
Ordering::Less | Ordering::Equal => other,
Ordering::Greater => self,
}
}

/// Compares and returns the minimum of two values.
Expand All @@ -816,8 +827,15 @@ pub trait Ord: Eq + PartialOrd<Self> {
fn min(self, other: Self) -> Self
where
Self: Sized,
Self: ~const Destruct,
{
min_by(self, other, Ord::cmp)
// HACK(fee1-dead): go back to using `self.min_by(other, Ord::cmp)`
// when trait methods are allowed to be used when a const closure is
// expected.
match self.cmp(&other) {
Ordering::Less | Ordering::Equal => self,
Ordering::Greater => other,
}
}

/// Restrict a value to a certain interval.
Expand All @@ -841,6 +859,8 @@ pub trait Ord: Eq + PartialOrd<Self> {
fn clamp(self, min: Self, max: Self) -> Self
where
Self: Sized,
Self: ~const Destruct,
Self: ~const PartialOrd,
{
assert!(min <= max);
if self < min {
Expand All @@ -862,15 +882,17 @@ pub macro Ord($item:item) {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for Ordering {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const Ord for Ordering {
#[inline]
fn cmp(&self, other: &Ordering) -> Ordering {
(*self as i32).cmp(&(*other as i32))
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for Ordering {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const PartialOrd for Ordering {
#[inline]
fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> {
(*self as i32).partial_cmp(&(*other as i32))
Expand Down Expand Up @@ -1187,8 +1209,9 @@ pub macro PartialOrd($item:item) {
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
#[cfg_attr(not(test), rustc_diagnostic_item = "cmp_min")]
pub fn min<T: Ord>(v1: T, v2: T) -> T {
pub const fn min<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
v1.min(v2)
}

Expand Down Expand Up @@ -1250,8 +1273,9 @@ pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
#[cfg_attr(not(test), rustc_diagnostic_item = "cmp_max")]
pub fn max<T: Ord>(v1: T, v2: T) -> T {
pub const fn max<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
v1.max(v2)
}

Expand Down Expand Up @@ -1304,7 +1328,8 @@ mod impls {
macro_rules! partial_eq_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for $t {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const PartialEq for $t {
#[inline]
fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
#[inline]
Expand All @@ -1314,7 +1339,8 @@ mod impls {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for () {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const PartialEq for () {
#[inline]
fn eq(&self, _other: &()) -> bool {
true
Expand All @@ -1341,7 +1367,8 @@ mod impls {
macro_rules! partial_ord_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for $t {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const PartialOrd for $t {
#[inline]
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
match (*self <= *other, *self >= *other) {
Expand All @@ -1364,15 +1391,17 @@ mod impls {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for () {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const PartialOrd for () {
#[inline]
fn partial_cmp(&self, _: &()) -> Option<Ordering> {
Some(Equal)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for bool {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const PartialOrd for bool {
#[inline]
fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
Some(self.cmp(other))
Expand All @@ -1384,7 +1413,8 @@ mod impls {
macro_rules! ord_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for $t {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const PartialOrd for $t {
#[inline]
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
Some(self.cmp(other))
Expand All @@ -1400,7 +1430,8 @@ mod impls {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for $t {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const Ord for $t {
#[inline]
fn cmp(&self, other: &$t) -> Ordering {
// The order here is important to generate more optimal assembly.
Expand All @@ -1414,15 +1445,17 @@ mod impls {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for () {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const Ord for () {
#[inline]
fn cmp(&self, _other: &()) -> Ordering {
Equal
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for bool {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const Ord for bool {
#[inline]
fn cmp(&self, other: &bool) -> Ordering {
// Casting to i8's and converting the difference to an Ordering generates
Expand All @@ -1441,7 +1474,8 @@ mod impls {
ord_impl! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }

#[unstable(feature = "never_type", issue = "35121")]
impl PartialEq for ! {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const PartialEq for ! {
fn eq(&self, _: &!) -> bool {
*self
}
Expand All @@ -1451,14 +1485,16 @@ mod impls {
impl Eq for ! {}

#[unstable(feature = "never_type", issue = "35121")]
impl PartialOrd for ! {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const PartialOrd for ! {
fn partial_cmp(&self, _: &!) -> Option<Ordering> {
*self
}
}

#[unstable(feature = "never_type", issue = "35121")]
impl Ord for ! {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const Ord for ! {
fn cmp(&self, _: &!) -> Ordering {
*self
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
#![feature(const_cell_into_inner)]
#![feature(const_char_convert)]
#![feature(const_clone)]
#![feature(const_cmp)]
#![feature(const_discriminant)]
#![feature(const_eval_select)]
#![feature(const_float_bits_conv)]
Expand Down

0 comments on commit 5509e42

Please sign in to comment.