Skip to content

Constify PartialEq #133995

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -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]
@@ -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]
@@ -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]
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
76 changes: 76 additions & 0 deletions library/core/src/slice/cmp.rs
Original file line number Diff line number Diff line change
@@ -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
@@ -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;
@@ -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>,
@@ -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>,
11 changes: 11 additions & 0 deletions library/core/src/str/traits.rs
Original file line number Diff line number Diff line change
@@ -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]
18 changes: 9 additions & 9 deletions tests/ui/const-generics/issues/issue-90318.stderr
Original file line number Diff line number Diff line change
@@ -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`.
14 changes: 13 additions & 1 deletion tests/ui/consts/fn_trait_refs.stderr
Original file line number Diff line number Diff line change
@@ -137,6 +137,12 @@ LL | where
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^ required by this bound in `test_fn`

error[E0277]: the trait bound `(i32, i32, i32): const PartialEq` is not satisfied
--> $DIR/fn_trait_refs.rs:71:17
|
LL | assert!(test_one == (1, 1, 1));
| ^^^^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `fn() -> i32 {two}: const Destruct` is not satisfied
--> $DIR/fn_trait_refs.rs:73:36
|
@@ -154,6 +160,12 @@ LL | where
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^ required by this bound in `test_fn_mut`

error[E0277]: the trait bound `(i32, i32): const PartialEq` is not satisfied
--> $DIR/fn_trait_refs.rs:74:17
|
LL | assert!(test_two == (2, 2));
| ^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `&T: ~const Destruct` is not satisfied
--> $DIR/fn_trait_refs.rs:39:19
|
@@ -241,7 +253,7 @@ help: consider further restricting this bound
LL | T: ~const FnOnce<()> + ~const FnOnce(),
| +++++++++++++++++

error: aborting due to 25 previous errors
error: aborting due to 27 previous errors

Some errors have detailed explanations: E0015, E0277, E0635.
For more information about an error, try `rustc --explain E0015`.
8 changes: 2 additions & 6 deletions tests/ui/consts/issue-73976-monomorphic.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
error[E0015]: cannot call non-const operator in constant functions
error[E0277]: the trait bound `TypeId: ~const PartialEq` is not satisfied
--> $DIR/issue-73976-monomorphic.rs:21:5
|
LL | GetTypeId::<T>::VALUE == GetTypeId::<usize>::VALUE
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: impl defined here, but it is not `const`
--> $SRC_DIR/core/src/any.rs:LL:COL
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0277`.
9 changes: 3 additions & 6 deletions tests/ui/consts/issue-90870.rs
Original file line number Diff line number Diff line change
@@ -4,21 +4,18 @@

const fn f(a: &u8, b: &u8) -> bool {
a == b
//~^ ERROR: cannot call non-const operator in constant functions [E0015]
//~| HELP: consider dereferencing here
//~^ ERROR cannot call conditionally-const method `<&u8 as PartialEq>::eq` in constant functions
}

const fn g(a: &&&&i64, b: &&&&i64) -> bool {
a == b
//~^ ERROR: cannot call non-const operator in constant functions [E0015]
//~| HELP: consider dereferencing here
//~^ ERROR cannot call conditionally-const method `<&&&&i64 as PartialEq>::eq` in constant functions
}

