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

Compiler suggests #[derive(Clone, Eq + Clone)] #79076

Closed
meithecatte opened this issue Nov 15, 2020 · 1 comment · Fixed by #95437
Closed

Compiler suggests #[derive(Clone, Eq + Clone)] #79076

meithecatte opened this issue Nov 15, 2020 · 1 comment · Fixed by #95437
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-trait-system Area: Trait system C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@meithecatte
Copy link
Contributor

Let's say I want my struct to be comparable with anything that can be converted into the struct itself, but the actual comparison requires Clone:

use core::cmp::PartialEq;

#[derive(Clone, Eq)]
pub struct Struct<T>(T);

impl<T, U> PartialEq<U> for Struct<T>
where
    U: Into<Struct<T>> + Clone
{
    fn eq(&self, other: &U) -> bool {
        todo!()
    }
}

(Playground)

Errors:

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

error: aborting due to previous error

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

To learn more, run the command again with --verbose.

@camelid camelid added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) labels Nov 15, 2020
@jyn514 jyn514 added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Nov 15, 2020
@estebank
Copy link
Contributor

For reference, the correct code we should be suggesting would be

use core::cmp::PartialEq;

#[derive(Clone, Eq)]
pub struct Struct<T: Clone>(T);

impl<T, U> PartialEq<U> for Struct<T>
where
    U: Into<Struct<T>> + Clone, T: Clone
{
    fn eq(&self, other: &U) -> bool {
        todo!()
    }
}

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Mar 29, 2022
…compiler-errors

diagnostics: regression test for derive bounds

Closes rust-lang#79076
@bors bors closed this as completed in 0eea334 Mar 29, 2022
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 A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-trait-system Area: Trait system C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants