Skip to content

Error message for T <binop> U worsens if T has exactly one implementation of the relevant trait #77304

Open
@SNCPlay42

Description

@SNCPlay42

This code

struct S;

fn main() {
    let _ = S == ();
}

Emits the error message

error[E0369]: binary operation `==` cannot be applied to type `S`
 --> src/main.rs:4:15
  |
4 |     let _ = S == ();
  |             - ^^ -- ()
  |             |
  |             S
  |
  = note: an implementation of `std::cmp::PartialEq` might be missing for `S`

However, if the type S has any implementation of PartialEq<T> for a single type T that is not (), the error message becomes less helpful:

#[derive(PartialEq)]
struct S;

fn main() {
    let _ = S == ();
}
error[E0308]: mismatched types
 --> src/main.rs:5:18
  |
5 |     let _ = S == ();
  |                  ^^ expected struct `S`, found `()`

If there are two or more impls of PartialEq, the error message becomes helpful again:

#[derive(PartialEq)]
struct S;

impl PartialEq<u8> for S {
    fn eq(&self, _: &u8) -> bool {
        unimplemented!()
    }
}

fn main() {
    let _ = S == ();
}
error[E0277]: can't compare `S` with `()`
  --> src/main.rs:11:15
   |
11 |     let _ = S == ();
   |               ^^ no implementation for `S == ()`
   |
   = help: the trait `std::cmp::PartialEq<()>` is not implemented for `S`

This also applies to other binary operation traits like PartialOrd and Add.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.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