From e47aa67bf7574423c5e9309f6fead35892418c61 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 16 May 2024 09:01:53 +0200 Subject: [PATCH] Update syn requirement from 1.0.95 to 2.0.63 (#67) * Update syn requirement from 1.0.95 to 2.0.63 Updates the requirements on [syn](https://github.com/dtolnay/syn) to permit the latest version. - [Release notes](https://github.com/dtolnay/syn/releases) - [Commits](https://github.com/dtolnay/syn/compare/1.0.95...2.0.63) --- updated-dependencies: - dependency-name: syn dependency-type: direct:production ... Signed-off-by: dependabot[bot] * WIP changing the api to fit the new version * Fix matcher logic * Fix tests * Better errors --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Simone Cottini --- Cargo.toml | 2 +- positional_derive/Cargo.toml | 2 +- positional_derive/src/analyze/field.rs | 119 ++++-------------- positional_derive/src/analyze/variant.rs | 35 ++---- positional_derive/src/parse.rs | 8 +- .../tests/ui/enums-with-wrong-matcher.stderr | 4 +- .../tests/ui/list-attributes.stderr | 4 +- .../tests/ui/path-attributes.stderr | 4 +- .../tests/ui/wrong-filler-type.stderr | 8 +- 9 files changed, 53 insertions(+), 133 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b1b7dfa..3c5c352 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] - +resolver = "2" members = [ "positional", "positional_derive", diff --git a/positional_derive/Cargo.toml b/positional_derive/Cargo.toml index 8791f1e..5bd87b9 100644 --- a/positional_derive/Cargo.toml +++ b/positional_derive/Cargo.toml @@ -13,5 +13,5 @@ proc-macro = true proc-macro-error = "1.0.4" proc-macro2 = "1.0.39" quote = "1.0" -syn = {version = "1.0.95", features = ["extra-traits", "full"]} +syn = {version = "2.0.63", features = ["extra-traits", "full"]} trybuild = "1.0.61" diff --git a/positional_derive/src/analyze/field.rs b/positional_derive/src/analyze/field.rs index f8214ef..d25c44d 100644 --- a/positional_derive/src/analyze/field.rs +++ b/positional_derive/src/analyze/field.rs @@ -1,6 +1,5 @@ use proc_macro_error::abort; -use std::collections::HashMap; -use syn::{Lit, LitChar, LitInt, LitStr, Meta, NestedMeta}; +use syn::{LitChar, LitInt, LitStr}; const FIELD_ATTRIBUTE: &str = "field"; @@ -26,106 +25,40 @@ fn parse_field_attributes(field: &syn::Field) -> Option<(LitInt, Option field .attrs .iter() - .find(|attribute| attribute.path.is_ident(FIELD_ATTRIBUTE)) + .find(|attribute| attribute.path().is_ident(FIELD_ATTRIBUTE)) .map(parse_field_attribute_meta) } fn parse_field_attribute_meta( attribute: &syn::Attribute, ) -> (LitInt, Option, Option) { - match attribute.parse_meta() { - Ok(meta) => { - let mut attrs = HashMap::new(); - parse_meta(&meta, &mut attrs); + let mut size: Option = None; + let mut align: Option = None; + let mut filler: Option = None; - let size = match attrs.get("size") { - None => { - abort!( - attribute, - "wrong field configuration"; - help = "you need to provide at least a size configuration to the field" - ) - } - Some(size_lit) => match size_lit { - Lit::Int(lit_int) => lit_int, - _ => { - abort!( - attribute, - "wrong field configuration"; - help = "the size configuration should be a number" - ) - } - }, - }; - - let filler = match attrs.get("filler") { - None => None, - Some(filler_lit) => match filler_lit { - Lit::Char(lit_char) => Some(lit_char), - _ => { - abort!( - attribute, - "wrong field configuration"; - help = "the filler configuration should be a char" - ) - } - }, - }; - - let align = match attrs.get("align") { - None => None, - Some(filler_align) => match filler_align { - Lit::Str(lit_str) => Some(lit_str), - _ => { - abort!( - attribute, - "wrong field configuration"; - help = "the align configuration should be a string" - ) - } - }, - }; - - (size.clone(), filler.cloned(), align.cloned()) - } - Err(_) => { - abort!( - attribute, - "wrong field configuration"; - help = "unable to parse field configuration" - ) + let parse_result = attribute.parse_nested_meta(|meta| { + if meta.path.is_ident("size") { + size = Some(meta.value()?.parse()?); + } else if meta.path.is_ident("align") { + align = Some(meta.value()?.parse()?); + } else if meta.path.is_ident("filler") { + filler = Some(meta.value()?.parse()?); + } else { + return Err(meta.error("unsupported attribute")); } + Ok(()) + }); + + if let Err(err) = parse_result { + abort!(err.span(), "failed to parse field attribute"; note = err.to_string()); } -} -fn parse_meta(meta: &syn::Meta, attrs: &mut HashMap) { - match meta { - Meta::Path(path) => { - abort!( - path, - "wrong field configuration"; - help = "there should only be name = value couple inside the field configuration" - ) - } - Meta::List(meta_list) => { - for nested_meta in &meta_list.nested { - match nested_meta { - NestedMeta::Meta(name_value) => parse_meta(name_value, attrs), - NestedMeta::Lit(lit) => { - abort!( - lit, - "wrong field configuration"; - help = "there should only be name = value couple inside the field configuration" - ) - } - } - } - } - Meta::NameValue(name_value) => { - attrs.insert( - name_value.path.get_ident().unwrap().to_string(), - name_value.lit.clone(), - ); - } + match size { + Some(size) => (size, filler, align), + None => abort!( + attribute, + "wrong field configuration"; + help = "you need to provide at least a size configuration to the field" + ), } } diff --git a/positional_derive/src/analyze/variant.rs b/positional_derive/src/analyze/variant.rs index 1fad743..5c67079 100644 --- a/positional_derive/src/analyze/variant.rs +++ b/positional_derive/src/analyze/variant.rs @@ -1,7 +1,5 @@ use proc_macro_error::abort; -use syn::parse::{Parse, ParseStream}; -use syn::spanned::Spanned; -use syn::{parenthesized, Expr, Fields}; +use syn::{Expr, Fields}; const MATCHER_ATTRIBUTE: &str = "matcher"; @@ -16,17 +14,6 @@ pub struct Matcher { pub expr: Expr, } -impl Parse for Matcher { - fn parse(input: ParseStream) -> syn::Result { - let content; - let _parenthesis = parenthesized!(content in input); - - Ok(Self { - expr: content.parse()?, - }) - } -} - impl Variant { pub fn new(variant: syn::Variant) -> Option { match variant.clone().fields { @@ -70,19 +57,19 @@ fn parse_variant_attributes(variant: &syn::Variant) -> Option { variant .attrs .iter() - .find(|attribute| attribute.path.is_ident(MATCHER_ATTRIBUTE)) + .find(|attribute| attribute.path().is_ident(MATCHER_ATTRIBUTE)) .map(parse_matcher_expression) } fn parse_matcher_expression(attribute: &syn::Attribute) -> Matcher { - let span = attribute.tokens.span(); - if let Ok(matcher) = syn::parse2::(attribute.tokens.clone()) { - matcher - } else { - abort!( - span, - "expected an expression as matcher"; - help = "example syntax: `#[matcher(row[0..2] == \"00\")]`" - ) + match attribute.parse_args::() { + Ok(expr) => Matcher { expr }, + Err(err) => { + abort!( + err.span(), + "expected an expression as matcher"; + help = "example syntax: `#[matcher(row[0..2] == \"00\")]`" + ) + } } } diff --git a/positional_derive/src/parse.rs b/positional_derive/src/parse.rs index f341bab..785f81c 100644 --- a/positional_derive/src/parse.rs +++ b/positional_derive/src/parse.rs @@ -5,22 +5,22 @@ use syn::{Data, DeriveInput}; pub type Ast = DeriveInput; pub fn parse(tokens: TokenStream) -> Ast { - match syn::parse2::(tokens) { - // the derive is applied to a struct + match syn::parse2::(tokens) { + // the derivation is applied to a struct Ok( item @ DeriveInput { data: Data::Struct(_), .. }, ) => item, - // the derive is applied to an enum + // the derivation is applied to an enum Ok( item @ DeriveInput { data: Data::Enum(_), .. }, ) => item, - // the derive is applied to a union + // the derivation is applied to a union Ok( item @ DeriveInput { data: Data::Union(_), diff --git a/positional_derive/tests/ui/enums-with-wrong-matcher.stderr b/positional_derive/tests/ui/enums-with-wrong-matcher.stderr index 61bf963..c5a74b6 100644 --- a/positional_derive/tests/ui/enums-with-wrong-matcher.stderr +++ b/positional_derive/tests/ui/enums-with-wrong-matcher.stderr @@ -2,7 +2,7 @@ error: expected an expression as matcher = help: example syntax: `#[matcher(row[0..2] == "00")]` - --> tests/ui/enums-with-wrong-matcher.rs:5:14 + --> tests/ui/enums-with-wrong-matcher.rs:5:15 | 5 | #[matcher(struct)] - | ^^^^^^^^ + | ^^^^^^ diff --git a/positional_derive/tests/ui/list-attributes.stderr b/positional_derive/tests/ui/list-attributes.stderr index fd4c410..dc85938 100644 --- a/positional_derive/tests/ui/list-attributes.stderr +++ b/positional_derive/tests/ui/list-attributes.stderr @@ -1,6 +1,6 @@ -error: wrong field configuration +error: failed to parse field attribute - = help: there should only be name = value couple inside the field configuration + = note: unsupported attribute --> tests/ui/list-attributes.rs:5:13 | diff --git a/positional_derive/tests/ui/path-attributes.stderr b/positional_derive/tests/ui/path-attributes.stderr index da0a86f..e06efd9 100644 --- a/positional_derive/tests/ui/path-attributes.stderr +++ b/positional_derive/tests/ui/path-attributes.stderr @@ -1,6 +1,6 @@ -error: wrong field configuration +error: failed to parse field attribute - = help: there should only be name = value couple inside the field configuration + = note: expected attribute arguments in parentheses: #[field(...)] --> tests/ui/path-attributes.rs:5:7 | diff --git a/positional_derive/tests/ui/wrong-filler-type.stderr b/positional_derive/tests/ui/wrong-filler-type.stderr index 5b0a2fa..90784ba 100644 --- a/positional_derive/tests/ui/wrong-filler-type.stderr +++ b/positional_derive/tests/ui/wrong-filler-type.stderr @@ -1,8 +1,8 @@ -error: wrong field configuration +error: failed to parse field attribute - = help: the filler configuration should be a char + = note: expected character literal - --> tests/ui/wrong-filler-type.rs:5:5 + --> tests/ui/wrong-filler-type.rs:5:33 | 5 | #[field(size = 10, filler = "a")] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^