From a1ce9288bef451f6bed599f05355bc46510d6484 Mon Sep 17 00:00:00 2001 From: Matthew Maurer Date: Mon, 3 Apr 2023 23:42:39 +0000 Subject: [PATCH] Update to syn-2 --- derive/Cargo.toml | 2 +- derive/src/container_attributes.rs | 24 ++++++++++++-------- derive/src/field_attributes.rs | 36 +++++++++++------------------- derive/src/lib.rs | 12 +++++----- 4 files changed, 35 insertions(+), 39 deletions(-) diff --git a/derive/Cargo.toml b/derive/Cargo.toml index 570519f..7ae30c8 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -21,7 +21,7 @@ rust-version = "1.63.0" [dependencies] proc-macro2 = "1.0" quote = "1.0" -syn = { version = "1.0.56", features = ['derive', 'parsing'] } +syn = { version = "2", features = ['derive', 'parsing'] } [lib] proc_macro = true diff --git a/derive/src/container_attributes.rs b/derive/src/container_attributes.rs index 9a91ac8..269131c 100644 --- a/derive/src/container_attributes.rs +++ b/derive/src/container_attributes.rs @@ -1,7 +1,7 @@ use crate::ARBITRARY_ATTRIBUTE_NAME; use syn::{ - parse::Error, punctuated::Punctuated, DeriveInput, Lit, Meta, MetaNameValue, NestedMeta, Token, - TypeParam, + parse::Error, punctuated::Punctuated, DeriveInput, Expr, ExprLit, Lit, Meta, MetaNameValue, + Token, TypeParam, }; pub struct ContainerAttributes { @@ -26,12 +26,12 @@ impl ContainerAttributes { let mut bounds = None; for attr in &derive_input.attrs { - if !attr.path.is_ident(ARBITRARY_ATTRIBUTE_NAME) { + if !attr.path().is_ident(ARBITRARY_ATTRIBUTE_NAME) { continue; } - let meta_list = match attr.parse_meta()? { - Meta::List(l) => l, + let meta_list = match attr.meta { + Meta::List(ref l) => l, _ => { return Err(Error::new_spanned( attr, @@ -43,13 +43,19 @@ impl ContainerAttributes { } }; - for nested_meta in meta_list.nested.iter() { + for nested_meta in + meta_list.parse_args_with(Punctuated::::parse_terminated)? + { match nested_meta { - NestedMeta::Meta(Meta::NameValue(MetaNameValue { + Meta::NameValue(MetaNameValue { path, - lit: Lit::Str(bound_str_lit), + value: + Expr::Lit(ExprLit { + lit: Lit::Str(bound_str_lit), + .. + }), .. - })) if path.is_ident("bound") => { + }) if path.is_ident("bound") => { bounds .get_or_insert_with(Vec::new) .push(bound_str_lit.parse_with(Punctuated::parse_terminated)?); diff --git a/derive/src/field_attributes.rs b/derive/src/field_attributes.rs index 2ca0f1c..9e5922e 100644 --- a/derive/src/field_attributes.rs +++ b/derive/src/field_attributes.rs @@ -1,5 +1,5 @@ use crate::ARBITRARY_ATTRIBUTE_NAME; -use proc_macro2::{Group, Span, TokenStream, TokenTree}; +use proc_macro2::{Span, TokenStream, TokenTree}; use quote::quote; use syn::{spanned::Spanned, *}; @@ -33,7 +33,7 @@ fn fetch_attr_from_field(field: &Field) -> Result> { .attrs .iter() .filter(|a| { - let path = &a.path; + let path = a.path(); let name = quote!(#path).to_string(); name == ARBITRARY_ATTRIBUTE_NAME }) @@ -49,38 +49,28 @@ fn fetch_attr_from_field(field: &Field) -> Result> { } fn parse_attribute(attr: &Attribute) -> Result { - let group = { - let mut tokens_iter = attr.clone().tokens.into_iter(); - let token = tokens_iter.next().ok_or_else(|| { - let msg = format!("#[{ARBITRARY_ATTRIBUTE_NAME}] cannot be empty."); - syn::Error::new(attr.span(), msg) - })?; - match token { - TokenTree::Group(g) => g, - t => { - let msg = format!("#[{ARBITRARY_ATTRIBUTE_NAME}] must contain a group, got: {t})"); - return Err(syn::Error::new(attr.span(), msg)); - } - } - }; - parse_attribute_internals(group) + if let Meta::List(ref meta_list) = attr.meta { + parse_attribute_internals(meta_list) + } else { + let msg = format!("#[{ARBITRARY_ATTRIBUTE_NAME}] must contain a group"); + Err(syn::Error::new(attr.span(), msg)) + } } -fn parse_attribute_internals(group: Group) -> Result { - let stream = group.stream(); - let mut tokens_iter = stream.into_iter(); +fn parse_attribute_internals(meta_list: &MetaList) -> Result { + let mut tokens_iter = meta_list.tokens.clone().into_iter(); let token = tokens_iter.next().ok_or_else(|| { let msg = format!("#[{ARBITRARY_ATTRIBUTE_NAME}] cannot be empty."); - syn::Error::new(group.span(), msg) + syn::Error::new(meta_list.span(), msg) })?; match token.to_string().as_ref() { "default" => Ok(FieldConstructor::Default), "with" => { - let func_path = parse_assigned_value("with", tokens_iter, group.span())?; + let func_path = parse_assigned_value("with", tokens_iter, meta_list.span())?; Ok(FieldConstructor::With(func_path)) } "value" => { - let value = parse_assigned_value("value", tokens_iter, group.span())?; + let value = parse_assigned_value("value", tokens_iter, meta_list.span())?; Ok(FieldConstructor::Value(value)) } _ => { diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 5e05522..886bb49 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -71,9 +71,9 @@ fn expand_derive_arbitrary(input: syn::DeriveInput) -> Result { // Returns: (lifetime without bounds, lifetime with bounds) // Example: ("'arbitrary", "'arbitrary: 'a + 'b") -fn build_arbitrary_lifetime(generics: Generics) -> (LifetimeDef, LifetimeDef) { +fn build_arbitrary_lifetime(generics: Generics) -> (LifetimeParam, LifetimeParam) { let lifetime_without_bounds = - LifetimeDef::new(Lifetime::new(ARBITRARY_LIFETIME_NAME, Span::call_site())); + LifetimeParam::new(Lifetime::new(ARBITRARY_LIFETIME_NAME, Span::call_site())); let mut lifetime_with_bounds = lifetime_without_bounds.clone(); for param in generics.params.iter() { @@ -89,7 +89,7 @@ fn build_arbitrary_lifetime(generics: Generics) -> (LifetimeDef, LifetimeDef) { fn apply_trait_bounds( mut generics: Generics, - lifetime: LifetimeDef, + lifetime: LifetimeParam, container_attrs: &ContainerAttributes, ) -> Result { // If user-supplied bounds exist, apply them to their matching type parameters. @@ -133,7 +133,7 @@ fn apply_trait_bounds( } // Add a bound `T: Arbitrary` to every type parameter T. -fn add_trait_bounds(mut generics: Generics, lifetime: LifetimeDef) -> Generics { +fn add_trait_bounds(mut generics: Generics, lifetime: LifetimeParam) -> Generics { for param in generics.params.iter_mut() { if let GenericParam::Type(type_param) = param { type_param @@ -174,13 +174,13 @@ fn with_recursive_count_guard( fn gen_arbitrary_method( input: &DeriveInput, - lifetime: LifetimeDef, + lifetime: LifetimeParam, recursive_count: &syn::Ident, ) -> Result { fn arbitrary_structlike( fields: &Fields, ident: &syn::Ident, - lifetime: LifetimeDef, + lifetime: LifetimeParam, recursive_count: &syn::Ident, ) -> Result { let arbitrary = construct(fields, |_idx, field| gen_constructor_for_field(field))?;