rust-lang / rust-lang/rust

Vec<T> implements PartialEq<[T; N]> but the other way round doesn't work

Open
#125,243 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Hello,

it seems that there is an oversight (or possibly it's wanted behaviour, but it felt odd to me) in how vectors and arrays can be compared, based on PartialEq implementation.

I tried this code:

fn main() {
    let a = vec![1, 2, 3];
    let b = [1, 2, 3];
    
    assert_eq!(a, b);
    assert_eq!(b, a);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3c95d140909d5ac62d769b8f663bcd47

I expected to see this happen: No errors because assert_eq should consider these two equal (or shouldn't it?)

Instead, this happened:

   Compiling playground v0.0.1 (/playground)
error[E0277]: can't compare `[{integer}; 3]` with `Vec<{integer}>`
 --> src/main.rs:6:5
  |
6 |     assert_eq!(b, a);
  |     ^^^^^^^^^^^^^^^^ no implementation for `[{integer}; 3] == Vec<{integer}>`
  |
  = help: the trait `PartialEq<Vec<{integer}>>` is not implemented for `[{integer}; 3]`
  = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider dereferencing here
 --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:40:35
  |
40|                 if !(*left_val == **right_val) {
  |                                   +

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Meta

I also checked beta and nightly (on the playground, not on my own machine; please tell me if I should repeat it here), and the results are the same over all three of these.

rustc --version --verbose:

rustc 1.77.2 (25ef9e3d8 2024-04-09)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-unknown-linux-gnu
release: 1.77.2
LLVM version: 17.0.6

Backtrace

(I did activate RUST_BACKTRACE=1 but the output was the same as before with the other runs)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the src/main.rs reproducer and verify the asymmetric comparisons on the relevant Rust toolchain. Then inspect the standard library's PartialEq implementations for Vec and arrays; done means deciding the intended behavior and adding or updating coverage for both comparison directions.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.