Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constify PartialEq #133995

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ use self::Ordering::*;
append_const_msg
)]
#[rustc_diagnostic_item = "PartialEq"]
#[cfg_attr(not(bootstrap), const_trait)]
pub trait PartialEq<Rhs: ?Sized = Self> {
/// Tests for `self` and `other` values to be equal, and is used by `==`.
#[must_use]
Expand Down Expand Up @@ -1630,6 +1631,16 @@ mod impls {

macro_rules! partial_eq_impl {
($($t:ty)*) => ($(
#[cfg(not(bootstrap))]
#[stable(feature = "rust1", since = "1.0.0")]
impl const PartialEq for $t {
#[inline]
fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
#[inline]
fn ne(&self, other: &$t) -> bool { (*self) != (*other) }
}

#[cfg(bootstrap)]
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for $t {
#[inline]
Expand All @@ -1640,6 +1651,20 @@ mod impls {
)*)
}

#[cfg(not(bootstrap))]
#[stable(feature = "rust1", since = "1.0.0")]
impl const PartialEq for () {
#[inline]
fn eq(&self, _other: &()) -> bool {
true
}
#[inline]
fn ne(&self, _other: &()) -> bool {
false
}
}

#[cfg(bootstrap)]
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for () {
#[inline]
Expand Down Expand Up @@ -1808,6 +1833,24 @@ mod impls {

// & pointers

#[cfg(not(bootstrap))]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<A: ?Sized, B: ?Sized> const PartialEq<&B> for &A
where
A: ~const PartialEq<B>,
{
#[inline]
fn eq(&self, other: &&B) -> bool {
PartialEq::eq(*self, *other)
}
#[inline]
fn ne(&self, other: &&B) -> bool {
PartialEq::ne(*self, *other)
}
}

#[cfg(bootstrap)]
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &A
where
Expand All @@ -1822,6 +1865,7 @@ mod impls {
PartialEq::ne(*self, *other)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialOrd<&B> for &A
where
Expand Down Expand Up @@ -1863,6 +1907,24 @@ mod impls {

// &mut pointers

#[cfg(not(bootstrap))]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<A: ?Sized, B: ?Sized> const PartialEq<&mut B> for &mut A
where
A: ~const PartialEq<B>,
{
#[inline]
fn eq(&self, other: &&mut B) -> bool {
PartialEq::eq(*self, *other)
}
#[inline]
fn ne(&self, other: &&mut B) -> bool {
PartialEq::ne(*self, *other)
}
}

#[cfg(bootstrap)]
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &mut A
where
Expand All @@ -1877,6 +1939,7 @@ mod impls {
PartialEq::ne(*self, *other)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialOrd<&mut B> for &mut A
where
Expand Down Expand Up @@ -1916,6 +1979,41 @@ mod impls {
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized> Eq for &mut A where A: Eq {}

#[cfg(not(bootstrap))]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<A: ?Sized, B: ?Sized> const PartialEq<&mut B> for &A
where
A: ~const PartialEq<B>,
{
#[inline]
fn eq(&self, other: &&mut B) -> bool {
PartialEq::eq(*self, *other)
}
#[inline]
fn ne(&self, other: &&mut B) -> bool {
PartialEq::ne(*self, *other)
}
}

#[cfg(not(bootstrap))]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<A: ?Sized, B: ?Sized> const PartialEq<&B> for &mut A
where
A: ~const PartialEq<B>,
{
#[inline]
fn eq(&self, other: &&B) -> bool {
PartialEq::eq(*self, *other)
}
#[inline]
fn ne(&self, other: &&B) -> bool {
PartialEq::ne(*self, *other)
}
}

#[cfg(bootstrap)]
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &A
where
Expand All @@ -1931,6 +2029,7 @@ mod impls {
}
}

#[cfg(bootstrap)]
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &mut A
where
Expand Down
76 changes: 76 additions & 0 deletions library/core/src/slice/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ use crate::intrinsics::compare_bytes;
use crate::num::NonZero;
use crate::{ascii, mem};

#[cfg(not(bootstrap))]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<T, U> const PartialEq<[U]> for [T]
where
T: ~const PartialEq<U>,
{
fn eq(&self, other: &[U]) -> bool {
SlicePartialEq::equal(self, other)
}

fn ne(&self, other: &[U]) -> bool {
SlicePartialEq::not_equal(self, other)
}
}

#[cfg(bootstrap)]
#[stable(feature = "rust1", since = "1.0.0")]
impl<T, U> PartialEq<[U]> for [T]
where
Expand Down Expand Up @@ -40,6 +57,7 @@ impl<T: PartialOrd> PartialOrd for [T] {
}

#[doc(hidden)]
#[cfg_attr(not(bootstrap), const_trait)]
// intermediate trait for specialization of slice's PartialEq
trait SlicePartialEq<B> {
fn equal(&self, other: &[B]) -> bool;
Expand All @@ -50,6 +68,43 @@ trait SlicePartialEq<B> {
}

// Generic slice equality
#[cfg(not(bootstrap))]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<A, B> const SlicePartialEq<B> for [A]
where
A: ~const PartialEq<B>,
{
default fn equal(&self, other: &[B]) -> bool {
if self.len() != other.len() {
return false;
}

/* FIXME(const_trait_impl): As soon as iterators are impld.
// Implemented as explicit indexing rather
// than zipped iterators for performance reasons.
// See PR https://github.com/rust-lang/rust/pull/116846
for idx in 0..self.len() {
// bound checks are optimized away
if self[idx] != other[idx] {
return false;
}
}
*/

let mut idx = 0;
while idx < self.len() {
// bound checks are optimized away
if self[idx] != other[idx] {
return false;
}
idx += 1;
}

true
}
}

#[cfg(bootstrap)]
impl<A, B> SlicePartialEq<B> for [A]
where
A: PartialEq<B>,
Expand All @@ -75,6 +130,27 @@ where

// When each element can be compared byte-wise, we can compare all the bytes
// from the whole size in one call to the intrinsics.
#[cfg(not(bootstrap))]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<A, B> const SlicePartialEq<B> for [A]
where
A: BytewiseEq<B> + ~const PartialEq<B>,
{
fn equal(&self, other: &[B]) -> bool {
if self.len() != other.len() {
return false;
}

// SAFETY: `self` and `other` are references and are thus guaranteed to be valid.
// The two slices have been checked to have the same size above.
unsafe {
let size = mem::size_of_val(self);
compare_bytes(self.as_ptr() as *const u8, other.as_ptr() as *const u8, size) == 0
}
}
}

#[cfg(bootstrap)]
impl<A, B> SlicePartialEq<B> for [A]
where
A: BytewiseEq<B>,
Expand Down
11 changes: 11 additions & 0 deletions library/core/src/str/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ impl Ord for str {
}
}

#[cfg(not(bootstrap))]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const PartialEq for str {
#[inline]
fn eq(&self, other: &str) -> bool {
self.as_bytes() == other.as_bytes()
}
}

#[cfg(bootstrap)]
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for str {
#[inline]
Expand Down
18 changes: 9 additions & 9 deletions tests/ui/const-generics/issues/issue-90318.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
= help: consider moving this anonymous constant into a `const` function
= note: this operation may be supported in the future

error[E0015]: cannot call non-const operator in constants
error[E0658]: cannot call conditionally-const method `<TypeId as PartialEq>::ne` in constants
--> $DIR/issue-90318.rs:14:10
|
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: impl defined here, but it is not `const`
--> $SRC_DIR/core/src/any.rs:LL:COL
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0015]: cannot call non-const operator in constants
error[E0658]: cannot call conditionally-const method `<TypeId as PartialEq>::ne` in constants
--> $DIR/issue-90318.rs:22:10
|
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: impl defined here, but it is not `const`
--> $SRC_DIR/core/src/any.rs:LL:COL
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0658`.
8 changes: 3 additions & 5 deletions tests/ui/consts/const_cmp_type_id.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tracking issue #101871 has never been updated >.< It didn't track the unconstification.

Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
//@ compile-flags: -Znext-solver

#![feature(const_type_id, const_trait_impl)]

use std::any::TypeId;

fn main() {
const {
assert!(TypeId::of::<u8>() == TypeId::of::<u8>());
//~^ ERROR cannot call non-const operator in constants
//~^ ERROR the trait bound `TypeId: const PartialEq` is not satisfied
assert!(TypeId::of::<()>() != TypeId::of::<u8>());
//~^ ERROR cannot call non-const operator in constants
//~^ ERROR the trait bound `TypeId: const PartialEq` is not satisfied
let _a = TypeId::of::<u8>() < TypeId::of::<u16>();
//~^ ERROR cannot call non-const operator in constants
// can't assert `_a` because it is not deterministic
// FIXME(const_trait_impl) make it pass
}
}
31 changes: 6 additions & 25 deletions tests/ui/consts/const_cmp_type_id.stderr
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
error[E0015]: cannot call non-const operator in constants
--> $DIR/const_cmp_type_id.rs:8:17
error[E0277]: the trait bound `TypeId: const PartialEq` is not satisfied
--> $DIR/const_cmp_type_id.rs:9:17
|
LL | assert!(TypeId::of::<u8>() == TypeId::of::<u8>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: impl defined here, but it is not `const`
--> $SRC_DIR/core/src/any.rs:LL:COL
= note: calls in constants are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const operator in constants
--> $DIR/const_cmp_type_id.rs:10:17
error[E0277]: the trait bound `TypeId: const PartialEq` is not satisfied
--> $DIR/const_cmp_type_id.rs:11:17
|
LL | assert!(TypeId::of::<()>() != TypeId::of::<u8>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: impl defined here, but it is not `const`
--> $SRC_DIR/core/src/any.rs:LL:COL
= note: calls in constants are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const operator in constants
--> $DIR/const_cmp_type_id.rs:12:18
|
LL | let _a = TypeId::of::<u8>() < TypeId::of::<u16>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: impl defined here, but it is not `const`
--> $SRC_DIR/core/src/any.rs:LL:COL
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the derive macro `PartialOrd` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0277`.
Loading
Loading