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 enum does not implement PartialEq #110867

Closed
andresovela opened this issue Apr 26, 2023 · 1 comment · Fixed by #110877
Closed

Unhelpful error message when enum does not implement PartialEq #110867

andresovela opened this issue Apr 26, 2023 · 1 comment · Fixed by #110877
Assignees
Labels
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.

Comments

@andresovela
Copy link

andresovela commented Apr 26, 2023

Code

#[derive(Debug)]
enum Foo {
    Bar,
    Qux,
}

fn test() {
    let vec1 = vec![Foo::Bar, Foo::Qux];
    let vec2 = vec![Foo::Bar, Foo::Qux];
    assert_eq!(vec1, vec2);
}

Current output

error[E0369]: binary operation `==` cannot be applied to type `Vec<Foo>`
  --> src/lib.rs:10:5
   |
10 |     assert_eq!(vec1, vec2);
   |     ^^^^^^^^^^^^^^^^^^^^^^
   |     |
   |     Vec<Foo>
   |     Vec<Foo>
   |
   = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

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

In the example code, if #[derive(Debug)] is removed, the compiler is helpful and suggests to add it. In this case suggesting #[derive(PartialEq, Eq)] would be great.

Desired output

error[E0277]: `Foo` doesn't implement `PartialEq`
 --> src/lib.rs:9:5
  |
9 |     assert_eq!(vec1, vec2);
  |     ^^^^^^^^^^^^^^^^^^^^^^ `Foo` cannot be formatted using `{:?}`
  |
  = help: the trait `PartialEq` is not implemented for `Foo`
  = note: add `#[derive(PartialEq, Eq)]` to `Foo` or manually `impl PartialEq for Foo`
  = help: the trait `PartialEq` is implemented for `Vec<T, A>`
  = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider annotating `Foo` with `#[derive(PartialEq, Eq)]`
  |
1 | #[derive(PartialEq, Eq)]
  |

Some errors have detailed explanations: E0277, E0369.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `playground` due to 3 previous errors

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

@andresovela andresovela 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 Apr 26, 2023
@compiler-errors compiler-errors self-assigned this Apr 26, 2023
@compiler-errors
Copy link
Member

I think I have a good idea for a general way of solving this....

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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants