Skip to content

Commit 1e36ef5

Browse files
committed
Fix hygiene of macro-generated local variable accesses in serde(with) wrappers
1 parent 0058c72 commit 1e36ef5

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

serde_derive/src/de.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2882,13 +2882,14 @@ fn wrap_deserialize_with(
28822882
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
28832883
split_with_de_lifetime(params);
28842884
let delife = params.borrowed.de_lifetime();
2885+
let deserializer_var = quote!(__deserializer);
28852886

28862887
// If #deserialize_with returns wrong type, error will be reported here (^^^^^).
28872888
// We attach span of the path to the function so it will be reported
28882889
// on the #[serde(with = "...")]
28892890
// ^^^^^
28902891
let value = quote_spanned! {deserialize_with.span()=>
2891-
#deserialize_with(__deserializer)?
2892+
#deserialize_with(#deserializer_var)?
28922893
};
28932894
let wrapper = quote! {
28942895
#[doc(hidden)]
@@ -2899,7 +2900,7 @@ fn wrap_deserialize_with(
28992900
}
29002901

29012902
impl #de_impl_generics _serde::Deserialize<#delife> for __DeserializeWith #de_ty_generics #where_clause {
2902-
fn deserialize<__D>(__deserializer: __D) -> _serde::__private::Result<Self, __D::Error>
2903+
fn deserialize<__D>(#deserializer_var: __D) -> _serde::__private::Result<Self, __D::Error>
29032904
where
29042905
__D: _serde::Deserializer<#delife>,
29052906
{

serde_derive/src/ser.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1220,12 +1220,15 @@ fn wrap_serialize_with(
12201220
})
12211221
});
12221222

1223+
let self_var = quote!(self);
1224+
let serializer_var = quote!(__s);
1225+
12231226
// If #serialize_with returns wrong type, error will be reported on here.
12241227
// We attach span of the path to this piece so error will be reported
12251228
// on the #[serde(with = "...")]
12261229
// ^^^^^
12271230
let wrapper_serialize = quote_spanned! {serialize_with.span()=>
1228-
#serialize_with(#(self.values.#field_access, )* __s)
1231+
#serialize_with(#(#self_var.values.#field_access, )* #serializer_var)
12291232
};
12301233

12311234
quote!({
@@ -1236,7 +1239,7 @@ fn wrap_serialize_with(
12361239
}
12371240

12381241
impl #wrapper_impl_generics _serde::Serialize for __SerializeWith #wrapper_ty_generics #where_clause {
1239-
fn serialize<__S>(&self, __s: __S) -> _serde::__private::Result<__S::Ok, __S::Error>
1242+
fn serialize<__S>(&#self_var, #serializer_var: __S) -> _serde::__private::Result<__S::Ok, __S::Error>
12401243
where
12411244
__S: _serde::Serializer,
12421245
{

test_suite/tests/ui/with/incorrect_type.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ note: required by a bound in `w::serialize`
1919
error[E0061]: this function takes 1 argument but 2 arguments were supplied
2020
--> tests/ui/with/incorrect_type.rs:15:25
2121
|
22+
14 | #[derive(Serialize, Deserialize)]
23+
| --------- unexpected argument #2 of type `__S`
2224
15 | struct W(#[serde(with = "w")] u8, u8);
23-
| ^^^ unexpected argument #2 of type `__S`
25+
| ^^^
2426
|
2527
note: function defined here
2628
--> tests/ui/with/incorrect_type.rs:9:12
@@ -68,8 +70,10 @@ note: required by a bound in `w::serialize`
6870
error[E0061]: this function takes 1 argument but 2 arguments were supplied
6971
--> tests/ui/with/incorrect_type.rs:18:35
7072
|
73+
17 | #[derive(Serialize, Deserialize)]
74+
| --------- unexpected argument #2 of type `__S`
7175
18 | struct S(#[serde(serialize_with = "w::serialize")] u8, u8);
72-
| ^^^^^^^^^^^^^^ unexpected argument #2 of type `__S`
76+
| ^^^^^^^^^^^^^^
7377
|
7478
note: function defined here
7579
--> tests/ui/with/incorrect_type.rs:9:12

0 commit comments

Comments
 (0)