Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntactically-incorrect suggestion given by Rustc for #[derive()] that does not satisfy trait bounds #91106

Closed
eddieantonio opened this issue Nov 21, 2021 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@eddieantonio
Copy link

rustc produces a syntactically-invalid fix; namely, it suggests to add a trait bound within the #[derive(...)] attribute, which does not seem right!

  • Suppose I have written my own impl<T> PartialEq for MyStruct<T> but only for T: Copy
  • Then I want to #[derive(Eq)] on MyStruct<T>

Given the following code: Rust Playground

#[derive(Eq)]
struct MyStruct<T>{
    x: T
}

impl<T> PartialEq for MyStruct<T>
where T: Copy + Eq
{
    fn eq(&self, other: &Self) -> bool {
        self.x == other.x
    }
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `T: Copy` is not satisfied
   --> src/lib.rs:1:10
    |
1   | #[derive(Eq)]
    |          ^^ the trait `Copy` is not implemented for `T`
    |
note: required because of the requirements on the impl of `PartialEq` for `MyStruct<T>`
   --> src/lib.rs:6:9
    |
6   | impl<T> PartialEq for MyStruct<T>
    |         ^^^^^^^^^     ^^^^^^^^^^^
note: required by a bound in `Eq`
    = note: this error originates in the derive macro `Eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
    |
1   | #[derive(Eq + std::marker::Copy)]
    |             +++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error

The issue is with the help: portion:

help: consider further restricting this bound
    |
1   | #[derive(Eq + std::marker::Copy)]
    |             +++++++++++++++++++

However, if I were to non-judgementally apply the suggestion... 💣💥 Rust Playground

   Compiling playground v0.0.1 (/playground)
error: expected one of `(`, `,`, `::`, or `=`, found `+`
 --> src/lib.rs:1:13
  |
1 | #[derive(Eq + std::marker::Copy)]
  |             ^ expected one of `(`, `,`, `::`, or `=`

error[E0412]: cannot find type `MyStruct` in this scope
 --> src/lib.rs:6:23
  |
6 | impl<T> PartialEq for MyStruct<T>
  |                       ^^^^^^^^ not found in this scope

For more information about this error, try `rustc --explain E0412`.
error: could not compile `playground` due to 2 previous errors

Possilbe resolutions to this issue:

  • omit the help: suggestion entirely because of how misleading it is
  • for this case in particular, a more appropriate help: suggestion is to remove the #[derive(...)] and provide an implementation of Eq manually Rust playground
@eddieantonio eddieantonio added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 21, 2021
@estebank
Copy link
Contributor

I believe this will be fixed by #90519.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants