Skip to content

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

Closed
@meithecatte

Description

@meithecatte

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`A-trait-systemArea: Trait systemC-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