rust-derivative
rust-derivative copied to clipboard
Failed to derive Clone for structs with fields which have references
trafficstars
Describe the bug The following code will fail to derive.
To Reproduce
use derivative::*;
#[derive(Derivative, Debug)]
#[derivative(Clone(bound = ""), Copy(bound = ""))]
pub struct T<'a, A> {
a: &'a String,
b: &'a A,
}
Expected behavior
derive succeeds.
Errors
error[E0308]: mismatched types
--> src/main.rs:3:10
|
3 | #[derive(Derivative, Debug)]
| ^^^^^^^^^^
| |
| expected `&String`, found struct `String`
| help: consider borrowing here: `&Derivative`
Version (please complete the following information):
- rustup 1.23.1
- cargo 1.49.0
- rustc 1.49.0
- derivative 2.2.0
This will be fixed if the macro expansion uses UFCS Clone::clone(arg) instead of arg.clone(), ie #111
As I wrote there, the macro can be forced to use UFCS by annotating the borrow fields with clone_with = "Clone::clone", which for OP's case means:
use derivative::*;
#[derive(Derivative, Debug)]
#[derivative(Clone(bound = ""), Copy(bound = ""))]
pub struct T<'a, A> {
#[derivative(Clone(clone_with = "Clone::clone"))]
a: &'a String,
#[derivative(Clone(clone_with = "Clone::clone"))]
b: &'a A,
}