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

Failed to derive Clone for structs with fields which have references #90

Open
yubrot opened this issue Feb 1, 2021 · 1 comment
Open
Labels
bug The crate does not work as expected

Comments

@yubrot
Copy link

yubrot commented Feb 1, 2021

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
@Arnavion
Copy link

Arnavion commented Jul 11, 2023

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,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The crate does not work as expected
Projects
None yet
Development

No branches or pull requests

2 participants