const fn h(mut a: &[u8], mut b: &[u8]) -> bool {
while let ([l, at @ ..], [r, bt @ ..]) = (a, b) {
if l == r {
//~^ ERROR: cannot call non-const operator in constant functions [E0015]
//~| HELP: consider dereferencing here
//~^ ERROR cannot call conditionally-const method `<&u8 as PartialEq>::eq` in constant functions
a = at;
b = bt;
} else {
36 changes: 15 additions & 21 deletions tests/ui/consts/issue-90870.stderr
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
error[E0015]: cannot call non-const operator in constant functions
error[E0658]: cannot call conditionally-const method `<&u8 as PartialEq>::eq` in constant functions
--> $DIR/issue-90870.rs:6:5
|
LL | a == b
| ^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider dereferencing here
|
LL | *a == *b
| + +
= 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 constant functions
--> $DIR/issue-90870.rs:12:5
error[E0658]: cannot call conditionally-const method `<&&&&i64 as PartialEq>::eq` in constant functions
--> $DIR/issue-90870.rs:11:5
|
LL | a == b
| ^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider dereferencing here
|
LL | ****a == ****b
| ++++ ++++
= 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 constant functions
--> $DIR/issue-90870.rs:19:12
error[E0658]: cannot call conditionally-const method `<&u8 as PartialEq>::eq` in constant functions
--> $DIR/issue-90870.rs:17:12
|
LL | if l == r {
| ^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider dereferencing here
|
LL | if *l == *r {
| + +
= 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 3 previous errors

For more information about this error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0658`.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ known-bug: #110395
//@ check-pass

#![feature(const_trait_impl)]

20 changes: 0 additions & 20 deletions tests/ui/traits/const-traits/call-const-trait-method-pass.stderr

This file was deleted.

4 changes: 2 additions & 2 deletions tests/ui/traits/const-traits/call-generic-in-impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ known-bug: #110395
// FIXME(const_trait_impl) check-pass
//@ check-pass

#![feature(const_trait_impl)]

#[const_trait]
25 changes: 0 additions & 25 deletions tests/ui/traits/const-traits/call-generic-in-impl.stderr

This file was deleted.

4 changes: 1 addition & 3 deletions tests/ui/traits/const-traits/call-generic-method-chain.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Basic test for calling methods on generic type parameters in `const fn`.
//@ known-bug: #110395
//@ compile-flags: -Znext-solver
// FIXME(const_trait_impl) check-pass
//@ check-pass

#![feature(const_trait_impl)]

60 changes: 0 additions & 60 deletions tests/ui/traits/const-traits/call-generic-method-chain.stderr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//@ compile-flags: -Znext-solver
//@ known-bug: #110395
// FIXME(const_trait_impl) check-pass
//@ check-pass

#![feature(const_trait_impl)]

72 changes: 0 additions & 72 deletions tests/ui/traits/const-traits/call-generic-method-dup-bound.stderr

This file was deleted.

3 changes: 1 addition & 2 deletions tests/ui/traits/const-traits/call-generic-method-fail.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//@ compile-flags: -Znext-solver
#![feature(const_trait_impl)]

pub const fn equals_self<T: PartialEq>(t: &T) -> bool {
*t == *t
//~^ ERROR cannot call non-const operator in constant functions
//~^ ERROR the trait bound `T: ~const PartialEq` is not satisfied
}

fn main() {}
12 changes: 3 additions & 9 deletions tests/ui/traits/const-traits/call-generic-method-fail.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
error[E0015]: cannot call non-const operator in constant functions
--> $DIR/call-generic-method-fail.rs:5:5
error[E0277]: the trait bound `T: ~const PartialEq` is not satisfied
--> $DIR/call-generic-method-fail.rs:4:5
|
LL | *t == *t
| ^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider further restricting this bound
|
LL | pub const fn equals_self<T: PartialEq + ~const std::cmp::PartialEq>(t: &T) -> bool {
| ++++++++++++++++++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0277`.
4 changes: 1 addition & 3 deletions tests/ui/traits/const-traits/call-generic-method-pass.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Basic test for calling methods on generic type parameters in `const fn`.
//@ compile-flags: -Znext-solver
//@ known-bug: #110395
// FIXME(const_trait_impl) check-pass
//@ check-pass

#![feature(const_trait_impl)]

46 changes: 0 additions & 46 deletions tests/ui/traits/const-traits/call-generic-method-pass.stderr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -29,25 +29,6 @@ LL | #[derive_const(Default, PartialEq)]
= note: adding a non-const method body in the future would be a breaking change
= note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)

error: const `impl` for trait `PartialEq` which is not marked with `#[const_trait]`
--> $DIR/derive-const-use.rs:11:12
|
LL | impl const PartialEq for A {
| ^^^^^^^^^
|
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change

error: const `impl` for trait `PartialEq` which is not marked with `#[const_trait]`
--> $DIR/derive-const-use.rs:15:25
|
LL | #[derive_const(Default, PartialEq)]
| ^^^^^^^^^
|
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `<S as Default>::default` in constants
--> $DIR/derive-const-use.rs:18:35
|
@@ -56,14 +37,6 @@ LL | const _: () = assert!(S((), A) == S::default());
|
= note: calls in constants are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const operator in constants
--> $DIR/derive-const-use.rs:18:23
|
LL | const _: () = assert!(S((), A) == S::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: calls in constants are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const fn `<() as Default>::default` in constant functions
--> $DIR/derive-const-use.rs:16:14
|
@@ -86,29 +59,7 @@ LL | pub struct S((), A);
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const operator in constant functions
--> $DIR/derive-const-use.rs:16:14
|
LL | #[derive_const(Default, PartialEq)]
| --------- in this derive macro expansion
LL | pub struct S((), A);
| ^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const operator in constant functions
--> $DIR/derive-const-use.rs:16:18
|
LL | #[derive_const(Default, PartialEq)]
| --------- in this derive macro expansion
LL | pub struct S((), A);
| ^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 12 previous errors
error: aborting due to 7 previous errors

Some errors have detailed explanations: E0015, E0635.
For more information about an error, try `rustc --explain E0015`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@ known-bug: #110395
// FIXME(const_trait_impl) check-pass
//@ check-pass

#![feature(derive_const)]
#![feature(const_trait_impl)]

This file was deleted.

Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ const fn test() -> impl ~const Fn() {
[first, remainder @ ..] => {
assert_eq!(first, &b'f');
//~^ ERROR cannot call non-const fn
//~| ERROR cannot call non-const operator
}
[] => panic!(),
}
Original file line number Diff line number Diff line change
@@ -30,15 +30,6 @@ LL | const fn test() -> impl ~const Fn() {
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0015]: cannot call non-const operator in constant functions
--> $DIR/ice-112822-expected-type-for-param.rs:12:17
|
LL | assert_eq!(first, &b'f');
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `core::panicking::assert_failed::<&u8, &u8>` in constant functions
--> $DIR/ice-112822-expected-type-for-param.rs:12:17
|
@@ -48,7 +39,7 @@ LL | assert_eq!(first, &b'f');
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 6 previous errors
error: aborting due to 5 previous errors

Some errors have detailed explanations: E0015, E0658.
For more information about an error, try `rustc --explain E0015`.
12 changes: 0 additions & 12 deletions tests/ui/traits/const-traits/match-non-const-eq.gated.stderr

This file was deleted.

9 changes: 5 additions & 4 deletions tests/ui/traits/const-traits/match-non-const-eq.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//@ known-bug: #110395
//@ revisions: stock gated
//@[gated] check-pass
//@ revisions: gated stock

#![cfg_attr(gated, feature(const_trait_impl))]

const fn foo(input: &'static str) {
match input {
"a" => (), //FIXME [gated]~ ERROR can't compare `str` with `str` in const contexts
//FIXME ~^ ERROR cannot match on `str` in constant functions
"a" => (),
//[stock]~^ cannot call conditionally-const method `<str as PartialEq>::eq` in constant functions
_ => (),
}
}
13 changes: 7 additions & 6 deletions tests/ui/traits/const-traits/match-non-const-eq.stock.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
error[E0015]: cannot match on `str` in constant functions
--> $DIR/match-non-const-eq.rs:7:9
error[E0658]: cannot call conditionally-const method `<str as PartialEq>::eq` in constant functions
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: surprised that when rendering predicates we talk about T: ~const PartialEq, but in fully qualified paths we talk <str as PartialEq>::eq instead of something like <str as ~const PartialEq>::eq. We already mention "conditionally-const" in the text, but wonder how many diagnostics will have to account for that to properly convey that context.

Copy link
Member Author

Choose a reason for hiding this comment

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

well, so, <str as ~const PartialEq>::eq is not a valid qpath. the method itself still is just <str as PartialEq>::eq, it just has an additional "const requirement" on it.

Copy link
Member Author

Choose a reason for hiding this comment

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

like, <str as ~const PartialEq>::eq and <str as PartialEq>::eq being distinct suggests that they are different methods, but they are not. this is a consequence of the current implementation of conditional constness.

Copy link
Member Author

Choose a reason for hiding this comment

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

this is somewhat related to the fact that we allow users to write T: Trait<Assoc = i32>, but not in method call position (i.e. <T as Trait<Assoc = i32>>::method is not valid). The constness of a trait is associated with a trait impl. Imagine that T: ~const Trait were written like T: Trait<constness = ~const>.

--> $DIR/match-non-const-eq.rs:8:9
|
LL | "a" => (), //FIXME [gated]~ ERROR can't compare `str` with `str` in const contexts
LL | "a" => (),
| ^^^
|
= note: `str` cannot be compared in compile-time, and therefore cannot be used in `match`es
= note: calls in constant functions 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 1 previous error

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