From 92d33b718142b1f952aef5ab9e3b09adb0a26548 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 23 Mar 2023 23:38:59 +0100 Subject: [PATCH] macros: update syn (#5572) --- tokio-macros/Cargo.toml | 2 +- tokio-macros/src/entry.rs | 36 ++++++++++++++++-------------------- tokio-macros/src/select.rs | 7 +++---- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/tokio-macros/Cargo.toml b/tokio-macros/Cargo.toml index 79772bdb470..bec401c08a0 100644 --- a/tokio-macros/Cargo.toml +++ b/tokio-macros/Cargo.toml @@ -24,7 +24,7 @@ proc-macro = true [dependencies] proc-macro2 = "1.0.7" quote = "1" -syn = { version = "1.0.56", features = ["full"] } +syn = { version = "2.0", features = ["full"] } [dev-dependencies] tokio = { version = "1.0.0", path = "../tokio", features = ["full"] } diff --git a/tokio-macros/src/entry.rs b/tokio-macros/src/entry.rs index 0c14bfb77ba..3b124cff98e 100644 --- a/tokio-macros/src/entry.rs +++ b/tokio-macros/src/entry.rs @@ -4,7 +4,7 @@ use quote::{quote, quote_spanned, ToTokens}; use syn::{parse::Parser, Ident, Path}; // syn::AttributeArgs does not implement syn::Parse -type AttributeArgs = syn::punctuated::Punctuated; +type AttributeArgs = syn::punctuated::Punctuated; #[derive(Clone, Copy, PartialEq)] enum RuntimeFlavor { @@ -245,7 +245,7 @@ fn build_config( for arg in args { match arg { - syn::NestedMeta::Meta(syn::Meta::NameValue(namevalue)) => { + syn::Meta::NameValue(namevalue) => { let ident = namevalue .path .get_ident() @@ -254,34 +254,26 @@ fn build_config( })? .to_string() .to_lowercase(); + let lit = match &namevalue.value { + syn::Expr::Lit(syn::ExprLit { lit, .. }) => lit, + expr => return Err(syn::Error::new_spanned(expr, "Must be a literal")), + }; match ident.as_str() { "worker_threads" => { - config.set_worker_threads( - namevalue.lit.clone(), - syn::spanned::Spanned::span(&namevalue.lit), - )?; + config.set_worker_threads(lit.clone(), syn::spanned::Spanned::span(lit))?; } "flavor" => { - config.set_flavor( - namevalue.lit.clone(), - syn::spanned::Spanned::span(&namevalue.lit), - )?; + config.set_flavor(lit.clone(), syn::spanned::Spanned::span(lit))?; } "start_paused" => { - config.set_start_paused( - namevalue.lit.clone(), - syn::spanned::Spanned::span(&namevalue.lit), - )?; + config.set_start_paused(lit.clone(), syn::spanned::Spanned::span(lit))?; } "core_threads" => { let msg = "Attribute `core_threads` is renamed to `worker_threads`"; return Err(syn::Error::new_spanned(namevalue, msg)); } "crate" => { - config.set_crate_name( - namevalue.lit.clone(), - syn::spanned::Spanned::span(&namevalue.lit), - )?; + config.set_crate_name(lit.clone(), syn::spanned::Spanned::span(lit))?; } name => { let msg = format!( @@ -292,7 +284,7 @@ fn build_config( } } } - syn::NestedMeta::Meta(syn::Meta::Path(path)) => { + syn::Meta::Path(path) => { let name = path .get_ident() .ok_or_else(|| syn::Error::new_spanned(&path, "Must have specified ident"))? @@ -478,7 +470,11 @@ pub(crate) fn test(args: TokenStream, item: TokenStream, rt_multi_thread: bool) Ok(it) => it, Err(e) => return token_stream_with_error(item, e), }; - let config = if let Some(attr) = input.attrs.iter().find(|attr| attr.path.is_ident("test")) { + let config = if let Some(attr) = input + .attrs + .iter() + .find(|attr| attr.meta.path().is_ident("test")) + { let msg = "second test attribute is supplied"; Err(syn::Error::new_spanned(attr, msg)) } else { diff --git a/tokio-macros/src/select.rs b/tokio-macros/src/select.rs index 8c5ae306e67..dd491f848c3 100644 --- a/tokio-macros/src/select.rs +++ b/tokio-macros/src/select.rs @@ -1,7 +1,7 @@ use proc_macro::{TokenStream, TokenTree}; use proc_macro2::Span; use quote::quote; -use syn::Ident; +use syn::{parse::Parser, Ident}; pub(crate) fn declare_output_enum(input: TokenStream) -> TokenStream { // passed in is: `(_ _ _)` with one `_` per branch @@ -46,7 +46,7 @@ pub(crate) fn clean_pattern_macro(input: TokenStream) -> TokenStream { // If this isn't a pattern, we return the token stream as-is. The select! // macro is using it in a location requiring a pattern, so an error will be // emitted there. - let mut input: syn::Pat = match syn::parse(input.clone()) { + let mut input: syn::Pat = match syn::Pat::parse_single.parse(input.clone()) { Ok(it) => it, Err(_) => return input, }; @@ -58,7 +58,6 @@ pub(crate) fn clean_pattern_macro(input: TokenStream) -> TokenStream { // Removes any occurrences of ref or mut in the provided pattern. fn clean_pattern(pat: &mut syn::Pat) { match pat { - syn::Pat::Box(_box) => {} syn::Pat::Lit(_literal) => {} syn::Pat::Macro(_macro) => {} syn::Pat::Path(_path) => {} @@ -94,7 +93,7 @@ fn clean_pattern(pat: &mut syn::Pat) { } } syn::Pat::TupleStruct(tuple) => { - for elem in tuple.pat.elems.iter_mut() { + for elem in tuple.elems.iter_mut() { clean_pattern(elem); } }