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

clone macro fails to compile indicating trait bounds are not satisfied but I am not sure why it is required to be satisfied because all struct members are clonnable #121102

Closed
softstream-link opened this issue Feb 14, 2024 · 1 comment
Labels
C-bug Category: This is a bug.

Comments

@softstream-link
Copy link

softstream-link commented Feb 14, 2024

I tried this code:

use std::sync::Arc;

trait Blah {}
struct IamBlah;
impl Blah for IamBlah {}

#[derive(Debug, Clone)]
struct ShouldBeClonable<N: Blah> {
    a: Arc<N>,
}

fn main() {
    let x = ShouldBeClonable { a: Arc::new(IamBlah) };
    let y = x.clone();
    println!("{:?}", y);
}

I expected to see this happen: I expect this to compile

At the same time this works if implemented manually

use std::sync::Arc;

trait Blah {}

#[derive(Debug)]
struct IamBlah;
impl Blah for IamBlah {}

// #[derive(Debug, Clone)]
#[derive(Debug)]
struct ShouldBeClonable<N: Blah> {
    a: Arc<N>,
}
impl<N: Blah> Clone for ShouldBeClonable<N> {
    fn clone(&self) -> Self {
        Self { a: self.a.clone() }
    }
}

fn main() {
    let x = ShouldBeClonable { a: Arc::new(IamBlah) };
    let y = x.clone();
    println!("{:?}", y);
}

Instead, this happened: I get a compiler error indicating

error[E0599]: the method `clone` exists for struct `ShouldBeClonable<IamBlah>`, but its trait bounds were not satisfied
  --> experimentation/examples/clone_gen.rs:14:15
   |
4  | struct IamBlah;
   | -------------- doesn't satisfy `IamBlah: Clone`
...
8  | struct ShouldBeClonable<N: Blah> {
   | --------------------------------
   | |
   | method `clone` not found for this struct
   | doesn't satisfy `ShouldBeClonable<IamBlah>: Clone`
...
14 |     let y = x.clone();
   |               ^^^^^ method cannot be called on `ShouldBeClonable<IamBlah>` due to unsatisfied trait bounds
   |
note: trait bound `IamBlah: Clone` was not satisfied
  --> experimentation/examples/clone_gen.rs:7:17
   |
7  | #[derive(Debug, Clone)]
   |                 ^^^^^ unsatisfied trait bound introduced in this `derive` macro
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `clone`, perhaps you need to implement it:
           candidate #1: `Clone`
help: consider annotating `IamBlah` with `#[derive(Clone)]`
   |
4  + #[derive(Clone)]
5  | struct IamBlah;
   |

For more information about this error, try `rustc --explain E0599`.

Meta

rustc --version --verbose:

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: aarch64-apple-darwin
release: 1.75.0
LLVM version: 17.0.6
Backtrace

n/a

@softstream-link softstream-link added the C-bug Category: This is a bug. label Feb 14, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 14, 2024
@compiler-errors
Copy link
Member

This issue has some explanation for why derive(Clone) is the way it is and some other cross-links for related discussion: #108894 (comment)

I'm gonna close this as a dupe.

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

4 participants