Skip to content

rustc error report suggests uncompilable #[derive(PartialEq.into())] #123132

Open
@sgtatham

Description

@sgtatham

Code

pub enum WeirdOption<T> {
    None,
    Some(T),
}

impl<T: PartialEq> PartialEq for WeirdOption<T> {
    fn eq(&self, _other: &Self) -> bool {
        todo!()
    }
}

impl<T: PartialEq<U>, U> PartialEq<WeirdOption<T>> for Option<U> {
    fn eq(&self, _other: &WeirdOption<T>) -> bool {
        todo!()
    }
}

impl<T> From<T> for WeirdOption<T> {
    fn from(_val: T) -> WeirdOption<T> {
        todo!()
    }
}

struct Foo;

#[derive(PartialEq)]
struct Bar {
    foo: Option<Foo>,
}

fn main() {
    println!("Hello, world!");
}

Current output

Compiling rustc-nonsense2 v0.1.0 (/r)
error[E0308]: mismatched types
  --> src/main.rs:28:5
   |
26 | #[derive(PartialEq)]
   |          --------- in this derive macro expansion
27 | struct Bar {
28 |     foo: Option<Foo>,
   |     ^^^^^^^^^^^^^^^^ expected `WeirdOption<_>`, found `Option<Foo>`
   |
   = note: expected enum `WeirdOption<_>`
              found enum `Option<Foo>`
   = note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: call `Into::into` on this expression to convert `Option<Foo>` into `WeirdOption<_>`
   |
26 | #[derive(PartialEq.into())]
   |                   +++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `rustc-nonsense2` (bin "rustc-nonsense2") due to 1 previous error

Desired output

Compiling rustc-nonsense2 v0.1.0 (/r)
error[E0369]: binary operation `==` cannot be applied to type `Option<Foo>`
  --> src/main.rs:28:5
   |
26 | #[derive(PartialEq)]
   |          --------- in this derive macro expansion
27 | struct Bar {
28 |     foo: Option<Foo>,
   |     ^^^^^^^^^^^^^^^^
   |
note: an implementation of `PartialEq` might be missing for `Foo`
  --> src/main.rs:24:1
   |
24 | struct Foo;
   | ^^^^^^^^^^ must implement `PartialEq`
   = note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider annotating `Foo` with `#[derive(PartialEq)]`
   |
24 + #[derive(PartialEq)]
25 | struct Foo;
   |

For more information about this error, try `rustc --explain E0369`.
error: could not compile `rustc-nonsense2` (bin "rustc-nonsense2") due to 1 previous error

Rationale and extra context

The compile failure is legitimate: I've tried to derive PartialEq for Bar, but Foo is not PartialEq in turn, and therefore, neither is Option<Foo>.

But the existence of WeirdOption in the compilation, even though nothing refers to it, has caused rustc to get so confused that it suggests I write #[derive(PartialEq.into())], which is not even syntactically legal, and as far as I can see, wouldn't mean anything comprehensible if it were.

If WeirdOption and all its impls are commented out, then I get a much more sensible error message, telling me that Foo needs to implement PartialEq, and suggesting that I try doing this with a derive. That's the text that I've pasted into "Desired output" as a suggestion of better output in this case.

(This is a cut-down test case from application code; WeirdOption was originally rkyv::ArchivedOption, which was a transitive dependency that I hadn't even known was in my compile at all until seeing this error message.)

I looked for existing issues along these lines; the closest I found was #122919, about a similarly spurious ref in a suggestion. But that one is fixed as of 2024-03-22, and this still fails in 2024-03-26, so I don't think they're the same issue.

Other cases

No response

Rust Version

$ rustc --version --verbose
rustc 1.79.0-nightly (47ecded35 2024-03-26)
binary: rustc
commit-hash: 47ecded3525392b77843534bed69b4302f9af8d2
commit-date: 2024-03-26
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.2

Anything else?

No response

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`D-confusingDiagnostics: Confusing error or lint that should be reworked.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