Skip to content

Diagnostics should recommend swapping operand order if possible #165968

Open

Description

@clarfonthey

Code

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

Current output

error[E0277]: can't compare `[{integer}; 3]` with `Vec<{integer}>`
 --> src/main.rs:4:5
  |
4 |     assert_eq!(a, v);
  |     ^^^^^^^^^^^^^^^^ no implementation for `[{integer}; 3] == Vec<{integer}>`
  |
  = help: the trait `PartialEq<Vec<{integer}>>` is not implemented for `[{integer}; 3]`
  = help: the following other types implement trait `PartialEq<Rhs>`:
            `&[T]` implements `PartialEq<Cow<'_, [U]>>`
            `&[T]` implements `PartialEq<Vec<U, A>>`
            `&[T]` implements `PartialEq<[U; N]>`
            `&[u8; N]` implements `PartialEq<ByteStr>`
            `&[u8; N]` implements `PartialEq<ByteString>`
            `&[u8]` implements `PartialEq<ByteStr>`
            `&[u8]` implements `PartialEq<ByteString>`
            `&mut [T]` implements `PartialEq<Cow<'_, [U]>>`
          and 13 others

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

Rationale and extra context

Right now, PartialEq<[T; N]> for Vec<T> exists, but not the other way around. It would be helpful for this diagnostic to simply recommend swapping operands instead of just showing other implementations.

Note that assert_eq! is a special case where as a macro we can auto-suggest this, but in other cases, showing how to swap operands should be helpful.

Other cases

This applies to pretty much all binary operations in core::ops, although you need to verify that the output is the same for them to be truly reasonable to swap. We could probably still suggest swapping the order if it's still possible.

Rust Version

rustc 1.102.0-nightly (5b7b37aef 2026-11-08)
binary: rustc
commit-hash: 5b7b37aefcb5b2fba3c2392bc9065b9e4602d494
commit-date: 2026-11-08
host: x86_64-unknown-linux-gnu
release: 1.102.0-nightly
LLVM version: 23.1.5

Anything else?

No response

Activity

  1. added
    A-diagnostics
    T-compiler
    on Nov 14, 2026
  2. estebank commented on Nov 17, 2026

    @estebank
    Contributor

    This needs to be adjusted in compiler/rustc_hir_typeck/src/op.rs:report_binop_fulfillment_errors in some cases. Do note that both the case in this ticket, and other cases in which an unmet trait bound E0277 error is emitted instead of a binop error would not get the improvement. I had an old PR to unify the errors after encountering E0277 on binops so that a failure on LHS and RHS would have consistent output regardless of position. That divergence should also be addressed, for completeness, and would simplify providing better error context.

  3. added
    D-confusing
    D-terse
    D-lack-of-suggestion
    on Nov 17, 2026
  4. Yogeshwaran-Projects commented on Nov 18, 2026

    @Yogeshwaran-Projects

    @rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-diagnosticsD-confusingD-lack-of-suggestionD-terseT-compiler

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions