Skip to content

Commit

Permalink
Update to syn-2
Browse files Browse the repository at this point in the history
  • Loading branch information
maurer committed Apr 3, 2023
1 parent c20a950 commit a1ce928
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 15 additions & 9 deletions derive/src/container_attributes.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand All @@ -43,13 +43,19 @@ impl ContainerAttributes {
}
};

for nested_meta in meta_list.nested.iter() {
for nested_meta in
meta_list.parse_args_with(Punctuated::<Meta, Token![,]>::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)?);
Expand Down
36 changes: 13 additions & 23 deletions derive/src/field_attributes.rs
Original file line number Diff line number Diff line change
@@ -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, *};

Expand Down Expand Up @@ -33,7 +33,7 @@ fn fetch_attr_from_field(field: &Field) -> Result<Option<&Attribute>> {
.attrs
.iter()
.filter(|a| {
let path = &a.path;
let path = a.path();
let name = quote!(#path).to_string();
name == ARBITRARY_ATTRIBUTE_NAME
})
Expand All @@ -49,38 +49,28 @@ fn fetch_attr_from_field(field: &Field) -> Result<Option<&Attribute>> {
}

fn parse_attribute(attr: &Attribute) -> Result<FieldConstructor> {
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<FieldConstructor> {
let stream = group.stream();
let mut tokens_iter = stream.into_iter();
fn parse_attribute_internals(meta_list: &MetaList) -> Result<FieldConstructor> {
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))
}
_ => {
Expand Down
12 changes: 6 additions & 6 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ fn expand_derive_arbitrary(input: syn::DeriveInput) -> Result<TokenStream> {

// 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() {
Expand All @@ -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<Generics> {
// If user-supplied bounds exist, apply them to their matching type parameters.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -174,13 +174,13 @@ fn with_recursive_count_guard(

fn gen_arbitrary_method(
input: &DeriveInput,
lifetime: LifetimeDef,
lifetime: LifetimeParam,
recursive_count: &syn::Ident,
) -> Result<TokenStream> {
fn arbitrary_structlike(
fields: &Fields,
ident: &syn::Ident,
lifetime: LifetimeDef,
lifetime: LifetimeParam,
recursive_count: &syn::Ident,
) -> Result<TokenStream> {
let arbitrary = construct(fields, |_idx, field| gen_constructor_for_field(field))?;
Expand Down

0 comments on commit a1ce928

Please sign in to comment.