Skip to content

Commit 494d350

Browse files
committed
Make slices [const] PartialEq
1 parent d86a9d1 commit 494d350

File tree

12 files changed

+53
-34
lines changed

12 files changed

+53
-34
lines changed

library/core/src/cmp.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@ pub struct AssertParamIsEq<T: Eq + PointeeSized> {
381381
///
382382
/// assert_eq!(2.cmp(&1), Ordering::Greater);
383383
/// ```
384-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
384+
#[derive(Clone, Copy, Eq, PartialOrd, Ord, Debug, Hash)]
385+
#[derive_const(PartialEq)]
385386
#[stable(feature = "rust1", since = "1.0.0")]
386387
// This is a lang item only so that `BinOp::Cmp` in MIR can return it.
387388
// It has no special behavior, but does require that the three variants

library/core/src/cmp/bytewise.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ use crate::num::NonZero;
1717
/// - Neither `Self` nor `Rhs` have provenance, so integer comparisons are correct.
1818
/// - `<Self as PartialEq<Rhs>>::{eq,ne}` are equivalent to comparing the bytes.
1919
#[rustc_specialization_trait]
20-
pub(crate) unsafe trait BytewiseEq<Rhs = Self>: PartialEq<Rhs> + Sized {}
20+
#[const_trait]
21+
pub(crate) unsafe trait BytewiseEq<Rhs = Self>:
22+
~const PartialEq<Rhs> + Sized
23+
{
24+
}
2125

2226
macro_rules! is_bytewise_comparable {
2327
($($t:ty),+ $(,)?) => {$(
24-
unsafe impl BytewiseEq for $t {}
28+
unsafe impl const BytewiseEq for $t {}
2529
)+};
2630
}
2731

library/core/src/intrinsics/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,6 +2208,7 @@ pub const unsafe fn raw_eq<T>(a: &T, b: &T) -> bool;
22082208
/// [valid]: crate::ptr#safety
22092209
#[rustc_nounwind]
22102210
#[rustc_intrinsic]
2211+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
22112212
pub const unsafe fn compare_bytes(left: *const u8, right: *const u8, bytes: usize) -> i32;
22122213

22132214
/// See documentation of [`std::hint::black_box`] for details.

library/core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
#![feature(cfg_select)]
104104
#![feature(cfg_target_has_reliable_f16_f128)]
105105
#![feature(const_carrying_mul_add)]
106+
#![feature(const_cmp)]
106107
#![feature(const_destruct)]
107108
#![feature(const_eval_select)]
108109
#![feature(core_intrinsics)]
@@ -146,6 +147,7 @@
146147
#![feature(const_trait_impl)]
147148
#![feature(decl_macro)]
148149
#![feature(deprecated_suggestion)]
150+
#![feature(derive_const)]
149151
#![feature(doc_cfg)]
150152
#![feature(doc_cfg_hide)]
151153
#![feature(doc_notable_trait)]

library/core/src/slice/cmp.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ use crate::num::NonZero;
88
use crate::ops::ControlFlow;
99

1010
#[stable(feature = "rust1", since = "1.0.0")]
11-
impl<T, U> PartialEq<[U]> for [T]
11+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
12+
impl<T, U> const PartialEq<[U]> for [T]
1213
where
13-
T: PartialEq<U>,
14+
T: ~const PartialEq<U>,
1415
{
1516
fn eq(&self, other: &[U]) -> bool {
1617
SlicePartialEq::equal(self, other)
@@ -94,6 +95,8 @@ impl<T: PartialOrd> PartialOrd for [T] {
9495

9596
#[doc(hidden)]
9697
// intermediate trait for specialization of slice's PartialEq
98+
#[const_trait]
99+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
97100
trait SlicePartialEq<B> {
98101
fn equal(&self, other: &[B]) -> bool;
99102

@@ -103,9 +106,10 @@ trait SlicePartialEq<B> {
103106
}
104107

105108
// Generic slice equality
106-
impl<A, B> SlicePartialEq<B> for [A]
109+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
110+
impl<A, B> const SlicePartialEq<B> for [A]
107111
where
108-
A: PartialEq<B>,
112+
A: ~const PartialEq<B>,
109113
{
110114
default fn equal(&self, other: &[B]) -> bool {
111115
if self.len() != other.len() {
@@ -115,11 +119,13 @@ where
115119
// Implemented as explicit indexing rather
116120
// than zipped iterators for performance reasons.
117121
// See PR https://github.com/rust-lang/rust/pull/116846
118-
for idx in 0..self.len() {
122+
let mut idx = 0;
123+
while idx < self.len() {
119124
// bound checks are optimized away
120125
if self[idx] != other[idx] {
121126
return false;
122127
}
128+
idx += 1;
123129
}
124130

125131
true
@@ -128,9 +134,10 @@ where
128134

129135
// When each element can be compared byte-wise, we can compare all the bytes
130136
// from the whole size in one call to the intrinsics.
131-
impl<A, B> SlicePartialEq<B> for [A]
137+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
138+
impl<A, B> const SlicePartialEq<B> for [A]
132139
where
133-
A: BytewiseEq<B>,
140+
A: ~const BytewiseEq<B>,
134141
{
135142
fn equal(&self, other: &[B]) -> bool {
136143
if self.len() != other.len() {

library/core/src/str/traits.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ impl Ord for str {
2323
}
2424

2525
#[stable(feature = "rust1", since = "1.0.0")]
26-
impl PartialEq for str {
26+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
27+
impl const PartialEq for str {
2728
#[inline]
2829
fn eq(&self, other: &str) -> bool {
2930
self.as_bytes() == other.as_bytes()

tests/ui/consts/const-compare-bytes-ub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ check-fail
22

3-
#![feature(core_intrinsics)]
3+
#![feature(core_intrinsics, const_cmp)]
44
use std::intrinsics::compare_bytes;
55
use std::mem::MaybeUninit;
66

tests/ui/consts/const-compare-bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ run-pass
22

3-
#![feature(core_intrinsics)]
3+
#![feature(core_intrinsics, const_cmp)]
44
use std::intrinsics::compare_bytes;
55

66
fn main() {

tests/ui/traits/const-traits/const-impl-trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ compile-flags: -Znext-solver
22
//@ known-bug: #110395
33

4-
// Broken until we have `const PartialEq` impl in stdlib
4+
// Broken until `(): const PartialEq`
55

66
#![allow(incomplete_features)]
77
#![feature(const_trait_impl, const_cmp, const_destruct)]

tests/ui/traits/const-traits/match-non-const-eq.gated.stderr

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)