Skip to content
Myr

Fix temporary value lifetime in serialize_struct

Before adoption: this commit came in with the history the project adopted, so no landing records it.

In the 2024 edition of Rust, `serde`s macros for `serialize_with` can lead to a temporary lifetime error such as: ``` error[E0716]: temporary value dropped while borrowed --> my-binary/src/main.rs:6:10 | 6 | #[derive(MyDerive)] | ^^^^^^^- | | | | | temporary value is freed at the end of this statement | creates a temporary value which is freed while still in use | borrow later used by call | in this derive macro expansion | ::: /private/tmp/life/my-project/my-macro/src/lib.rs:6:1 | 6 | pub fn my_derive(_input: TokenStream) -> TokenStream { | ---------------------------------------------------- in this expansion of `#[derive(MyDerive)]` | = note: consider using a `let` binding to create a longer lived value ``` This is because the macro code takes a reference to struct inside of a block, which then goes out of scope when `serde` passes it to a function. To resolve this, we move the reference to outside of the block, to ensure that the lifetime extends into the function call.

authored by Andrew V. Teylu · committed by Andrew V. Teylu · parent babafa5 · Browse files · 1 file+2 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Andrew V. Teylu <andrew.teylu@vector.com>

Diff stats

1 file+2 added−2 removed

  • other:+2 added−2 removed

1 file+2 added−2 removed

modified:serde_derive/src/ser.rs+2 added-2 removed
1233 unmodified lines
1234 let wrapper_serialize = quote_spanned! {serialize_with.span()=>1235 #serialize_with(#(#self_var.values.#field_access, )* #serializer_var)1236 };12371238 quote!({1238 quote!(&{1239 #[doc(hidden)]1240 struct __SerializeWith #wrapper_impl_generics #where_clause {1241 values: (#(&'__a #field_tys, )*),1242 phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
8 unmodified lines
1251 #wrapper_serialize1252 }1253 }12541255 &__SerializeWith {1255 __SerializeWith {1256 values: (#(#field_exprs, )*),1257 phantom: _serde::__private::PhantomData::<#this_type #ty_generics>,1258 }1259 })
107 unmodified lines