Skip to content

Unhelpful error message when comparing an array of types that are not comparable #95285

Closed
@zohnannor

Description

@zohnannor

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=46491383b2d69ee047fd7312d8ba7c94

fn main() {
    struct X;
    let xs = [X, X, X];
    let eq = xs == [X, X, X];
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[E0369]: binary operation `==` cannot be applied to type `[X; 3]`
 --> src/main.rs:4:17
  |
4 |     let eq = xs == [X, X, X];
  |              -- ^^ --------- [X; 3]
  |              |
  |              [X; 3]

For more information about this error, try `rustc --explain E0369`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

Compiling playground v0.0.1 (/playground)
error[E0369]: binary operation `==` cannot be applied to type `[X; 3]`; the trait `PartialEq` is not implemented for `[X; 3]`
 --> src/main.rs:4:17
  |
4 |     let eq = xs == [X, X, X];
  |              -- ^^ --------- [X; 3]
  |              |
  |              [X; 3]
   = help: consider implementing PartialEq trait or add `#[derive(PartialEq)]` to the `X` definition:
 --> src/main.rs:2:5
   |
2  |     #[derive(PartialEq)]
++ |     ++++++++++++++++++++
3  |     struct X;

For more information about this error, try `rustc --explain E0369`.
error: could not compile `playground` due to previous error

This error could be confusing for beginners or even experienced rust programmers because it does not explain why one can't compare such type. It should point out that type is not comparable and suggest to implement PartialEq<{TYPE}> where {TYPE} is type of the thing they tried to compare it with.

For example, another error that can occur:

Compiling playground v0.0.1 (/playground)
error[E0277]: can't compare `{integer}` with `X`
 --> src/main.rs:4:22
  |
4 |     let eq = vec![1] == [X, X, X];
  |                      ^^ no implementation for `{integer} == X`
  |
  = help: the trait `PartialEq<X>` is not implemented for `{integer}`
  = note: required because of the requirements on the impl of `PartialEq<[X; 3]>` for `Vec<{integer}>`

This error message explains in details, why the comparison isn't possible to do: types Vec<{integer}> and [X; 3] are not comparable because of the absence of the PartialEq implementation. E0369 should look like it.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions