From b7d986bd6c3cb7457b6d06baaca8588449c28e8e Mon Sep 17 00:00:00 2001 From: Luiz Carvalho Date: Mon, 1 May 2023 18:02:07 -0300 Subject: [PATCH] chore: bump syn to 2.0.15 --- Cargo.lock | 67 +++++---- sqlx-macros-core/Cargo.toml | 5 +- sqlx-macros-core/src/derives/attributes.rs | 161 +++++++++------------ sqlx-macros-core/src/derives/encode.rs | 4 +- sqlx-macros-core/src/query/args.rs | 5 +- sqlx-macros-core/src/test_attr.rs | 126 ++++++++-------- sqlx-macros/Cargo.toml | 4 +- sqlx-macros/src/lib.rs | 4 +- 8 files changed, 175 insertions(+), 201 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2e83ecd706..d491d92b92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,7 +50,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5" dependencies = [ "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -165,7 +165,7 @@ checksum = "96cf8829f67d2eab0b2dfa42c5d0ef737e0724e4a82b01b3e292456202b19716" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -298,7 +298,7 @@ dependencies = [ "borsh-schema-derive-internal", "proc-macro-crate", "proc-macro2", - "syn", + "syn 1.0.107", ] [[package]] @@ -309,7 +309,7 @@ checksum = "186b734fa1c9f6743e90c95d7233c9faab6360d1a96d4ffa19d9cfd1e9350f8a" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -320,7 +320,7 @@ checksum = "99b7ff1008316626f485991b960ade129253d4034014616b94f309a15366cc49" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -357,7 +357,7 @@ checksum = "13e576ebe98e605500b3c8041bb888e966653577172df6dd97398714eb30b9bf" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -460,7 +460,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -643,7 +643,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f877be4f7c9f246b183111634f75baa039715e3f46ce860677d3b19a69fb229c" dependencies = [ "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -670,7 +670,7 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn", + "syn 1.0.107", ] [[package]] @@ -687,7 +687,7 @@ checksum = "086c685979a698443656e5cf7856c95c642295a38599f12fb1ff76fb28d19892" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -997,7 +997,7 @@ checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -1510,7 +1510,7 @@ checksum = "b501e44f11665960c7e7fcf062c7d96a14ade4aa98116c004b2e37b5be7d736c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -1615,7 +1615,7 @@ checksum = "710faf75e1b33345361201d36d04e98ac1ed8909151a017ed384700836104c74" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -1695,7 +1695,7 @@ dependencies = [ "proc-macro-error-attr", "proc-macro2", "quote", - "syn", + "syn 1.0.107", "version_check", ] @@ -1712,9 +1712,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.51" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" dependencies = [ "unicode-ident", ] @@ -1745,14 +1745,14 @@ checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] name = "quote" -version = "1.0.20" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" dependencies = [ "proc-macro2", ] @@ -1972,7 +1972,7 @@ checksum = "ff26ed6c7c4dfc2aa9480b86a60e3c7233543a270a680e10758a507c5a4ce476" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -2164,7 +2164,7 @@ checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -2401,7 +2401,7 @@ dependencies = [ "quote", "sqlx-core", "sqlx-macros-core", - "syn", + "syn 2.0.15", ] [[package]] @@ -2423,7 +2423,7 @@ dependencies = [ "sqlx-mysql", "sqlx-postgres", "sqlx-sqlite", - "syn", + "syn 2.0.15", "tempfile", "tokio", "url", @@ -2597,6 +2597,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "2.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "tempdir" version = "0.3.7" @@ -2663,7 +2674,7 @@ checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -2737,7 +2748,7 @@ checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -2781,7 +2792,7 @@ checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -2938,7 +2949,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn", + "syn 1.0.107", "wasm-bindgen-shared", ] @@ -2972,7 +2983,7 @@ checksum = "7d94ac45fcf608c1f45ef53e748d35660f168490c10b23704c7779ab8f5c3048" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/sqlx-macros-core/Cargo.toml b/sqlx-macros-core/Cargo.toml index e7a76f60fc..cf857a26b3 100644 --- a/sqlx-macros-core/Cargo.toml +++ b/sqlx-macros-core/Cargo.toml @@ -54,8 +54,7 @@ proc-macro2 = { version = "1.0.36", default-features = false } serde = { version = "1.0.132", features = ["derive"] } serde_json = { version = "1.0.73" } sha2 = { version = "0.10.0" } -syn = { version = "1.0.84", default-features = false, features = ["full", "derive", "parsing", "printing", "clone-impls"] } +syn = { version = "2.0", default-features = false, features = ["full", "derive", "parsing", "printing", "clone-impls"] } tempfile = { version = "3.3.0" } -quote = { version = "1.0.14", default-features = false } +quote = { version = "1.0.26", default-features = false } url = { version = "2.2.2", default-features = false } - diff --git a/sqlx-macros-core/src/derives/attributes.rs b/sqlx-macros-core/src/derives/attributes.rs index 7d32e0037f..059d06882f 100644 --- a/sqlx-macros-core/src/derives/attributes.rs +++ b/sqlx-macros-core/src/derives/attributes.rs @@ -1,9 +1,8 @@ use proc_macro2::{Ident, Span, TokenStream}; use quote::quote; use syn::punctuated::Punctuated; -use syn::spanned::Spanned; use syn::token::Comma; -use syn::{Attribute, DeriveInput, Field, Lit, Meta, MetaNameValue, NestedMeta, Type, Variant}; +use syn::{Attribute, DeriveInput, Field, LitStr, Meta, Token, Type, Variant}; macro_rules! assert_attribute { ($e:expr, $err:expr, $input:expr) => { @@ -72,74 +71,49 @@ pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result { - for value in list.nested.iter() { - match value { - NestedMeta::Meta(meta) => match meta { - Meta::Path(p) if p.is_ident("transparent") => { - try_set!(transparent, true, value) - } - - Meta::NameValue(MetaNameValue { - path, - lit: Lit::Str(val), - .. - }) if path.is_ident("rename_all") => { - let val = match &*val.value() { - "lowercase" => RenameAll::LowerCase, - "snake_case" => RenameAll::SnakeCase, - "UPPERCASE" => RenameAll::UpperCase, - "SCREAMING_SNAKE_CASE" => RenameAll::ScreamingSnakeCase, - "kebab-case" => RenameAll::KebabCase, - "camelCase" => RenameAll::CamelCase, - "PascalCase" => RenameAll::PascalCase, - _ => fail!(meta, "unexpected value for rename_all"), - }; - - try_set!(rename_all, val, value) - } - - Meta::NameValue(MetaNameValue { - path, - lit: Lit::Str(val), - .. - }) if path.is_ident("type_name") => { - try_set!( - type_name, - TypeName { - val: val.value(), - span: value.span(), - }, - value - ) - } - - u => fail!(u, "unexpected attribute"), - }, - u => fail!(u, "unexpected attribute"), - } - } - } - Meta::List(list) if list.path.is_ident("repr") => { - if list.nested.len() != 1 { - fail!(&list.nested, "expected one value") - } - match list.nested.first().unwrap() { - NestedMeta::Meta(Meta::Path(p)) if p.get_ident().is_some() => { - try_set!(repr, p.get_ident().unwrap().clone(), list); - } - u => fail!(u, "unexpected value"), + for attr in input { + if attr.path().is_ident("sqlx") { + attr.parse_nested_meta(|meta| { + if meta.path.is_ident("transparent") { + try_set!(transparent, true, attr); + } else if meta.path.is_ident("rename_all") { + meta.input.parse::()?; + let lit: LitStr = meta.input.parse()?; + + let val = match lit.value().as_str() { + "lowercase" => RenameAll::LowerCase, + "snake_case" => RenameAll::SnakeCase, + "UPPERCASE" => RenameAll::UpperCase, + "SCREAMING_SNAKE_CASE" => RenameAll::ScreamingSnakeCase, + "kebab-case" => RenameAll::KebabCase, + "camelCase" => RenameAll::CamelCase, + "PascalCase" => RenameAll::PascalCase, + _ => fail!(lit, "unexpected value for rename_all"), + }; + + try_set!(rename_all, val, lit) + } else if meta.path.is_ident("type_name") { + meta.input.parse::()?; + let lit: LitStr = meta.input.parse()?; + let name = TypeName { + val: lit.value(), + span: lit.span(), + }; + + try_set!(type_name, name, lit) + } else { + fail!(meta.path, "unexpected attribute") } + + Ok(()) + })?; + } else if attr.path().is_ident("repr") { + let list: Punctuated = + attr.parse_args_with(>::parse_terminated)?; + + if let Some(path) = list.iter().find_map(|f| f.require_path_only().ok()) { + try_set!(repr, path.get_ident().unwrap().clone(), list); } - _ => {} } } @@ -158,34 +132,31 @@ pub fn parse_child_attributes(input: &[Attribute]) -> syn::Result match meta { - Meta::NameValue(MetaNameValue { - path, - lit: Lit::Str(val), - .. - }) if path.is_ident("rename") => try_set!(rename, val.value(), value), - Meta::NameValue(MetaNameValue { - path, - lit: Lit::Str(val), - .. - }) if path.is_ident("try_from") => try_set!(try_from, val.parse()?, value), - Meta::Path(path) if path.is_ident("default") => default = true, - Meta::Path(path) if path.is_ident("flatten") => flatten = true, - Meta::Path(path) if path.is_ident("skip") => skip = true, - u => fail!(u, "unexpected attribute"), - }, - u => fail!(u, "unexpected attribute"), - } + for attr in input.iter().filter(|a| a.path().is_ident("sqlx")) { + attr.parse_nested_meta(|meta| { + if meta.path.is_ident("rename") { + meta.input.parse::()?; + let val: LitStr = meta.input.parse()?; + try_set!(rename, val.value(), val); + return Ok(()); + } else if meta.path.is_ident("try_from") { + meta.input.parse::()?; + let val: LitStr = meta.input.parse()?; + let val = val.parse()?; + try_set!(try_from, val, val); + return Ok(()); } - } + + if meta.path.is_ident("default") { + default = true; + } else if meta.path.is_ident("flatten") { + flatten = true; + } else if meta.path.is_ident("skip") { + skip = true; + } + + Ok(()) + })?; } Ok(SqlxChildAttributes { diff --git a/sqlx-macros-core/src/derives/encode.rs b/sqlx-macros-core/src/derives/encode.rs index 7bb568210f..823af65ad3 100644 --- a/sqlx-macros-core/src/derives/encode.rs +++ b/sqlx-macros-core/src/derives/encode.rs @@ -9,7 +9,7 @@ use syn::punctuated::Punctuated; use syn::token::Comma; use syn::{ parse_quote, Data, DataEnum, DataStruct, DeriveInput, Expr, Field, Fields, FieldsNamed, - FieldsUnnamed, Lifetime, LifetimeDef, Stmt, Variant, + FieldsUnnamed, Lifetime, LifetimeParam, Stmt, Variant, }; pub fn expand_derive_encode(input: &DeriveInput) -> syn::Result { @@ -66,7 +66,7 @@ fn expand_derive_encode_transparent( let mut generics = generics.clone(); generics .params - .insert(0, LifetimeDef::new(lifetime.clone()).into()); + .insert(0, LifetimeParam::new(lifetime.clone()).into()); generics .params diff --git a/sqlx-macros-core/src/query/args.rs b/sqlx-macros-core/src/query/args.rs index 71094a67d2..cf418ae8cf 100644 --- a/sqlx-macros-core/src/query/args.rs +++ b/sqlx-macros-core/src/query/args.rs @@ -5,7 +5,7 @@ use proc_macro2::TokenStream; use quote::{format_ident, quote, quote_spanned}; use sqlx_core::describe::Describe; use syn::spanned::Spanned; -use syn::{Expr, ExprCast, ExprGroup, ExprType, Type}; +use syn::{Expr, ExprCast, ExprGroup, Type}; /// Returns a tokenstream which typechecks the arguments passed to the macro /// and binds them to `DB::Arguments` with the ident `query_args`. @@ -118,7 +118,6 @@ fn get_type_override(expr: &Expr) -> Option<&Type> { match expr { Expr::Group(group) => get_type_override(&group.expr), Expr::Cast(cast) => Some(&cast.ty), - Expr::Type(ascription) => Some(&ascription.ty), _ => None, } } @@ -134,8 +133,6 @@ fn strip_wildcard(expr: Expr) -> Expr { group_token, expr: Box::new(strip_wildcard(*expr)), }), - // type ascription syntax is experimental so we always strip it - Expr::Type(ExprType { expr, .. }) => *expr, // we want to retain casts if they semantically matter Expr::Cast(ExprCast { attrs, diff --git a/sqlx-macros-core/src/test_attr.rs b/sqlx-macros-core/src/test_attr.rs index 9f25d5661f..558d7af080 100644 --- a/sqlx-macros-core/src/test_attr.rs +++ b/sqlx-macros-core/src/test_attr.rs @@ -1,12 +1,20 @@ -use proc_macro2::{Span, TokenStream}; +use proc_macro2::TokenStream; use quote::quote; -use syn::LitStr; +use syn::{parse::Parser, Meta}; +#[cfg(feature = "migrate")] +use proc_macro2::Span; +#[cfg(feature = "migrate")] +use syn::{punctuated::Punctuated, Expr, ExprLit, Lit, LitStr, MetaNameValue, Token}; + +#[cfg(feature = "migrate")] struct Args { fixtures: Vec, + #[cfg(feature = "migrate")] migrations: MigrationsOpt, } +#[cfg(feature = "migrate")] enum MigrationsOpt { InferredPath, ExplicitPath(LitStr), @@ -14,7 +22,12 @@ enum MigrationsOpt { Disabled, } -pub fn expand(args: syn::AttributeArgs, input: syn::ItemFn) -> crate::Result { +type AttributeArgs = syn::punctuated::Punctuated; + +pub fn expand(args: TokenStream, input: syn::ItemFn) -> crate::Result { + let parser = AttributeArgs::parse_terminated; + let args = parser.parse2(args)?; + if input.sig.inputs.is_empty() { if !args.is_empty() { if cfg!(feature = "migrate") { @@ -61,7 +74,7 @@ fn expand_simple(input: syn::ItemFn) -> TokenStream { } #[cfg(feature = "migrate")] -fn expand_advanced(args: syn::AttributeArgs, input: syn::ItemFn) -> crate::Result { +fn expand_advanced(args: AttributeArgs, input: syn::ItemFn) -> crate::Result { let ret = &input.sig.output; let name = &input.sig.ident; let inputs = &input.sig.inputs; @@ -127,87 +140,72 @@ fn expand_advanced(args: syn::AttributeArgs, input: syn::ItemFn) -> crate::Resul } #[cfg(feature = "migrate")] -fn parse_args(attr_args: syn::AttributeArgs) -> syn::Result { +fn parse_args(args: AttributeArgs) -> Result { let mut fixtures = vec![]; let mut migrations = MigrationsOpt::InferredPath; - for arg in attr_args { + for arg in args { + let path = arg.path().clone(); + match arg { - syn::NestedMeta::Meta(syn::Meta::List(list)) if list.path.is_ident("fixtures") => { + Meta::List(list) if path.is_ident("fixtures") => { if !fixtures.is_empty() { - return Err(syn::Error::new_spanned(list, "duplicate `fixtures` arg")); + return Err(syn::Error::new_spanned(path, "duplicate `fixtures` arg")); } - for nested in list.nested { - match nested { - syn::NestedMeta::Lit(syn::Lit::Str(litstr)) => fixtures.push(litstr), - other => { - return Err(syn::Error::new_spanned(other, "expected string literal")) - } - } - } + let parser = >::parse_terminated; + let list = parser.parse2(list.tokens)?; + fixtures.extend(list); } - syn::NestedMeta::Meta(syn::Meta::NameValue(namevalue)) - if namevalue.path.is_ident("migrations") => - { + Meta::NameValue(MetaNameValue { value, .. }) if path.is_ident("migrations") => { if !matches!(migrations, MigrationsOpt::InferredPath) { return Err(syn::Error::new_spanned( - namevalue, + path, "cannot have more than one `migrations` or `migrator` arg", )); } - migrations = match namevalue.lit { - syn::Lit::Bool(litbool) => { - if !litbool.value { - // migrations = false - MigrationsOpt::Disabled - } else { - // migrations = true - return Err(syn::Error::new_spanned( - litbool, - "`migrations = true` is redundant", - )); - } - } - // migrations = "" - syn::Lit::Str(litstr) => MigrationsOpt::ExplicitPath(litstr), - _ => { - return Err(syn::Error::new_spanned( - namevalue, - "expected string or `false`", - )) - } + let Expr::Lit(ExprLit { lit, .. }) = value else { + return Err(syn::Error::new_spanned(path, "expected string for `false`")) }; - } - syn::NestedMeta::Meta(syn::Meta::NameValue(namevalue)) - if namevalue.path.is_ident("migrator") => - { - if !matches!(migrations, MigrationsOpt::InferredPath) { + + migrations = match lit { + // migrations = false + Lit::Bool(b) if !b.value => MigrationsOpt::Disabled, + // migrations = true + Lit::Bool(b) => { return Err(syn::Error::new_spanned( - namevalue, - "cannot have more than one `migrations` or `migrator` arg", + b, + "`migrations = true` is redundant", )); } - - migrations = match namevalue.lit { - // migrator = "" - syn::Lit::Str(litstr) => MigrationsOpt::ExplicitMigrator(litstr.parse()?), - _ => { - return Err(syn::Error::new_spanned( - namevalue, - "expected string", - )) - } - }; + // migrations = "path" + Lit::Str(s) => MigrationsOpt::ExplicitPath(s), + lit => return Err(syn::Error::new_spanned(lit, "expected string or `false`")), + }; + } + // migrator = "path" + Meta::NameValue(MetaNameValue { value, .. }) if path.is_ident("migrator") => { + if !matches!(migrations, MigrationsOpt::InferredPath) { + return Err(syn::Error::new_spanned( + path, + "cannot have more than one `migrations` or `migrator` arg", + )); } - other => { + + let Expr::Lit(ExprLit { lit: Lit::Str(lit), .. }) = value else { + return Err(syn::Error::new_spanned(path, "expected string")) + }; + + migrations = MigrationsOpt::ExplicitMigrator(lit.parse()?); + } + arg => { return Err(syn::Error::new_spanned( - other, - "expected `fixtures(\"\", ...)` or `migrations = \"\" | false` or `migrator = \"\"`", - )) + arg, + r#"expected `fixtures("", ...)` or `migrations = "" | false` or `migrator = ""`"#, + )); } - } + }; } Ok(Args { diff --git a/sqlx-macros/Cargo.toml b/sqlx-macros/Cargo.toml index f545a186d7..eb1200b0a0 100644 --- a/sqlx-macros/Cargo.toml +++ b/sqlx-macros/Cargo.toml @@ -43,5 +43,5 @@ sqlx-core = { workspace = true, default-features = false, features = ["any"] } sqlx-macros-core = { workspace = true } proc-macro2 = { version = "1.0.36", default-features = false } -syn = { version = "1.0.84", default-features = false, features = ["full"] } -quote = { version = "1.0.14", default-features = false } +syn = { version = "2.0", default-features = false, features = ["parsing", "proc-macro"] } +quote = { version = "1.0.26", default-features = false } diff --git a/sqlx-macros/src/lib.rs b/sqlx-macros/src/lib.rs index 229c1030b1..8d1a66abdc 100644 --- a/sqlx-macros/src/lib.rs +++ b/sqlx-macros/src/lib.rs @@ -51,7 +51,6 @@ pub fn derive_type(tokenstream: TokenStream) -> TokenStream { #[proc_macro_derive(FromRow, attributes(sqlx))] pub fn derive_from_row(input: TokenStream) -> TokenStream { let input = syn::parse_macro_input!(input as syn::DeriveInput); - match derives::expand_derive_from_row(&input) { Ok(ts) => ts.into(), Err(e) => e.to_compile_error().into(), @@ -79,10 +78,9 @@ pub fn migrate(input: TokenStream) -> TokenStream { #[proc_macro_attribute] pub fn test(args: TokenStream, input: TokenStream) -> TokenStream { - let args = syn::parse_macro_input!(args as syn::AttributeArgs); let input = syn::parse_macro_input!(input as syn::ItemFn); - match test_attr::expand(args, input) { + match test_attr::expand(args.into(), input) { Ok(ts) => ts.into(), Err(e) => { if let Some(parse_err) = e.downcast_ref::() {