From 5e6255b8f95020cab67bff8c45a95f6e59cb64c2 Mon Sep 17 00:00:00 2001 From: Ansh Jaiswal Date: Sun, 5 Jul 2026 17:35:24 +0530 Subject: [PATCH] Add PartialEq> for [T; N] and &[T; N] Enable comparing arrays with Vec in both directions, matching the existing Vec == [U; N] impls. Fixes #149017 --- library/alloc/src/vec/partial_eq.rs | 4 ++-- tests/ui/array-vec-partialeq.rs | 12 ++++++++++++ tests/ui/consts/too_generic_eval_ice.current.stderr | 4 ++-- .../assert-ne-no-invalid-help-issue-146204.stderr | 8 ++++---- 4 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 tests/ui/array-vec-partialeq.rs diff --git a/library/alloc/src/vec/partial_eq.rs b/library/alloc/src/vec/partial_eq.rs index 943c9309836d3..363dcab7cb7c0 100644 --- a/library/alloc/src/vec/partial_eq.rs +++ b/library/alloc/src/vec/partial_eq.rs @@ -34,12 +34,12 @@ __impl_slice_eq1! { [] Cow<'_, [T]>, &[U] where T: Clone, #[stable(feature = "ru __impl_slice_eq1! { [] Cow<'_, [T]>, &mut [U] where T: Clone, #[stable(feature = "rust1", since = "1.0.0")] } __impl_slice_eq1! { const, [A: Allocator, const N: usize] Vec, [U; N], #[rustc_const_unstable(feature = "const_cmp", issue = "143800")] #[stable(feature = "rust1", since = "1.0.0")] } __impl_slice_eq1! { const, [A: Allocator, const N: usize] Vec, &[U; N], #[rustc_const_unstable(feature = "const_cmp", issue = "143800")] #[stable(feature = "rust1", since = "1.0.0")] } +__impl_slice_eq1! { const, [const N: usize] [T; N], Vec, #[rustc_const_unstable(feature = "const_cmp", issue = "143800")] #[stable(feature = "array_vec_partialeq", since = "CURRENT_RUSTC_VERSION")] } +__impl_slice_eq1! { const, [const N: usize] &[T; N], Vec, #[rustc_const_unstable(feature = "const_cmp", issue = "143800")] #[stable(feature = "array_vec_partialeq", since = "CURRENT_RUSTC_VERSION")] } // NOTE: some less important impls are omitted to reduce code bloat // FIXME(Centril): Reconsider this? //__impl_slice_eq1! { [const N: usize] Vec, &mut [B; N], } -//__impl_slice_eq1! { [const N: usize] [A; N], Vec, } -//__impl_slice_eq1! { [const N: usize] &[A; N], Vec, } //__impl_slice_eq1! { [const N: usize] &mut [A; N], Vec, } //__impl_slice_eq1! { [const N: usize] Cow<'a, [A]>, [B; N], } //__impl_slice_eq1! { [const N: usize] Cow<'a, [A]>, &[B; N], } diff --git a/tests/ui/array-vec-partialeq.rs b/tests/ui/array-vec-partialeq.rs new file mode 100644 index 0000000000000..3c337ee005eb3 --- /dev/null +++ b/tests/ui/array-vec-partialeq.rs @@ -0,0 +1,12 @@ +//@ check-pass + +fn main() { + let x = vec![1]; + let y = [1]; + assert!(x == y); + assert!(y == x); + + let z = [1, 2, 3]; + assert!(z == vec![1, 2, 3]); + assert!(&z == vec![1, 2, 3]); +} diff --git a/tests/ui/consts/too_generic_eval_ice.current.stderr b/tests/ui/consts/too_generic_eval_ice.current.stderr index 02bcaee80154f..00a9d714c273b 100644 --- a/tests/ui/consts/too_generic_eval_ice.current.stderr +++ b/tests/ui/consts/too_generic_eval_ice.current.stderr @@ -30,6 +30,7 @@ LL | [5; Self::HOST_SIZE] == [6; 0] | = help: the trait `PartialEq<[{integer}; 0]>` is not implemented for `[{integer}; Self::HOST_SIZE]` = help: the following other types implement trait `PartialEq`: + `&[T; N]` implements `PartialEq>` `&[T]` implements `PartialEq>` `&[T]` implements `PartialEq<[U; N]>` `&[u8; N]` implements `PartialEq` @@ -37,8 +38,7 @@ LL | [5; Self::HOST_SIZE] == [6; 0] `&[u8]` implements `PartialEq` `&[u8]` implements `PartialEq` `&mut [T]` implements `PartialEq>` - `&mut [T]` implements `PartialEq<[U; N]>` - and 11 others + and 13 others error: aborting due to 4 previous errors diff --git a/tests/ui/macros/assert-ne-no-invalid-help-issue-146204.stderr b/tests/ui/macros/assert-ne-no-invalid-help-issue-146204.stderr index 359deee7bee4b..a979992d4c848 100644 --- a/tests/ui/macros/assert-ne-no-invalid-help-issue-146204.stderr +++ b/tests/ui/macros/assert-ne-no-invalid-help-issue-146204.stderr @@ -6,6 +6,7 @@ LL | assert_ne!(buf, b"----"); | = help: the trait `PartialEq<&[u8; 4]>` is not implemented for `[u8; 4]` = help: the following other types implement trait `PartialEq`: + `&[T; N]` implements `PartialEq>` `&[T]` implements `PartialEq>` `&[T]` implements `PartialEq<[U; N]>` `&[u8; N]` implements `PartialEq` @@ -13,8 +14,7 @@ LL | assert_ne!(buf, b"----"); `&[u8]` implements `PartialEq` `&[u8]` implements `PartialEq` `&mut [T]` implements `PartialEq>` - `&mut [T]` implements `PartialEq<[U; N]>` - and 11 others + and 13 others error[E0277]: can't compare `[u8; 4]` with `&[u8; 4]` --> $DIR/assert-ne-no-invalid-help-issue-146204.rs:19:5 @@ -24,6 +24,7 @@ LL | assert_eq!(buf, b"----"); | = help: the trait `PartialEq<&[u8; 4]>` is not implemented for `[u8; 4]` = help: the following other types implement trait `PartialEq`: + `&[T; N]` implements `PartialEq>` `&[T]` implements `PartialEq>` `&[T]` implements `PartialEq<[U; N]>` `&[u8; N]` implements `PartialEq` @@ -31,8 +32,7 @@ LL | assert_eq!(buf, b"----"); `&[u8]` implements `PartialEq` `&[u8]` implements `PartialEq` `&mut [T]` implements `PartialEq>` - `&mut [T]` implements `PartialEq<[U; N]>` - and 11 others + and 13 others error[E0277]: can't compare `[u8; 4]` with `&[u8; 4]` --> $DIR/assert-ne-no-invalid-help-issue-146204.rs:5:30