Skip to content

Commit 4b3a017

Browse files
authored
Rollup merge of #63061 - Centril:constantly-improving, r=scottmcm
In which we constantly improve the Vec(Deque) array PartialEq impls Use the same approach as in #62435 as sanctioned by #61415 (comment). r? @scottmcm
2 parents a558668 + bfdfa85 commit 4b3a017

File tree

6 files changed

+170
-59
lines changed

6 files changed

+170
-59
lines changed

Diff for: src/liballoc/collections/vec_deque.rs

+14-26
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
1010
#![stable(feature = "rust1", since = "1.0.0")]
1111

12+
use core::array::LengthAtMost32;
1213
use core::cmp::{self, Ordering};
1314
use core::fmt;
1415
use core::iter::{repeat_with, FromIterator, FusedIterator};
@@ -2571,13 +2572,14 @@ impl<A: PartialEq> PartialEq for VecDeque<A> {
25712572
impl<A: Eq> Eq for VecDeque<A> {}
25722573

25732574
macro_rules! __impl_slice_eq1 {
2574-
($Lhs: ty, $Rhs: ty) => {
2575-
__impl_slice_eq1! { $Lhs, $Rhs, Sized }
2576-
};
2577-
($Lhs: ty, $Rhs: ty, $Bound: ident) => {
2575+
([$($vars:tt)*] $lhs:ty, $rhs:ty, $($constraints:tt)*) => {
25782576
#[stable(feature = "vec_deque_partial_eq_slice", since = "1.17.0")]
2579-
impl<A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
2580-
fn eq(&self, other: &$Rhs) -> bool {
2577+
impl<A, B, $($vars)*> PartialEq<$rhs> for $lhs
2578+
where
2579+
A: PartialEq<B>,
2580+
$($constraints)*
2581+
{
2582+
fn eq(&self, other: &$rhs) -> bool {
25812583
if self.len() != other.len() {
25822584
return false;
25832585
}
@@ -2589,26 +2591,12 @@ macro_rules! __impl_slice_eq1 {
25892591
}
25902592
}
25912593

2592-
__impl_slice_eq1! { VecDeque<A>, Vec<B> }
2593-
__impl_slice_eq1! { VecDeque<A>, &[B] }
2594-
__impl_slice_eq1! { VecDeque<A>, &mut [B] }
2595-
2596-
macro_rules! array_impls {
2597-
($($N: expr)+) => {
2598-
$(
2599-
__impl_slice_eq1! { VecDeque<A>, [B; $N] }
2600-
__impl_slice_eq1! { VecDeque<A>, &[B; $N] }
2601-
__impl_slice_eq1! { VecDeque<A>, &mut [B; $N] }
2602-
)+
2603-
}
2604-
}
2605-
2606-
array_impls! {
2607-
0 1 2 3 4 5 6 7 8 9
2608-
10 11 12 13 14 15 16 17 18 19
2609-
20 21 22 23 24 25 26 27 28 29
2610-
30 31 32
2611-
}
2594+
__impl_slice_eq1! { [] VecDeque<A>, Vec<B>, }
2595+
__impl_slice_eq1! { [] VecDeque<A>, &[B], }
2596+
__impl_slice_eq1! { [] VecDeque<A>, &mut [B], }
2597+
__impl_slice_eq1! { [const N: usize] VecDeque<A>, [B; N], [B; N]: LengthAtMost32 }
2598+
__impl_slice_eq1! { [const N: usize] VecDeque<A>, &[B; N], [B; N]: LengthAtMost32 }
2599+
__impl_slice_eq1! { [const N: usize] VecDeque<A>, &mut [B; N], [B; N]: LengthAtMost32 }
26122600

26132601
#[stable(feature = "rust1", since = "1.0.0")]
26142602
impl<A: PartialOrd> PartialOrd for VecDeque<A> {

Diff for: src/liballoc/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
#![feature(cfg_target_has_atomic)]
7979
#![feature(coerce_unsized)]
8080
#![cfg_attr(not(bootstrap), feature(const_in_array_repeat_expressions))]
81+
#![feature(const_generic_impls_guard)]
82+
#![feature(const_generics)]
8183
#![feature(dispatch_from_dyn)]
8284
#![feature(core_intrinsics)]
8385
#![feature(dropck_eyepatch)]

Diff for: src/liballoc/vec.rs

+23-33
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
5757
#![stable(feature = "rust1", since = "1.0.0")]
5858

59+
use core::array::LengthAtMost32;
5960
use core::cmp::{self, Ordering};
6061
use core::fmt;
6162
use core::hash::{self, Hash};
@@ -2171,47 +2172,36 @@ impl<'a, T: 'a + Copy> Extend<&'a T> for Vec<T> {
21712172
}
21722173

21732174
macro_rules! __impl_slice_eq1 {
2174-
($Lhs: ty, $Rhs: ty) => {
2175-
__impl_slice_eq1! { $Lhs, $Rhs, Sized }
2176-
};
2177-
($Lhs: ty, $Rhs: ty, $Bound: ident) => {
2175+
([$($vars:tt)*] $lhs:ty, $rhs:ty, $($constraints:tt)*) => {
21782176
#[stable(feature = "rust1", since = "1.0.0")]
2179-
impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
2177+
impl<A, B, $($vars)*> PartialEq<$rhs> for $lhs
2178+
where
2179+
A: PartialEq<B>,
2180+
$($constraints)*
2181+
{
21802182
#[inline]
2181-
fn eq(&self, other: &$Rhs) -> bool { self[..] == other[..] }
2183+
fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] }
21822184
#[inline]
2183-
fn ne(&self, other: &$Rhs) -> bool { self[..] != other[..] }
2185+
fn ne(&self, other: &$rhs) -> bool { self[..] != other[..] }
21842186
}
21852187
}
21862188
}
21872189

2188-
__impl_slice_eq1! { Vec<A>, Vec<B> }
2189-
__impl_slice_eq1! { Vec<A>, &'b [B] }
2190-
__impl_slice_eq1! { Vec<A>, &'b mut [B] }
2191-
__impl_slice_eq1! { Cow<'a, [A]>, &'b [B], Clone }
2192-
__impl_slice_eq1! { Cow<'a, [A]>, &'b mut [B], Clone }
2193-
__impl_slice_eq1! { Cow<'a, [A]>, Vec<B>, Clone }
2190+
__impl_slice_eq1! { [] Vec<A>, Vec<B>, }
2191+
__impl_slice_eq1! { [] Vec<A>, &[B], }
2192+
__impl_slice_eq1! { [] Vec<A>, &mut [B], }
2193+
__impl_slice_eq1! { [] Cow<'_, [A]>, &[B], A: Clone }
2194+
__impl_slice_eq1! { [] Cow<'_, [A]>, &mut [B], A: Clone }
2195+
__impl_slice_eq1! { [] Cow<'_, [A]>, Vec<B>, A: Clone }
2196+
__impl_slice_eq1! { [const N: usize] Vec<A>, [B; N], [B; N]: LengthAtMost32 }
2197+
__impl_slice_eq1! { [const N: usize] Vec<A>, &[B; N], [B; N]: LengthAtMost32 }
21942198

2195-
macro_rules! array_impls {
2196-
($($N: expr)+) => {
2197-
$(
2198-
// NOTE: some less important impls are omitted to reduce code bloat
2199-
__impl_slice_eq1! { Vec<A>, [B; $N] }
2200-
__impl_slice_eq1! { Vec<A>, &'b [B; $N] }
2201-
// __impl_slice_eq1! { Vec<A>, &'b mut [B; $N] }
2202-
// __impl_slice_eq1! { Cow<'a, [A]>, [B; $N], Clone }
2203-
// __impl_slice_eq1! { Cow<'a, [A]>, &'b [B; $N], Clone }
2204-
// __impl_slice_eq1! { Cow<'a, [A]>, &'b mut [B; $N], Clone }
2205-
)+
2206-
}
2207-
}
2208-
2209-
array_impls! {
2210-
0 1 2 3 4 5 6 7 8 9
2211-
10 11 12 13 14 15 16 17 18 19
2212-
20 21 22 23 24 25 26 27 28 29
2213-
30 31 32
2214-
}
2199+
// NOTE: some less important impls are omitted to reduce code bloat
2200+
// FIXME(Centril): Reconsider this?
2201+
//__impl_slice_eq1! { [const N: usize] Vec<A>, &mut [B; N], [B; N]: LengthAtMost32 }
2202+
//__impl_slice_eq1! { [const N: usize] Cow<'a, [A]>, [B; N], [B; N]: LengthAtMost32 }
2203+
//__impl_slice_eq1! { [const N: usize] Cow<'a, [A]>, &[B; N], [B; N]: LengthAtMost32 }
2204+
//__impl_slice_eq1! { [const N: usize] Cow<'a, [A]>, &mut [B; N], [B; N]: LengthAtMost32 }
22152205

22162206
/// Implements comparison of vectors, lexicographically.
22172207
#[stable(feature = "rust1", since = "1.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// check-pass
2+
3+
pub fn yes_vec_partial_eq_array<A, B>() -> impl PartialEq<[B; 32]>
4+
where
5+
A: PartialEq<B>,
6+
{
7+
Vec::<A>::new()
8+
}
9+
10+
pub fn yes_vec_partial_eq_ref_array<'a, A, B>() -> impl PartialEq<&'a [B; 32]>
11+
where
12+
A: PartialEq<B>,
13+
{
14+
Vec::<A>::new()
15+
}
16+
17+
use std::collections::VecDeque;
18+
19+
pub fn yes_vecdeque_partial_eq_array<A, B>() -> impl PartialEq<[B; 32]>
20+
where
21+
A: PartialEq<B>,
22+
{
23+
VecDeque::<A>::new()
24+
}
25+
26+
pub fn yes_vecdeque_partial_eq_ref_array<'a, A, B>() -> impl PartialEq<&'a [B; 32]>
27+
where
28+
A: PartialEq<B>,
29+
{
30+
VecDeque::<A>::new()
31+
}
32+
33+
pub fn yes_vecdeque_partial_eq_ref_mut_array<'a, A, B>() -> impl PartialEq<&'a mut [B; 32]>
34+
where
35+
A: PartialEq<B>,
36+
{
37+
VecDeque::<A>::new()
38+
}
39+
40+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
pub fn no_vec_partial_eq_array<A, B>() -> impl PartialEq<[B; 33]>
2+
//~^ ERROR arrays only have std trait implementations for lengths 0..=32
3+
where
4+
A: PartialEq<B>,
5+
{
6+
Vec::<A>::new()
7+
}
8+
9+
pub fn no_vec_partial_eq_ref_array<'a, A, B>() -> impl PartialEq<&'a [B; 33]>
10+
//~^ ERROR arrays only have std trait implementations for lengths 0..=32
11+
where
12+
A: PartialEq<B>,
13+
{
14+
Vec::<A>::new()
15+
}
16+
17+
use std::collections::VecDeque;
18+
19+
pub fn no_vecdeque_partial_eq_array<A, B>() -> impl PartialEq<[B; 33]>
20+
//~^ ERROR arrays only have std trait implementations for lengths 0..=32
21+
where
22+
A: PartialEq<B>,
23+
{
24+
VecDeque::<A>::new()
25+
}
26+
27+
pub fn no_vecdeque_partial_eq_ref_array<'a, A, B>() -> impl PartialEq<&'a [B; 33]>
28+
//~^ ERROR arrays only have std trait implementations for lengths 0..=32
29+
where
30+
A: PartialEq<B>,
31+
{
32+
VecDeque::<A>::new()
33+
}
34+
35+
pub fn no_vecdeque_partial_eq_ref_mut_array<'a, A, B>() -> impl PartialEq<&'a mut [B; 33]>
36+
//~^ ERROR arrays only have std trait implementations for lengths 0..=32
37+
where
38+
A: PartialEq<B>,
39+
{
40+
VecDeque::<A>::new()
41+
}
42+
43+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
error[E0277]: arrays only have std trait implementations for lengths 0..=32
2+
--> $DIR/alloc-traits-no-impls-length-33.rs:1:43
3+
|
4+
LL | pub fn no_vec_partial_eq_array<A, B>() -> impl PartialEq<[B; 33]>
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[B; 33]`
6+
|
7+
= note: required because of the requirements on the impl of `std::cmp::PartialEq<[B; 33]>` for `std::vec::Vec<A>`
8+
= note: the return type of a function must have a statically known size
9+
10+
error[E0277]: arrays only have std trait implementations for lengths 0..=32
11+
--> $DIR/alloc-traits-no-impls-length-33.rs:9:51
12+
|
13+
LL | pub fn no_vec_partial_eq_ref_array<'a, A, B>() -> impl PartialEq<&'a [B; 33]>
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[B; 33]`
15+
|
16+
= note: required because of the requirements on the impl of `std::cmp::PartialEq<&'a [B; 33]>` for `std::vec::Vec<A>`
17+
= note: the return type of a function must have a statically known size
18+
19+
error[E0277]: arrays only have std trait implementations for lengths 0..=32
20+
--> $DIR/alloc-traits-no-impls-length-33.rs:19:48
21+
|
22+
LL | pub fn no_vecdeque_partial_eq_array<A, B>() -> impl PartialEq<[B; 33]>
23+
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[B; 33]`
24+
|
25+
= note: required because of the requirements on the impl of `std::cmp::PartialEq<[B; 33]>` for `std::collections::VecDeque<A>`
26+
= note: the return type of a function must have a statically known size
27+
28+
error[E0277]: arrays only have std trait implementations for lengths 0..=32
29+
--> $DIR/alloc-traits-no-impls-length-33.rs:27:56
30+
|
31+
LL | pub fn no_vecdeque_partial_eq_ref_array<'a, A, B>() -> impl PartialEq<&'a [B; 33]>
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[B; 33]`
33+
|
34+
= note: required because of the requirements on the impl of `std::cmp::PartialEq<&'a [B; 33]>` for `std::collections::VecDeque<A>`
35+
= note: the return type of a function must have a statically known size
36+
37+
error[E0277]: arrays only have std trait implementations for lengths 0..=32
38+
--> $DIR/alloc-traits-no-impls-length-33.rs:35:60
39+
|
40+
LL | pub fn no_vecdeque_partial_eq_ref_mut_array<'a, A, B>() -> impl PartialEq<&'a mut [B; 33]>
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[B; 33]`
42+
|
43+
= note: required because of the requirements on the impl of `std::cmp::PartialEq<&'a mut [B; 33]>` for `std::collections::VecDeque<A>`
44+
= note: the return type of a function must have a statically known size
45+
46+
error: aborting due to 5 previous errors
47+
48+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)