From 3c19bb1fd731698d0cfabb2d84befce953ddad93 Mon Sep 17 00:00:00 2001 From: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com> Date: Mon, 10 Apr 2023 10:10:17 +0100 Subject: [PATCH] Update syn to 2.0 (#289) --- Cargo.toml | 3 --- logos-cli/Cargo.toml | 2 +- logos-codegen/Cargo.toml | 2 +- logos-codegen/src/lib.rs | 33 +++++++++++++++---------- logos-codegen/src/parser/mod.rs | 12 +++++---- logos-codegen/src/parser/type_params.rs | 8 +++--- logos-derive/Cargo.toml | 2 +- logos/Cargo.toml | 2 +- 8 files changed, 35 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4e04f479..4817d195 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,3 @@ members = ["logos", "logos-cli", "logos-codegen", "logos-derive", "tests"] [profile] release = { lto = true } bench = { lto = true } - -[patch.crates-io] -logos-derive = { path = "./logos-derive" } diff --git a/logos-cli/Cargo.toml b/logos-cli/Cargo.toml index fb59bbd9..a412a71b 100644 --- a/logos-cli/Cargo.toml +++ b/logos-cli/Cargo.toml @@ -14,7 +14,7 @@ edition = "2018" anyhow = "1.0.57" clap = { version = "3.1.18", features = ["derive"] } fs-err = "2.7.0" -logos-codegen = { path = "../logos-codegen", version = "0.12.0" } +logos-codegen = { version = "0.12.0", path = "../logos-codegen" } proc-macro2 = "1.0.39" [dev-dependencies] diff --git a/logos-codegen/Cargo.toml b/logos-codegen/Cargo.toml index 741acfb2..869bf0fa 100644 --- a/logos-codegen/Cargo.toml +++ b/logos-codegen/Cargo.toml @@ -14,7 +14,7 @@ edition = "2018" [dependencies] beef = "0.5.0" fnv = "1.0.6" -syn = { version = "1.0.17", features = ["full"] } +syn = { version = "2.0.13", features = ["full"] } quote = "1.0.3" proc-macro2 = "1.0.9" regex-syntax = "0.6" diff --git a/logos-codegen/src/lib.rs b/logos-codegen/src/lib.rs index 8c30819b..dd60830d 100644 --- a/logos-codegen/src/lib.rs +++ b/logos-codegen/src/lib.rs @@ -23,7 +23,7 @@ use parser::{IgnoreFlags, Mode, Parser}; use quote::ToTokens; use util::MaybeVoid; -use proc_macro2::TokenStream; +use proc_macro2::{TokenStream, TokenTree}; use quote::quote; use syn::parse_quote; use syn::spanned::Spanned; @@ -107,7 +107,7 @@ pub fn generate(input: TokenStream) -> TokenStream { let leaf = move |span| Leaf::new(var_ident, span).field(field.clone()); for attr in &mut variant.attrs { - let attr_name = match attr.path.get_ident() { + let attr_name = match attr.path().get_ident() { Some(ident) => ident.to_string(), None => continue, }; @@ -314,14 +314,21 @@ pub fn strip_attributes(input: TokenStream) -> TokenStream { strip_attrs_from_vec(&mut item.attrs); for attr in &mut item.attrs { - if attr.path.is_ident("derive") { - if let Ok(syn::Meta::List(mut meta)) = attr.parse_meta() { - meta.nested = meta.nested.into_iter().filter(|nested| !matches!(nested, syn::NestedMeta::Meta(nested) if nested.path().is_ident("Logos"))).collect(); - - attr.tokens = TokenStream::new(); - meta.paren_token.surround(&mut attr.tokens, |tokens| { - meta.nested.to_tokens(tokens); - }); + if let syn::Meta::List(meta) = &mut attr.meta { + if meta.path.is_ident("derive") { + let mut tokens = + std::mem::replace(&mut meta.tokens, TokenStream::new()).into_iter(); + + while let Some(TokenTree::Ident(ident)) = tokens.next() { + let punct = tokens.next(); + + if ident == "Logos" { + continue; + } + + meta.tokens.extend([TokenTree::Ident(ident)]); + meta.tokens.extend(punct); + } } } } @@ -341,7 +348,7 @@ fn strip_attrs_from_vec(attrs: &mut Vec) { } fn is_logos_attr(attr: &syn::Attribute) -> bool { - attr.path.is_ident(LOGOS_ATTR) - || attr.path.is_ident(TOKEN_ATTR) - || attr.path.is_ident(REGEX_ATTR) + attr.path().is_ident(LOGOS_ATTR) + || attr.path().is_ident(TOKEN_ATTR) + || attr.path().is_ident(REGEX_ATTR) } diff --git a/logos-codegen/src/parser/mod.rs b/logos-codegen/src/parser/mod.rs index 5424a1ed..8d0665fc 100644 --- a/logos-codegen/src/parser/mod.rs +++ b/logos-codegen/src/parser/mod.rs @@ -2,7 +2,7 @@ use beef::lean::Cow; use proc_macro2::{Span, TokenStream, TokenTree}; use quote::quote; use syn::spanned::Spanned; -use syn::{Attribute, GenericParam, Lit, Type}; +use syn::{Attribute, GenericParam, Lit, Meta, Type}; use crate::error::Errors; use crate::leaf::{Callback, InlineCallback}; @@ -65,10 +65,12 @@ impl Parser { } fn parse_attr(&mut self, attr: &mut Attribute) -> Option { - let mut tokens = std::mem::replace(&mut attr.tokens, TokenStream::new()).into_iter(); + match &mut attr.meta { + Meta::List(list) => { + let tokens = std::mem::replace(&mut list.tokens, TokenStream::new()); - match tokens.next() { - Some(TokenTree::Group(group)) => Some(AttributeParser::new(group.stream())), + Some(AttributeParser::new(tokens)) + } _ => None, } } @@ -76,7 +78,7 @@ impl Parser { /// Try to parse the main `#[logos(...)]`, does nothing if /// the attribute's name isn't `logos`. pub fn try_parse_logos(&mut self, attr: &mut Attribute) { - if !attr.path.is_ident(LOGOS_ATTR) { + if !attr.path().is_ident(LOGOS_ATTR) { return; } diff --git a/logos-codegen/src/parser/type_params.rs b/logos-codegen/src/parser/type_params.rs index 268de40a..1be4948e 100644 --- a/logos-codegen/src/parser/type_params.rs +++ b/logos-codegen/src/parser/type_params.rs @@ -1,7 +1,7 @@ use proc_macro2::{Ident, Span, TokenStream}; use quote::quote; use syn::spanned::Spanned; -use syn::{Lifetime, LifetimeDef, Path, Type}; +use syn::{Lifetime, LifetimeParam, Path, Type}; use crate::error::Errors; @@ -12,7 +12,7 @@ pub struct TypeParams { } impl TypeParams { - pub fn explicit_lifetime(&mut self, lt: LifetimeDef, errors: &mut Errors) { + pub fn explicit_lifetime(&mut self, lt: LifetimeParam, errors: &mut Errors) { if self.lifetime { let span = lt.span(); @@ -180,8 +180,8 @@ fn traverse_path(path: &mut Path, f: &mut impl FnMut(&mut Type)) { syn::GenericArgument::Type(ty) => { traverse_type(ty, f); } - syn::GenericArgument::Binding(bind) => { - traverse_type(&mut bind.ty, f); + syn::GenericArgument::AssocType(assoc) => { + traverse_type(&mut assoc.ty, f); } _ => (), } diff --git a/logos-derive/Cargo.toml b/logos-derive/Cargo.toml index c1ac8702..cabbd902 100644 --- a/logos-derive/Cargo.toml +++ b/logos-derive/Cargo.toml @@ -16,4 +16,4 @@ name = "logos_derive" proc-macro = true [dependencies] -logos-codegen = { path = "../logos-codegen", version = "0.12.0" } +logos-codegen = { version = "0.12.0", path = "../logos-codegen" } diff --git a/logos/Cargo.toml b/logos/Cargo.toml index 2925f117..190e74ca 100644 --- a/logos/Cargo.toml +++ b/logos/Cargo.toml @@ -12,7 +12,7 @@ readme = "../README.md" edition = "2018" [dependencies] -logos-derive = { path = "../logos-derive", version = "0.12.1", optional = true } +logos-derive = { version = "0.12.1", path = "../logos-derive", optional = true } [features] default = ["export_derive", "std"]