Skip to content

Confusing error deriving PartialEq when child type impls PartialEq<OtherType> #63564

Open
@cmyr

Description

@cmyr

Compiling the following code

struct TypeA;
struct TypeB;

#[derive(PartialEq)]
struct TypeC(TypeA);

impl std::cmp::PartialEq<TypeA> for TypeB {
    fn eq(&self, other: &TypeA) -> bool { true }
}

impl std::cmp::PartialEq<TypeB> for TypeA {
    fn eq(&self, other: &TypeB) -> bool { true }
}


fn main() {
    let one = TypeC(TypeA);
    let two = TypeC(TypeA);
    if one == two {
        eprintln!("yay");
    }
}

produces,

error[E0308]: mismatched types
 --> src/main.rs:8:14
  |
8 | struct TypeC(TypeA);
  |              ^^^^^ expected struct `TypeB`, found struct `TypeA`
  |
  = note: expected type `TypeB`
             found type `TypeA`

Which I found very confusing. It would be helpful, at least, if we could indicate that this error occurred during macro expansion?

Playground here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ac13781b2c40c1966e18adfb919c236c

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-type-systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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