rust-lang / rust-lang/rust

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

Open
#123,132 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-suggestion-diagnostics D-confusing D-invalid-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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 https://github.com/rust-lang/rust/issues/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue's reduced Rust program with rustc 1.79.0-nightly and compare the diagnostic against the requested E0369 output. Trace the derive(PartialEq) expansion and suggestion generation implicated by the report, then add or update a compiler diagnostic test so the unused WeirdOption impls no longer produce the invalid derive suggestion and the missing PartialEq guidance remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.