Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zohnannor opened this issue Mar 24, 2022 · 0 comments · Fixed by #110877
Closed

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

zohnannor opened this issue Mar 24, 2022 · 0 comments · Fixed by #110877
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@zohnannor
Copy link
Contributor

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.

@zohnannor zohnannor added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 24, 2022
@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Mar 24, 2022
@TaKO8Ki TaKO8Ki self-assigned this Mar 25, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 28, 2023
Provide better type hints when a type doesn't support a binary operator

For example, when checking whether `vec![A] == vec![A]` holds, we first evaluate the LHS's ty, then probe for any `PartialEq` implementations for that. If none is found, we report an error by evaluating `Vec<A>: PartialEq<?0>` for fulfillment errors, but the RHS is not yet evaluated and remains an inference variable `?0`!

To fix this, we evaluate the RHS and equate it to that RHS infer var `?0`, so that we are able to provide more detailed fulfillment errors for why `Vec<A>: PartialEq<Vec<A>>` doesn't hold (namely, the nested obligation `A: PartialEq<A>` doesn't hold).

Fixes rust-lang#95285
Fixes rust-lang#110867
@bors bors closed this as completed in aba9fb4 Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants