diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:57:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:57:31 +0000 |
commit | dc0db358abe19481e475e10c32149b53370f1a1c (patch) | |
tree | ab8ce99c4b255ce46f99ef402c27916055b899ee /vendor/serde_derive/src | |
parent | Releasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff) | |
download | rustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip |
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/serde_derive/src')
-rw-r--r-- | vendor/serde_derive/src/de.rs | 92 | ||||
-rw-r--r-- | vendor/serde_derive/src/dummy.rs | 22 | ||||
-rw-r--r-- | vendor/serde_derive/src/internals/ast.rs | 17 | ||||
-rw-r--r-- | vendor/serde_derive/src/internals/attr.rs | 9 | ||||
-rw-r--r-- | vendor/serde_derive/src/internals/ctxt.rs | 17 | ||||
-rw-r--r-- | vendor/serde_derive/src/lib.rs | 11 | ||||
-rw-r--r-- | vendor/serde_derive/src/pretend.rs | 29 | ||||
-rw-r--r-- | vendor/serde_derive/src/ser.rs | 18 |
8 files changed, 111 insertions, 104 deletions
diff --git a/vendor/serde_derive/src/de.rs b/vendor/serde_derive/src/de.rs index 4d60d5a6f..a834a736e 100644 --- a/vendor/serde_derive/src/de.rs +++ b/vendor/serde_derive/src/de.rs @@ -15,9 +15,7 @@ use this; use std::collections::BTreeSet; use std::ptr; -pub fn expand_derive_deserialize( - input: &mut syn::DeriveInput, -) -> Result<TokenStream, Vec<syn::Error>> { +pub fn expand_derive_deserialize(input: &mut syn::DeriveInput) -> syn::Result<TokenStream> { replace_receiver(input); let ctxt = Ctxt::new(); @@ -69,8 +67,6 @@ pub fn expand_derive_deserialize( Ok(dummy::wrap_in_const( cont.attrs.custom_serde_path(), - "DESERIALIZE", - ident, impl_block, )) } @@ -1171,6 +1167,22 @@ fn deserialize_enum( variants: &[Variant], cattrs: &attr::Container, ) -> Fragment { + // The variants have already been checked (in ast.rs) that all untagged variants appear at the end + match variants.iter().position(|var| var.attrs.untagged()) { + Some(variant_idx) => { + let (tagged, untagged) = variants.split_at(variant_idx); + let tagged_frag = Expr(deserialize_homogeneous_enum(params, tagged, cattrs)); + deserialize_untagged_enum_after(params, untagged, cattrs, Some(tagged_frag)) + } + None => deserialize_homogeneous_enum(params, variants, cattrs), + } +} + +fn deserialize_homogeneous_enum( + params: &Parameters, + variants: &[Variant], + cattrs: &attr::Container, +) -> Fragment { match cattrs.tag() { attr::TagType::External => deserialize_externally_tagged_enum(params, variants, cattrs), attr::TagType::Internal { tag } => { @@ -1671,6 +1683,16 @@ fn deserialize_untagged_enum( variants: &[Variant], cattrs: &attr::Container, ) -> Fragment { + let first_attempt = None; + deserialize_untagged_enum_after(params, variants, cattrs, first_attempt) +} + +fn deserialize_untagged_enum_after( + params: &Parameters, + variants: &[Variant], + cattrs: &attr::Container, + first_attempt: Option<Expr>, +) -> Fragment { let attempts = variants .iter() .filter(|variant| !variant.attrs.skip_deserializing()) @@ -1679,12 +1701,10 @@ fn deserialize_untagged_enum( params, variant, cattrs, - quote!( - _serde::__private::de::ContentRefDeserializer::<__D::Error>::new(&__content) - ), + quote!(__deserializer), )) }); - + let attempts = first_attempt.into_iter().chain(attempts); // TODO this message could be better by saving the errors from the failed // attempts. The heuristic used by TOML was to count the number of fields // processed before an error, and use the error that happened after the @@ -1699,6 +1719,7 @@ fn deserialize_untagged_enum( quote_block! { let __content = try!(<_serde::__private::de::Content as _serde::Deserialize>::deserialize(__deserializer)); + let __deserializer = _serde::__private::de::ContentRefDeserializer::<__D::Error>::new(&__content); #( if let _serde::__private::Ok(__ok) = #attempts { @@ -3086,23 +3107,31 @@ struct DeTypeGenerics<'a>(&'a Parameters); #[cfg(feature = "deserialize_in_place")] struct InPlaceTypeGenerics<'a>(&'a Parameters); +fn de_type_generics_to_tokens( + mut generics: syn::Generics, + borrowed: &BorrowedLifetimes, + tokens: &mut TokenStream, +) { + if borrowed.de_lifetime_param().is_some() { + let def = syn::LifetimeParam { + attrs: Vec::new(), + lifetime: syn::Lifetime::new("'de", Span::call_site()), + colon_token: None, + bounds: Punctuated::new(), + }; + // Prepend 'de lifetime to list of generics + generics.params = Some(syn::GenericParam::Lifetime(def)) + .into_iter() + .chain(generics.params) + .collect(); + } + let (_, ty_generics, _) = generics.split_for_impl(); + ty_generics.to_tokens(tokens); +} + impl<'a> ToTokens for DeTypeGenerics<'a> { fn to_tokens(&self, tokens: &mut TokenStream) { - let mut generics = self.0.generics.clone(); - if self.0.borrowed.de_lifetime_param().is_some() { - let def = syn::LifetimeParam { - attrs: Vec::new(), - lifetime: syn::Lifetime::new("'de", Span::call_site()), - colon_token: None, - bounds: Punctuated::new(), - }; - generics.params = Some(syn::GenericParam::Lifetime(def)) - .into_iter() - .chain(generics.params) - .collect(); - } - let (_, ty_generics, _) = generics.split_for_impl(); - ty_generics.to_tokens(tokens); + de_type_generics_to_tokens(self.0.generics.clone(), &self.0.borrowed, tokens); } } @@ -3115,20 +3144,7 @@ impl<'a> ToTokens for InPlaceTypeGenerics<'a> { .chain(generics.params) .collect(); - if self.0.borrowed.de_lifetime_param().is_some() { - let def = syn::LifetimeParam { - attrs: Vec::new(), - lifetime: syn::Lifetime::new("'de", Span::call_site()), - colon_token: None, - bounds: Punctuated::new(), - }; - generics.params = Some(syn::GenericParam::Lifetime(def)) - .into_iter() - .chain(generics.params) - .collect(); - } - let (_, ty_generics, _) = generics.split_for_impl(); - ty_generics.to_tokens(tokens); + de_type_generics_to_tokens(generics, &self.0.borrowed, tokens); } } diff --git a/vendor/serde_derive/src/dummy.rs b/vendor/serde_derive/src/dummy.rs index 2be502713..57e8eabf2 100644 --- a/vendor/serde_derive/src/dummy.rs +++ b/vendor/serde_derive/src/dummy.rs @@ -1,23 +1,11 @@ -use proc_macro2::{Ident, TokenStream}; -use quote::format_ident; +use proc_macro2::TokenStream; use syn; use try; -pub fn wrap_in_const( - serde_path: Option<&syn::Path>, - trait_: &str, - ty: &Ident, - code: TokenStream, -) -> TokenStream { +pub fn wrap_in_const(serde_path: Option<&syn::Path>, code: TokenStream) -> TokenStream { let try_replacement = try::replacement(); - let dummy_const = if cfg!(no_underscore_consts) { - format_ident!("_IMPL_{}_FOR_{}", trait_, unraw(ty)) - } else { - format_ident!("_") - }; - let use_serde = match serde_path { Some(path) => quote! { use #path as _serde; @@ -31,14 +19,10 @@ pub fn wrap_in_const( quote! { #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] - const #dummy_const: () = { + const _: () = { #use_serde #try_replacement #code }; } } - -fn unraw(ident: &Ident) -> String { - ident.to_string().trim_start_matches("r#").to_owned() -} diff --git a/vendor/serde_derive/src/internals/ast.rs b/vendor/serde_derive/src/internals/ast.rs index 2a6950b2a..8bcb0ecd1 100644 --- a/vendor/serde_derive/src/internals/ast.rs +++ b/vendor/serde_derive/src/internals/ast.rs @@ -140,7 +140,7 @@ fn enum_from_ast<'a>( variants: &'a Punctuated<syn::Variant, Token![,]>, container_default: &attr::Default, ) -> Vec<Variant<'a>> { - variants + let variants: Vec<Variant> = variants .iter() .map(|variant| { let attrs = attr::Variant::from_ast(cx, variant); @@ -154,7 +154,20 @@ fn enum_from_ast<'a>( original: variant, } }) - .collect() + .collect(); + + let index_of_last_tagged_variant = variants + .iter() + .rposition(|variant| !variant.attrs.untagged()); + if let Some(index_of_last_tagged_variant) = index_of_last_tagged_variant { + for variant in &variants[..index_of_last_tagged_variant] { + if variant.attrs.untagged() { + cx.error_spanned_by(&variant.ident, "all variants with the #[serde(untagged)] attribute must be placed at the end of the enum"); + } + } + } + + variants } fn struct_from_ast<'a>( diff --git a/vendor/serde_derive/src/internals/attr.rs b/vendor/serde_derive/src/internals/attr.rs index b0a7d08a2..bff82191b 100644 --- a/vendor/serde_derive/src/internals/attr.rs +++ b/vendor/serde_derive/src/internals/attr.rs @@ -740,6 +740,7 @@ pub struct Variant { serialize_with: Option<syn::ExprPath>, deserialize_with: Option<syn::ExprPath>, borrow: Option<BorrowAttribute>, + untagged: bool, } struct BorrowAttribute { @@ -762,6 +763,7 @@ impl Variant { let mut serialize_with = Attr::none(cx, SERIALIZE_WITH); let mut deserialize_with = Attr::none(cx, DESERIALIZE_WITH); let mut borrow = Attr::none(cx, BORROW); + let mut untagged = BoolAttr::none(cx, UNTAGGED); for attr in &variant.attrs { if attr.path() != SERDE { @@ -879,6 +881,8 @@ impl Variant { cx.error_spanned_by(variant, msg); } } + } else if meta.path == UNTAGGED { + untagged.set_true(&meta.path); } else { let path = meta.path.to_token_stream().to_string().replace(' ', ""); return Err( @@ -905,6 +909,7 @@ impl Variant { serialize_with: serialize_with.get(), deserialize_with: deserialize_with.get(), borrow: borrow.get(), + untagged: untagged.get(), } } @@ -956,6 +961,10 @@ impl Variant { pub fn deserialize_with(&self) -> Option<&syn::ExprPath> { self.deserialize_with.as_ref() } + + pub fn untagged(&self) -> bool { + self.untagged + } } /// Represents field attribute information diff --git a/vendor/serde_derive/src/internals/ctxt.rs b/vendor/serde_derive/src/internals/ctxt.rs index d692c2a44..707bed90e 100644 --- a/vendor/serde_derive/src/internals/ctxt.rs +++ b/vendor/serde_derive/src/internals/ctxt.rs @@ -44,12 +44,19 @@ impl Ctxt { } /// Consume this object, producing a formatted error string if there are errors. - pub fn check(self) -> Result<(), Vec<syn::Error>> { - let errors = self.errors.borrow_mut().take().unwrap(); - match errors.len() { - 0 => Ok(()), - _ => Err(errors), + pub fn check(self) -> syn::Result<()> { + let mut errors = self.errors.borrow_mut().take().unwrap().into_iter(); + + let mut combined = match errors.next() { + Some(first) => first, + None => return Ok(()), + }; + + for rest in errors { + combined.combine(rest); } + + Err(combined) } } diff --git a/vendor/serde_derive/src/lib.rs b/vendor/serde_derive/src/lib.rs index 947033b78..e632cc9f5 100644 --- a/vendor/serde_derive/src/lib.rs +++ b/vendor/serde_derive/src/lib.rs @@ -13,7 +13,7 @@ //! //! [https://serde.rs/derive.html]: https://serde.rs/derive.html -#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.160")] +#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.164")] #![allow(unknown_lints, bare_trait_objects)] // Ignored clippy lints #![allow( @@ -92,7 +92,7 @@ mod try; pub fn derive_serialize(input: TokenStream) -> TokenStream { let mut input = parse_macro_input!(input as DeriveInput); ser::expand_derive_serialize(&mut input) - .unwrap_or_else(to_compile_errors) + .unwrap_or_else(syn::Error::into_compile_error) .into() } @@ -100,11 +100,6 @@ pub fn derive_serialize(input: TokenStream) -> TokenStream { pub fn derive_deserialize(input: TokenStream) -> TokenStream { let mut input = parse_macro_input!(input as DeriveInput); de::expand_derive_deserialize(&mut input) - .unwrap_or_else(to_compile_errors) + .unwrap_or_else(syn::Error::into_compile_error) .into() } - -fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream { - let compile_errors = errors.iter().map(syn::Error::to_compile_error); - quote!(#(#compile_errors)*) -} diff --git a/vendor/serde_derive/src/pretend.rs b/vendor/serde_derive/src/pretend.rs index d7b953d63..7decbaa52 100644 --- a/vendor/serde_derive/src/pretend.rs +++ b/vendor/serde_derive/src/pretend.rs @@ -97,29 +97,14 @@ fn pretend_fields_used_struct_packed(cont: &Container, fields: &[Field]) -> Toke let members = fields.iter().map(|field| &field.member).collect::<Vec<_>>(); - #[cfg(not(no_ptr_addr_of))] - { - quote! { - match _serde::__private::None::<&#type_ident #ty_generics> { - _serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => { - #( - let _ = _serde::__private::ptr::addr_of!(__v.#members); - )* - } - _ => {} - } - } - } - - #[cfg(no_ptr_addr_of)] - { - let placeholders = (0usize..).map(|i| format_ident!("__v{}", i)); - - quote! { - match _serde::__private::None::<#type_ident #ty_generics> { - _serde::__private::Some(#type_ident { #(#members: #placeholders),* }) => {} - _ => {} + quote! { + match _serde::__private::None::<&#type_ident #ty_generics> { + _serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => { + #( + let _ = _serde::__private::ptr::addr_of!(__v.#members); + )* } + _ => {} } } } diff --git a/vendor/serde_derive/src/ser.rs b/vendor/serde_derive/src/ser.rs index f223f713a..b9a9dce21 100644 --- a/vendor/serde_derive/src/ser.rs +++ b/vendor/serde_derive/src/ser.rs @@ -10,9 +10,7 @@ use internals::{attr, replace_receiver, Ctxt, Derive}; use pretend; use this; -pub fn expand_derive_serialize( - input: &mut syn::DeriveInput, -) -> Result<TokenStream, Vec<syn::Error>> { +pub fn expand_derive_serialize(input: &mut syn::DeriveInput) -> syn::Result<TokenStream> { replace_receiver(input); let ctxt = Ctxt::new(); @@ -59,8 +57,6 @@ pub fn expand_derive_serialize( Ok(dummy::wrap_in_const( cont.attrs.custom_serde_path(), - "SERIALIZE", - ident, impl_block, )) } @@ -477,17 +473,19 @@ fn serialize_variant( } }; - let body = Match(match cattrs.tag() { - attr::TagType::External => { + let body = Match(match (cattrs.tag(), variant.attrs.untagged()) { + (attr::TagType::External, false) => { serialize_externally_tagged_variant(params, variant, variant_index, cattrs) } - attr::TagType::Internal { tag } => { + (attr::TagType::Internal { tag }, false) => { serialize_internally_tagged_variant(params, variant, cattrs, tag) } - attr::TagType::Adjacent { tag, content } => { + (attr::TagType::Adjacent { tag, content }, false) => { serialize_adjacently_tagged_variant(params, variant, cattrs, tag, content) } - attr::TagType::None => serialize_untagged_variant(params, variant, cattrs), + (attr::TagType::None, _) | (_, true) => { + serialize_untagged_variant(params, variant, cattrs) + } }); quote! { |