Skip to content

Commit 93531e5

Browse files
committed
Make Compare traits have ~const super traits.
1 parent ef934d9 commit 93531e5

File tree

5 files changed

+61
-34
lines changed

5 files changed

+61
-34
lines changed

library/core/src/any.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,8 @@ impl dyn Any + Send + Sync {
662662
/// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth
663663
/// noting that the hashes and ordering will vary between Rust releases. Beware
664664
/// of relying on them inside of your code!
665-
#[derive(Clone, Copy, Debug, Hash, Eq)]
666-
#[derive_const(PartialEq, PartialOrd, Ord)]
665+
#[derive(Clone, Copy, Debug, Hash)]
666+
#[derive_const(PartialEq, Eq, PartialOrd, Ord)]
667667
#[stable(feature = "rust1", since = "1.0.0")]
668668
pub struct TypeId {
669669
t: u64,

library/core/src/cmp.rs

+50-24
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ pub macro PartialEq($item:item) {
279279
#[doc(alias = "!=")]
280280
#[stable(feature = "rust1", since = "1.0.0")]
281281
#[rustc_diagnostic_item = "Eq"]
282-
pub trait Eq: PartialEq<Self> {
282+
#[const_trait]
283+
pub trait Eq: ~const PartialEq<Self> {
283284
// this method is used solely by #[deriving] to assert
284285
// that every component of a type implements #[deriving]
285286
// itself, the current deriving infrastructure means doing this
@@ -329,8 +330,8 @@ pub struct AssertParamIsEq<T: Eq + ?Sized> {
329330
/// let result = 2.cmp(&1);
330331
/// assert_eq!(Ordering::Greater, result);
331332
/// ```
332-
#[derive(Clone, Copy, Eq, Debug, Hash)]
333-
#[derive_const(PartialOrd, Ord, PartialEq)]
333+
#[derive(Clone, Copy, Debug, Hash)]
334+
#[derive_const(PartialOrd, Eq, Ord, PartialEq)]
334335
#[stable(feature = "rust1", since = "1.0.0")]
335336
#[repr(i8)]
336337
pub enum Ordering {
@@ -594,11 +595,25 @@ impl Ordering {
594595
/// v.sort_by_key(|&num| (num > 3, Reverse(num)));
595596
/// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
596597
/// ```
597-
#[derive(PartialEq, Eq, Debug, Copy, Default, Hash)]
598+
#[derive(Debug, Copy, Default, Hash)]
599+
#[cfg_attr(not(bootstrap), derive_const(PartialEq, Eq))]
598600
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
599601
#[repr(transparent)]
600602
pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T);
601603

604+
#[cfg(bootstrap)]
605+
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
606+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
607+
impl<T: ~const PartialEq> const PartialEq for Reverse<T> {
608+
fn eq(&self, other: &Self) -> bool {
609+
self.0 == other.0
610+
}
611+
}
612+
#[cfg(bootstrap)]
613+
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
614+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
615+
impl<T: ~const Eq> const Eq for Reverse<T> {}
616+
602617
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
603618
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
604619
impl<T: ~const PartialOrd> const PartialOrd for Reverse<T> {
@@ -760,7 +775,7 @@ impl<T: Clone> Clone for Reverse<T> {
760775
#[stable(feature = "rust1", since = "1.0.0")]
761776
#[rustc_diagnostic_item = "Ord"]
762777
#[const_trait]
763-
pub trait Ord: Eq + PartialOrd<Self> {
778+
pub trait Ord: ~const Eq + ~const PartialOrd<Self> {
764779
/// This method returns an [`Ordering`] between `self` and `other`.
765780
///
766781
/// By convention, `self.cmp(&other)` returns the ordering matching the expression
@@ -1032,7 +1047,7 @@ pub macro Ord($item:item) {
10321047
)]
10331048
#[const_trait]
10341049
#[rustc_diagnostic_item = "PartialOrd"]
1035-
pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1050+
pub trait PartialOrd<Rhs: ?Sized = Self>: ~const PartialEq<Rhs> {
10361051
/// This method returns an ordering between `self` and `other` values if one exists.
10371052
///
10381053
/// # Examples
@@ -1331,7 +1346,8 @@ mod impls {
13311346
macro_rules! eq_impl {
13321347
($($t:ty)*) => ($(
13331348
#[stable(feature = "rust1", since = "1.0.0")]
1334-
impl Eq for $t {}
1349+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1350+
impl const Eq for $t {}
13351351
)*)
13361352
}
13371353

@@ -1455,7 +1471,8 @@ mod impls {
14551471
}
14561472

14571473
#[unstable(feature = "never_type", issue = "35121")]
1458-
impl Eq for ! {}
1474+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1475+
impl const Eq for ! {}
14591476

14601477
#[unstable(feature = "never_type", issue = "35121")]
14611478
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
@@ -1491,9 +1508,10 @@ mod impls {
14911508
}
14921509
}
14931510
#[stable(feature = "rust1", since = "1.0.0")]
1494-
impl<A: ?Sized, B: ?Sized> PartialOrd<&B> for &A
1511+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1512+
impl<A: ?Sized, B: ?Sized> const PartialOrd<&B> for &A
14951513
where
1496-
A: PartialOrd<B>,
1514+
A: ~const PartialOrd<B>,
14971515
{
14981516
#[inline]
14991517
fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
@@ -1517,24 +1535,27 @@ mod impls {
15171535
}
15181536
}
15191537
#[stable(feature = "rust1", since = "1.0.0")]
1520-
impl<A: ?Sized> Ord for &A
1538+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1539+
impl<A: ?Sized> const Ord for &A
15211540
where
1522-
A: Ord,
1541+
A: ~const Ord,
15231542
{
15241543
#[inline]
15251544
fn cmp(&self, other: &Self) -> Ordering {
15261545
Ord::cmp(*self, *other)
15271546
}
15281547
}
15291548
#[stable(feature = "rust1", since = "1.0.0")]
1530-
impl<A: ?Sized> Eq for &A where A: Eq {}
1549+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1550+
impl<A: ?Sized> const Eq for &A where A: ~const Eq {}
15311551

15321552
// &mut pointers
15331553

15341554
#[stable(feature = "rust1", since = "1.0.0")]
1535-
impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &mut A
1555+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1556+
impl<A: ?Sized, B: ?Sized> const PartialEq<&mut B> for &mut A
15361557
where
1537-
A: PartialEq<B>,
1558+
A: ~const PartialEq<B>,
15381559
{
15391560
#[inline]
15401561
fn eq(&self, other: &&mut B) -> bool {
@@ -1546,9 +1567,10 @@ mod impls {
15461567
}
15471568
}
15481569
#[stable(feature = "rust1", since = "1.0.0")]
1549-
impl<A: ?Sized, B: ?Sized> PartialOrd<&mut B> for &mut A
1570+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1571+
impl<A: ?Sized, B: ?Sized> const PartialOrd<&mut B> for &mut A
15501572
where
1551-
A: PartialOrd<B>,
1573+
A: ~const PartialOrd<B>,
15521574
{
15531575
#[inline]
15541576
fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
@@ -1572,22 +1594,25 @@ mod impls {
15721594
}
15731595
}
15741596
#[stable(feature = "rust1", since = "1.0.0")]
1575-
impl<A: ?Sized> Ord for &mut A
1597+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1598+
impl<A: ?Sized> const Ord for &mut A
15761599
where
1577-
A: Ord,
1600+
A: ~const Ord,
15781601
{
15791602
#[inline]
15801603
fn cmp(&self, other: &Self) -> Ordering {
15811604
Ord::cmp(*self, *other)
15821605
}
15831606
}
15841607
#[stable(feature = "rust1", since = "1.0.0")]
1585-
impl<A: ?Sized> Eq for &mut A where A: Eq {}
1608+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1609+
impl<A: ?Sized> const Eq for &mut A where A: ~const Eq {}
15861610

15871611
#[stable(feature = "rust1", since = "1.0.0")]
1588-
impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &A
1612+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1613+
impl<A: ?Sized, B: ?Sized> const PartialEq<&mut B> for &A
15891614
where
1590-
A: PartialEq<B>,
1615+
A: ~const PartialEq<B>,
15911616
{
15921617
#[inline]
15931618
fn eq(&self, other: &&mut B) -> bool {
@@ -1600,9 +1625,10 @@ mod impls {
16001625
}
16011626

16021627
#[stable(feature = "rust1", since = "1.0.0")]
1603-
impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &mut A
1628+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1629+
impl<A: ?Sized, B: ?Sized> const PartialEq<&B> for &mut A
16041630
where
1605-
A: PartialEq<B>,
1631+
A: ~const PartialEq<B>,
16061632
{
16071633
#[inline]
16081634
fn eq(&self, other: &&B) -> bool {

library/core/src/ptr/alignment.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::{cmp, fmt, hash, mem, num};
99
/// Note that particularly large alignments, while representable in this type,
1010
/// are likely not to be supported by actual allocators and linkers.
1111
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
12-
#[derive(Copy, Clone, Eq)]
13-
#[derive_const(PartialEq)]
12+
#[derive(Copy, Clone)]
13+
#[derive_const(PartialEq, Eq)]
1414
#[repr(transparent)]
1515
pub struct Alignment(AlignmentEnum);
1616

library/core/src/tuple.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ macro_rules! tuple_impls {
4141
maybe_tuple_doc! {
4242
$($T)+ @
4343
#[stable(feature = "rust1", since = "1.0.0")]
44-
impl<$($T: Eq),+> Eq for ($($T,)+)
44+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
45+
impl<$($T: ~const Eq),+> const Eq for ($($T,)+)
4546
where
4647
last_type!($($T,)+): ?Sized
4748
{}
@@ -51,7 +52,7 @@ macro_rules! tuple_impls {
5152
$($T)+ @
5253
#[stable(feature = "rust1", since = "1.0.0")]
5354
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
54-
impl<$($T: ~const PartialOrd + ~const PartialEq),+> const PartialOrd for ($($T,)+)
55+
impl<$($T: ~const PartialOrd),+> const PartialOrd for ($($T,)+)
5556
where
5657
last_type!($($T,)+): ?Sized
5758
{

tests/ui/consts/issue-25826.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0277]: can't compare `*const ()` with `*const ()` in const contexts
1+
error[E0277]: can't compare `*const ()` with `_` in const contexts
22
--> $DIR/issue-25826.rs:3:52
33
|
44
LL | const A: bool = unsafe { id::<u8> as *const () < id::<u16> as *const () };
5-
| ^ no implementation for `*const () < *const ()` and `*const () > *const ()`
5+
| ^ no implementation for `*const () < _` and `*const () > _`
66
|
7-
= help: the trait `~const PartialOrd` is not implemented for `*const ()`
8-
note: the trait `PartialOrd` is implemented for `*const ()`, but that implementation is not `const`
7+
= help: the trait `~const PartialOrd<_>` is not implemented for `*const ()`
8+
note: the trait `PartialOrd<_>` is implemented for `*const ()`, but that implementation is not `const`
99
--> $DIR/issue-25826.rs:3:52
1010
|
1111
LL | const A: bool = unsafe { id::<u8> as *const () < id::<u16> as *const () };

0 commit comments

Comments
 (0)