Skip to content

Commit

Permalink
Make Compare traits have ~const super traits.
Browse files Browse the repository at this point in the history
  • Loading branch information
onestacked committed Feb 9, 2023
1 parent ef934d9 commit 93531e5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 34 deletions.
4 changes: 2 additions & 2 deletions library/core/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,8 @@ impl dyn Any + Send + Sync {
/// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth
/// noting that the hashes and ordering will vary between Rust releases. Beware
/// of relying on them inside of your code!
#[derive(Clone, Copy, Debug, Hash, Eq)]
#[derive_const(PartialEq, PartialOrd, Ord)]
#[derive(Clone, Copy, Debug, Hash)]
#[derive_const(PartialEq, Eq, PartialOrd, Ord)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct TypeId {
t: u64,
Expand Down
74 changes: 50 additions & 24 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ pub macro PartialEq($item:item) {
#[doc(alias = "!=")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "Eq"]
pub trait Eq: PartialEq<Self> {
#[const_trait]
pub trait Eq: ~const PartialEq<Self> {
// this method is used solely by #[deriving] to assert
// that every component of a type implements #[deriving]
// itself, the current deriving infrastructure means doing this
Expand Down Expand Up @@ -329,8 +330,8 @@ pub struct AssertParamIsEq<T: Eq + ?Sized> {
/// let result = 2.cmp(&1);
/// assert_eq!(Ordering::Greater, result);
/// ```
#[derive(Clone, Copy, Eq, Debug, Hash)]
#[derive_const(PartialOrd, Ord, PartialEq)]
#[derive(Clone, Copy, Debug, Hash)]
#[derive_const(PartialOrd, Eq, Ord, PartialEq)]
#[stable(feature = "rust1", since = "1.0.0")]
#[repr(i8)]
pub enum Ordering {
Expand Down Expand Up @@ -594,11 +595,25 @@ impl Ordering {
/// v.sort_by_key(|&num| (num > 3, Reverse(num)));
/// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
/// ```
#[derive(PartialEq, Eq, Debug, Copy, Default, Hash)]
#[derive(Debug, Copy, Default, Hash)]
#[cfg_attr(not(bootstrap), derive_const(PartialEq, Eq))]
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
#[repr(transparent)]
pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T);

#[cfg(bootstrap)]
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<T: ~const PartialEq> const PartialEq for Reverse<T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
#[cfg(bootstrap)]
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<T: ~const Eq> const Eq for Reverse<T> {}

#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<T: ~const PartialOrd> const PartialOrd for Reverse<T> {
Expand Down Expand Up @@ -760,7 +775,7 @@ impl<T: Clone> Clone for Reverse<T> {
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "Ord"]
#[const_trait]
pub trait Ord: Eq + PartialOrd<Self> {
pub trait Ord: ~const Eq + ~const PartialOrd<Self> {
/// This method returns an [`Ordering`] between `self` and `other`.
///
/// By convention, `self.cmp(&other)` returns the ordering matching the expression
Expand Down Expand Up @@ -1032,7 +1047,7 @@ pub macro Ord($item:item) {
)]
#[const_trait]
#[rustc_diagnostic_item = "PartialOrd"]
pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
pub trait PartialOrd<Rhs: ?Sized = Self>: ~const PartialEq<Rhs> {
/// This method returns an ordering between `self` and `other` values if one exists.
///
/// # Examples
Expand Down Expand Up @@ -1331,7 +1346,8 @@ mod impls {
macro_rules! eq_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl Eq for $t {}
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const Eq for $t {}
)*)
}

Expand Down Expand Up @@ -1455,7 +1471,8 @@ mod impls {
}

#[unstable(feature = "never_type", issue = "35121")]
impl Eq for ! {}
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const Eq for ! {}

#[unstable(feature = "never_type", issue = "35121")]
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
Expand Down Expand Up @@ -1491,9 +1508,10 @@ mod impls {
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialOrd<&B> for &A
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<A: ?Sized, B: ?Sized> const PartialOrd<&B> for &A
where
A: PartialOrd<B>,
A: ~const PartialOrd<B>,
{
#[inline]
fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
Expand All @@ -1517,24 +1535,27 @@ mod impls {
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized> Ord for &A
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<A: ?Sized> const Ord for &A
where
A: Ord,
A: ~const Ord,
{
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(*self, *other)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized> Eq for &A where A: Eq {}
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<A: ?Sized> const Eq for &A where A: ~const Eq {}

// &mut pointers

#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &mut A
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<A: ?Sized, B: ?Sized> const PartialEq<&mut B> for &mut A
where
A: PartialEq<B>,
A: ~const PartialEq<B>,
{
#[inline]
fn eq(&self, other: &&mut B) -> bool {
Expand All @@ -1546,9 +1567,10 @@ mod impls {
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialOrd<&mut B> for &mut A
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<A: ?Sized, B: ?Sized> const PartialOrd<&mut B> for &mut A
where
A: PartialOrd<B>,
A: ~const PartialOrd<B>,
{
#[inline]
fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
Expand All @@ -1572,22 +1594,25 @@ mod impls {
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized> Ord for &mut A
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<A: ?Sized> const Ord for &mut A
where
A: Ord,
A: ~const Ord,
{
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(*self, *other)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized> Eq for &mut A where A: Eq {}
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<A: ?Sized> const Eq for &mut A where A: ~const Eq {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &A
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<A: ?Sized, B: ?Sized> const PartialEq<&mut B> for &A
where
A: PartialEq<B>,
A: ~const PartialEq<B>,
{
#[inline]
fn eq(&self, other: &&mut B) -> bool {
Expand All @@ -1600,9 +1625,10 @@ mod impls {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &mut A
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<A: ?Sized, B: ?Sized> const PartialEq<&B> for &mut A
where
A: PartialEq<B>,
A: ~const PartialEq<B>,
{
#[inline]
fn eq(&self, other: &&B) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/ptr/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::{cmp, fmt, hash, mem, num};
/// Note that particularly large alignments, while representable in this type,
/// are likely not to be supported by actual allocators and linkers.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[derive(Copy, Clone, Eq)]
#[derive_const(PartialEq)]
#[derive(Copy, Clone)]
#[derive_const(PartialEq, Eq)]
#[repr(transparent)]
pub struct Alignment(AlignmentEnum);

Expand Down
5 changes: 3 additions & 2 deletions library/core/src/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ macro_rules! tuple_impls {
maybe_tuple_doc! {
$($T)+ @
#[stable(feature = "rust1", since = "1.0.0")]
impl<$($T: Eq),+> Eq for ($($T,)+)
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<$($T: ~const Eq),+> const Eq for ($($T,)+)
where
last_type!($($T,)+): ?Sized
{}
Expand All @@ -51,7 +52,7 @@ macro_rules! tuple_impls {
$($T)+ @
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<$($T: ~const PartialOrd + ~const PartialEq),+> const PartialOrd for ($($T,)+)
impl<$($T: ~const PartialOrd),+> const PartialOrd for ($($T,)+)
where
last_type!($($T,)+): ?Sized
{
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/consts/issue-25826.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0277]: can't compare `*const ()` with `*const ()` in const contexts
error[E0277]: can't compare `*const ()` with `_` in const contexts
--> $DIR/issue-25826.rs:3:52
|
LL | const A: bool = unsafe { id::<u8> as *const () < id::<u16> as *const () };
| ^ no implementation for `*const () < *const ()` and `*const () > *const ()`
| ^ no implementation for `*const () < _` and `*const () > _`
|
= help: the trait `~const PartialOrd` is not implemented for `*const ()`
note: the trait `PartialOrd` is implemented for `*const ()`, but that implementation is not `const`
= help: the trait `~const PartialOrd<_>` is not implemented for `*const ()`
note: the trait `PartialOrd<_>` is implemented for `*const ()`, but that implementation is not `const`
--> $DIR/issue-25826.rs:3:52
|
LL | const A: bool = unsafe { id::<u8> as *const () < id::<u16> as *const () };
Expand Down

0 comments on commit 93531e5

Please sign in to comment.