Skip to content
Myr

Merge pull request #2847 from dtolnay/newtypewith

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

Hygiene for macro-generated newtype struct deserialization with `with` attr

authored by David Tolnay · committed by GitHub · parents 49e11ce, 79925ac · Browse files · 2 files+31 added−3 removed
This commit

State

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

Diff stats

2 files+31 added−3 removed

  • tests:+27 added−0 removed
  • other:+4 added−3 removed

2 files+31 added−3 removed

modified:serde_derive/src/de.rs+4 added-3 removed
873 unmodified lines
874 field: &Field,875) -> TokenStream {876 let delife = params.borrowed.de_lifetime();877 let field_ty = field.ty;878 let deserializer_var = quote!(__e);879880 let value = match field.attrs.deserialize_with() {881 None => {882 let span = field.original.span();883 let func = quote_spanned!(span=> <#field_ty as _serde::Deserialize>::deserialize);884 quote! {884 #func(__e)?885 #func(#deserializer_var)?886 }887 }888 Some(path) => {889 // If #path returns wrong type, error will be reported here (^^^^^).890 // We attach span of the path to the function so it will be reported891 // on the #[serde(with = "...")]892 // ^^^^^893 quote_spanned! {path.span()=>893 #path(__e)?894 #path(#deserializer_var)?895 }896 }897 };898
7 unmodified lines
906 }907908 quote! {909 #[inline]909 fn visit_newtype_struct<__E>(self, __e: __E) -> _serde::__private::Result<Self::Value, __E::Error>910 fn visit_newtype_struct<__E>(self, #deserializer_var: __E) -> _serde::__private::Result<Self::Value, __E::Error>911 where912 __E: _serde::Deserializer<#delife>,913 {914 let __field0: #field_ty = #value;
2302 unmodified lines
added:test_suite/tests/regression/issue2846.rs+27 added
1#![allow(clippy::trivially_copy_pass_by_ref)]23use serde_derive::Deserialize;45macro_rules! declare_in_macro {6 ($with:literal) => {7 #[derive(Deserialize)]8 pub struct S(9 #[serde(with = $with)]10 #[allow(dead_code)]11 i32,12 );13 };14}1516declare_in_macro!("with");1718mod with {19 use serde::Deserializer;2021 pub fn deserialize<'de, D>(_: D) -> Result<i32, D::Error>22 where23 D: Deserializer<'de>,24 {25 unimplemented!()26 }27}