Skip to content

Derive Clone not working with struct containing generic type not implementing Clone #41481

Closed
@antoyo

Description

@antoyo

Hello.
The following code:

use std::rc::Rc;

struct NonCloneI32(i32);

#[derive(Clone)]
struct Generic<T> {
    value: Rc<T>,
}

/*impl<T> Clone for Generic<T> {
    fn clone(&self) -> Self {
        Generic {
            value: self.value.clone(),
        }
    }
}*/

fn main() {
    let gen: Generic<NonCloneI32> = Generic {
        value: Rc::new(NonCloneI32(42)),
    };
    let gen2: Generic<NonCloneI32> = gen.clone();
}

causes the following compiler error:

error: no method named `clone` found for type `Generic<NonCloneI32>` in the current scope
  --> src/main.rs:22:42
   |
22 |     let gen2: Generic<NonCloneI32> = gen.clone();
   |                                          ^^^^^
   |
   = note: the method `clone` exists but the following trait bounds were not satisfied: `NonCloneI32 : std::clone::Clone`
   = help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `clone`, perhaps you need to implement it:
   = help: candidate #1: `std::clone::Clone`

But I believe it should work as I can write the impl myself (commented in the code above) and Rc is Clone.

The same error is triggered on versions rustc 1.16.0 (30cf806ef 2017-03-10) and rustc 1.18.0-nightly (bbdaad0dc 2017-04-14).

Thanks to fix this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions