From 8b30d1a4b2e71d9791db96c60782257111ddfc85 Mon Sep 17 00:00:00 2001 From: Samson <16504129+sagudev@users.noreply.github.com> Date: Tue, 19 Sep 2023 17:57:37 +0200 Subject: [PATCH] Update to syn 2 where possible (#30387) * Update to syn 2 where possible * Cleanups * Better no_trace comment Co-authored-by: Martin Robinson --------- Co-authored-by: Martin Robinson --- Cargo.lock | 36 ++++++++++++++++++--------- Cargo.toml | 4 +-- components/config_plugins/lib.rs | 31 +++++++++-------------- components/config_plugins/parse.rs | 5 ++-- components/derive_common/Cargo.toml | 4 +-- components/jstraceable_derive/lib.rs | 24 +++++++----------- components/to_shmem_derive/Cargo.toml | 4 +-- servo-tidy.toml | 3 +++ 8 files changed, 57 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 299e7ccc6f7..8fd2b3cf1aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1275,8 +1275,8 @@ dependencies = [ name = "deny_public_fields" version = "0.0.1" dependencies = [ - "syn 1.0.103", - "synstructure", + "syn 2.0.32", + "synstructure 0.13.0", ] [[package]] @@ -1294,7 +1294,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.103", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -1408,7 +1408,7 @@ name = "dom_struct" version = "0.0.1" dependencies = [ "quote", - "syn 1.0.103", + "syn 2.0.32", ] [[package]] @@ -1417,7 +1417,7 @@ version = "0.0.1" dependencies = [ "proc-macro2", "quote", - "syn 1.0.103", + "syn 2.0.32", ] [[package]] @@ -2981,8 +2981,8 @@ name = "jstraceable_derive" version = "0.0.1" dependencies = [ "proc-macro2", - "syn 1.0.103", - "synstructure", + "syn 2.0.32", + "synstructure 0.13.0", ] [[package]] @@ -3449,7 +3449,7 @@ checksum = "632647502a8bfa82458c07134791fffa7a719f00427d1afd79c3cb6d4960a982" dependencies = [ "proc-macro2", "syn 1.0.103", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -4239,7 +4239,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.103", - "synstructure", + "synstructure 0.12.6", "unicode-xid", ] @@ -5376,7 +5376,7 @@ dependencies = [ "itertools", "proc-macro2", "quote", - "syn 1.0.103", + "syn 2.0.32", ] [[package]] @@ -5840,7 +5840,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.103", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -5969,6 +5969,18 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "285ba80e733fac80aa4270fbcdf83772a79b80aa35c97075320abfee4a915b06" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.32", + "unicode-xid", +] + [[package]] name = "take_mut" version = "0.2.2" @@ -6169,7 +6181,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.103", - "synstructure", + "synstructure 0.12.6", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 01d2f59ad2a..bbb6309514d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,8 +75,8 @@ string_cache = "0.8" string_cache_codegen = "0.5" # NOTE: the sm-angle feature only enables ANGLE on Windows, not other platforms! surfman = { version = "0.8", features = ["chains", "sm-angle", "sm-angle-default"] } -syn = { version = "1", default-features = false, features = ["clone-impls", "derive", "parsing"] } -synstructure = "0.12" +syn = { version = "2", default-features = false, features = ["clone-impls", "derive", "parsing"] } +synstructure = "0.13" time = "0.1.41" tokio = "1" tokio-rustls = "0.24" diff --git a/components/config_plugins/lib.rs b/components/config_plugins/lib.rs index 608a8a5b0c6..5ea3846b4d9 100644 --- a/components/config_plugins/lib.rs +++ b/components/config_plugins/lib.rs @@ -13,10 +13,7 @@ use proc_macro2::{Span, TokenStream}; use quote::*; use syn::parse::Result; use syn::spanned::Spanned; -use syn::{ - parse_macro_input, Attribute, Ident, Lit, LitStr, Meta, MetaList, MetaNameValue, NestedMeta, - Path, -}; +use syn::{parse_macro_input, Attribute, Ident, LitStr, Path}; mod parse; use parse::*; @@ -199,23 +196,19 @@ impl Field { } fn attr_to_pref_name(attr: &Attribute) -> Option { - attr.parse_meta().ok().and_then(|meta| { - if let Meta::List(MetaList { path, nested, .. }) = meta { - if path.is_ident("serde") { - if let Some(NestedMeta::Meta(Meta::NameValue(MetaNameValue { - ref path, - lit: Lit::Str(val), - .. - }))) = nested.iter().next() - { - if path.is_ident("rename") { - return Some(val.clone()); - } - } + if attr.path().is_ident("serde") { + // If `parse_nested_meta()` fails, `result` will remain None. + let mut result = None; + let _ = attr.parse_nested_meta(|meta| { + if meta.path.is_ident("rename") { + result = Some(meta.value()?.parse()?); } - } + Ok(()) + }); + result + } else { None - }) + } } fn err(s: S, msg: &str) -> syn::Error { diff --git a/components/config_plugins/parse.rs b/components/config_plugins/parse.rs index 9fd49490913..3a390f2f07f 100644 --- a/components/config_plugins/parse.rs +++ b/components/config_plugins/parse.rs @@ -56,7 +56,8 @@ pub struct RootTypeDef { impl Parse for MacroInput { fn parse(input: ParseStream<'_>) -> Result { - let fields: Punctuated = input.parse_terminated(MacroArg::parse)?; + let fields: Punctuated = + Punctuated::parse_terminated_with(input, MacroArg::parse)?; let mut gen_accessors = None; let mut type_def = None; let mut accessor_type = None; @@ -134,7 +135,7 @@ impl Parse for NewTypeDef { #[allow(clippy::eval_order_dependence)] Ok(NewTypeDef { _braces: braced!(content in input), - fields: content.parse_terminated(Field::parse)?, + fields: Punctuated::parse_terminated_with(&content, Field::parse)?, }) } } diff --git a/components/derive_common/Cargo.toml b/components/derive_common/Cargo.toml index c3fe0456e6a..937e97f81ac 100644 --- a/components/derive_common/Cargo.toml +++ b/components/derive_common/Cargo.toml @@ -13,5 +13,5 @@ path = "lib.rs" darling = { workspace = true } proc-macro2 = { workspace = true } quote = { workspace = true } -syn = { workspace = true } -synstructure = { workspace = true } +syn = { version = "1", default-features = false, features = ["clone-impls", "derive", "parsing"] } +synstructure = "0.12" diff --git a/components/jstraceable_derive/lib.rs b/components/jstraceable_derive/lib.rs index 68d9f3a7675..f58ec04ac09 100644 --- a/components/jstraceable_derive/lib.rs +++ b/components/jstraceable_derive/lib.rs @@ -149,21 +149,15 @@ fn js_traceable_derive(s: synstructure::Structure) -> proc_macro2::TokenStream { let mut asserts = quote!(); let match_body = s.each(|binding| { for attr in binding.ast().attrs.iter() { - match attr.parse_meta().unwrap() { - syn::Meta::Path(ref path) | syn::Meta::List(syn::MetaList { ref path, .. }) => { - if path.is_ident("no_trace") { - asserts.extend(assert_not_impl_traceable(&binding.ast().ty)); - return None; - } else if path.is_ident("custom_trace") { - return Some(quote!(::trace(#binding, tracer);)); - } - }, - syn::Meta::NameValue(syn::MetaNameValue { ref path, .. }) => { - // if reason provided we can skip JSTraceable check - if path.is_ident("no_trace") { - return None; - } - }, + if attr.path().is_ident("no_trace") { + // If no reason argument is provided to `no_trace` (ie `#[no_trace="This types does not need..."]`), + // assert that the type in this bound field does not implement traceable. + if !matches!(attr.meta, syn::Meta::NameValue(_)) { + asserts.extend(assert_not_impl_traceable(&binding.ast().ty)); + } + return None; + } else if attr.path().is_ident("custom_trace") { + return Some(quote!(::trace(#binding, tracer);)); } } Some(quote!(#binding.trace(tracer);)) diff --git a/components/to_shmem_derive/Cargo.toml b/components/to_shmem_derive/Cargo.toml index f60a9a4da1f..0825d5b6834 100644 --- a/components/to_shmem_derive/Cargo.toml +++ b/components/to_shmem_derive/Cargo.toml @@ -15,5 +15,5 @@ darling = { workspace = true } derive_common = { path = "../derive_common" } proc-macro2 = { workspace = true } quote = { workspace = true } -syn = { workspace = true } -synstructure = { workspace = true } +syn = { version = "1", default-features = false, features = ["clone-impls", "derive", "parsing"] } +synstructure = "0.12" diff --git a/servo-tidy.toml b/servo-tidy.toml index eb858a59400..c6dde0044c4 100644 --- a/servo-tidy.toml +++ b/servo-tidy.toml @@ -67,7 +67,10 @@ packages = [ "foreign-types-shared", "metal", "paste", + + # Duplicated by Gecko crates that haven't been updated yet (style, shmem, derive_common, ...). "syn", + "synstructure", # style/webxr (0.62) vs. mozjs_sys (0.66) "bindgen",