Skip to content

Error message suggests invalid Rust code with PartialEq<OtherType> and ToString #83320

Open
@ranile

Description

@ranile

Given the following code:

enum ETest {}

impl ToString for ETest {
    fn to_string(&self) -> String { String::new() }
}

#[derive(PartialEq)]
struct Props {
    to: ETest
}

impl PartialEq<String> for ETest {
    fn eq(&self, other: &String) -> bool { true }
}

struct Test {
    props: Props
}

trait Foo {
    type TProps: PartialEq;
}

impl Foo for Test {
    type TProps = Props;
}

The current output is:

error[E0308]: mismatched types
 --> src/lib.rs:9:5
  |
9 |     to: ETest
  |     ^^^^^^^^^
  |     |
  |     expected struct `String`, found enum `ETest`
  |     help: try using a conversion method: `(to: ETest).to_string()`
  |
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

If we replace our code with compiler's suggestion, we end up with this code:

#[derive(PartialEq)]
struct Props {
    (to: ETest).to_string()
}

Which is invalid Rust.

I've only observed this happen with PartialEq<Type> and ToString being implemented.
Related to: #63564

#63564 makes this error message even worse as it isn't obvious what's actually wrong here.

I reproduced this with today's nightly build (2021-03-19 f5f33ec0e0455eefa72f)

Playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=7ac536d0521cefa0cf1f088b9d88db1f

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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