From 95730dc7f7175ad9e766035c10393138d05e1f03 Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 1 May 2023 02:43:25 +0500 Subject: [PATCH] Reorder variables to match order in final quote! --- serde_derive/src/de.rs | 63 +++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index c2b01ca1..b7f82b28 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -914,8 +914,6 @@ fn deserialize_struct( deserializer: Option, untagged: &Untagged, ) -> Fragment { - let is_enum = variant_ident.is_some(); - let this_type = ¶ms.this_type; let this_value = ¶ms.this_value; let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = @@ -951,36 +949,6 @@ fn deserialize_struct( let fields_stmt = fields_stmt.map(Stmts); let visit_map = Stmts(visit_map); - let visitor_expr = quote! { - __Visitor { - marker: _serde::__private::PhantomData::<#this_type #ty_generics>, - lifetime: _serde::__private::PhantomData, - } - }; - let need_seed = deserializer.is_none(); - let dispatch = if let Some(deserializer) = deserializer { - quote! { - _serde::Deserializer::deserialize_any(#deserializer, #visitor_expr) - } - } else if is_enum && cattrs.has_flatten() { - quote! { - _serde::de::VariantAccess::newtype_variant_seed(__variant, #visitor_expr) - } - } else if is_enum { - quote! { - _serde::de::VariantAccess::struct_variant(__variant, FIELDS, #visitor_expr) - } - } else if cattrs.has_flatten() { - quote! { - _serde::Deserializer::deserialize_map(__deserializer, #visitor_expr) - } - } else { - let type_name = cattrs.name().deserialize_name(); - quote! { - _serde::Deserializer::deserialize_struct(__deserializer, #type_name, FIELDS, #visitor_expr) - } - }; - // untagged struct variants do not get a visit_seq method. The same applies to // structs that only have a map representation. let visit_seq = match *untagged { @@ -1009,6 +977,8 @@ fn deserialize_struct( _ => None, }; + let is_enum = variant_ident.is_some(); + let need_seed = deserializer.is_none(); let visitor_seed = if need_seed && is_enum && cattrs.has_flatten() { Some(quote! { impl #de_impl_generics _serde::de::DeserializeSeed<#delife> for __Visitor #de_ty_generics #where_clause { @@ -1026,6 +996,35 @@ fn deserialize_struct( None }; + let visitor_expr = quote! { + __Visitor { + marker: _serde::__private::PhantomData::<#this_type #ty_generics>, + lifetime: _serde::__private::PhantomData, + } + }; + let dispatch = if let Some(deserializer) = deserializer { + quote! { + _serde::Deserializer::deserialize_any(#deserializer, #visitor_expr) + } + } else if is_enum && cattrs.has_flatten() { + quote! { + _serde::de::VariantAccess::newtype_variant_seed(__variant, #visitor_expr) + } + } else if is_enum { + quote! { + _serde::de::VariantAccess::struct_variant(__variant, FIELDS, #visitor_expr) + } + } else if cattrs.has_flatten() { + quote! { + _serde::Deserializer::deserialize_map(__deserializer, #visitor_expr) + } + } else { + let type_name = cattrs.name().deserialize_name(); + quote! { + _serde::Deserializer::deserialize_struct(__deserializer, #type_name, FIELDS, #visitor_expr) + } + }; + quote_block! { #field_visitor