Preserve span of fields in derived Clone impl

This commit is contained in:
David Tolnay 2020-11-27 12:27:26 -08:00
parent 4d73379753
commit f3b89fb23a
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 11 additions and 6 deletions

View File

@ -163,8 +163,14 @@ fn expand_struct(strct: &Struct) -> TokenStream {
quote!(*self)
} else {
let fields = strct.fields.iter().map(|field| &field.ident);
quote!(#ident {
#(#fields: ::std::clone::Clone::clone(&self.#fields),)*
let values = strct.fields.iter().map(|field| {
let ident = &field.ident;
let ty = field.ty.to_token_stream();
let span = ty.into_iter().last().unwrap().span();
quote_spanned!(span=> &self.#ident)
});
quote_spanned!(derive.span=> #ident {
#(#fields: ::std::clone::Clone::clone(#values),)*
})
};
expanded.extend(quote_spanned! {derive.span=>

View File

@ -1,8 +1,7 @@
error[E0277]: the trait bound `ffi::Other: Clone` is not satisfied
--> $DIR/derive_nonclone.rs:1:1
--> $DIR/derive_nonclone.rs:5:9
|
1 | #[cxx::bridge]
| ^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `ffi::Other`
5 | other: Other,
| ^^^^^^^^^^^^ the trait `Clone` is not implemented for `ffi::Other`
|
= note: required by `clone`
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)