Skip to content

Commit 014e044

Browse files
committed
Update to syn v2
1 parent 6acd1d5 commit 014e044

25 files changed

+322
-276
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ zeroize-on-drop = ["zeroize"]
2929
[dependencies]
3030
proc-macro2 = { version = "1", default-features = false, features = ["proc-macro"] }
3131
quote = { version = "1", default-features = false }
32-
syn = { version = "1.0.56", default-features = false, features = [
32+
syn = { version = "2", default-features = false, features = [
3333
"clone-impls",
3434
"derive",
3535
"extra-traits",

src/attr/field.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Attribute parsing for fields.
22
3-
use syn::{spanned::Spanned, Attribute, Meta, NestedMeta, Result};
3+
use syn::{punctuated::Punctuated, spanned::Spanned, Attribute, Meta, Result, Token};
44

55
use crate::{DeriveWhere, Error, Skip, DERIVE_WHERE};
66
#[cfg(feature = "zeroize")]
@@ -27,11 +27,8 @@ impl FieldAttr {
2727
let mut self_ = FieldAttr::default();
2828

2929
for attr in attrs {
30-
if attr.path.is_ident(DERIVE_WHERE) {
31-
match attr.parse_meta() {
32-
Ok(meta) => self_.add_meta(derive_wheres, skip_inner, &meta)?,
33-
Err(error) => return Err(Error::attribute_syntax(attr.span(), error)),
34-
}
30+
if attr.path().is_ident(DERIVE_WHERE) {
31+
self_.add_meta(derive_wheres, skip_inner, &attr.meta)?
3532
}
3633
}
3734

@@ -48,31 +45,28 @@ impl FieldAttr {
4845
debug_assert!(meta.path().is_ident(DERIVE_WHERE));
4946

5047
if let Meta::List(list) = meta {
51-
if list.nested.is_empty() {
48+
let nested = list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
49+
50+
if nested.is_empty() {
5251
return Err(Error::empty(list.span()));
5352
}
5453

55-
for nested_meta in &list.nested {
56-
match nested_meta {
57-
NestedMeta::Meta(meta) => {
58-
if meta.path().is_ident(Skip::SKIP) {
59-
self.skip
60-
.add_attribute(derive_wheres, Some(skip_inner), meta)?;
61-
continue;
62-
}
63-
64-
#[cfg(feature = "zeroize")]
65-
{
66-
if meta.path().is_ident(Trait::Zeroize.as_str()) {
67-
self.zeroize_fqs.add_attribute(meta, derive_wheres)?;
68-
continue;
69-
}
70-
}
54+
for meta in &nested {
55+
if meta.path().is_ident(Skip::SKIP) {
56+
self.skip
57+
.add_attribute(derive_wheres, Some(skip_inner), meta)?;
58+
continue;
59+
}
7160

72-
return Err(Error::option(meta.path().span()));
61+
#[cfg(feature = "zeroize")]
62+
{
63+
if meta.path().is_ident(Trait::Zeroize.as_str()) {
64+
self.zeroize_fqs.add_attribute(meta, derive_wheres)?;
65+
continue;
7366
}
74-
_ => return Err(Error::option_syntax(nested_meta.span())),
7567
}
68+
69+
return Err(Error::option(meta.path().span()));
7670
}
7771

7872
Ok(())

src/attr/item.rs

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use syn::{
77
parse::{discouraged::Speculative, Parse, ParseStream},
88
punctuated::Punctuated,
99
spanned::Spanned,
10-
Attribute, Data, Ident, Meta, NestedMeta, Path, PredicateType, Result, Token, TraitBound,
10+
Attribute, Data, Ident, Meta, Path, PredicateType, Result, Token, TraitBound,
1111
TraitBoundModifier, Type, TypeParamBound, TypePath, WhereClause, WherePredicate,
1212
};
1313

@@ -33,61 +33,57 @@ impl ItemAttr {
3333
let mut incomparables = Vec::new();
3434

3535
for attr in attrs {
36-
if attr.path.is_ident(DERIVE_WHERE) {
37-
if let Ok(meta) = attr.parse_meta() {
38-
if let Meta::List(list) = meta {
39-
match list.nested.len() {
36+
if attr.path().is_ident(DERIVE_WHERE) {
37+
if let Meta::List(list) = &attr.meta {
38+
if let Ok(nested) =
39+
list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)
40+
{
41+
match nested.len() {
4042
// Don't allow an empty list.
4143
0 => return Err(Error::empty(list.span())),
4244
// Check for `skip_inner` if list only has one item.
43-
1 => match list
44-
.nested
45-
.into_iter()
46-
.next()
47-
.expect("unexpected empty list")
48-
{
49-
NestedMeta::Meta(meta) => {
50-
if meta.path().is_ident(Skip::SKIP_INNER) {
51-
// Don't allow `skip_inner` on the item level for enums.
52-
if let Data::Enum(_) = data {
53-
return Err(Error::option_enum_skip_inner(meta.span()));
54-
}
55-
56-
// Don't parse `Skip` yet, because it needs access to all
57-
// `DeriveWhere`s.
58-
skip_inners.push(meta);
59-
} else if meta.path().is_ident(Incomparable::INCOMPARABLE) {
60-
// Needs to be parsed after all traits are known.
61-
incomparables.push(meta)
62-
} else if meta.path().is_ident("crate") {
63-
// Do nothing, we checked this before
64-
// already.
65-
}
66-
// The list can have one item but still not be the `skip_inner`
67-
// attribute, continue with parsing `DeriveWhere`.
68-
else {
69-
self_
70-
.derive_wheres
71-
.push(DeriveWhere::from_attr(span, data, attr)?);
45+
1 => {
46+
let meta =
47+
nested.into_iter().next().expect("unexpected empty list");
48+
49+
if meta.path().is_ident(Skip::SKIP_INNER) {
50+
// Don't allow `skip_inner` on the item level for enums.
51+
if let Data::Enum(_) = data {
52+
return Err(Error::option_enum_skip_inner(meta.span()));
7253
}
54+
55+
// Don't parse `Skip` yet, because it needs access to all
56+
// `DeriveWhere`s.
57+
skip_inners.push(meta);
58+
} else if meta.path().is_ident(Incomparable::INCOMPARABLE) {
59+
// Needs to be parsed after all traits are known.
60+
incomparables.push(meta)
61+
} else if meta.path().is_ident("crate") {
62+
// Do nothing, we checked this before
63+
// already.
7364
}
74-
nested_meta => {
75-
return Err(Error::option_syntax(nested_meta.span()))
65+
// The list can have one item but still not be the `skip_inner`
66+
// attribute, continue with parsing `DeriveWhere`.
67+
else {
68+
self_
69+
.derive_wheres
70+
.push(DeriveWhere::from_attr(span, data, attr)?);
7671
}
77-
},
72+
}
7873
_ => self_
7974
.derive_wheres
8075
.push(DeriveWhere::from_attr(span, data, attr)?),
8176
}
82-
} else {
83-
return Err(Error::option_syntax(meta.span()));
8477
}
85-
}
86-
// Anything that can't be parsed by `Meta`, is because `A, B; C` isn't valid syntax.
87-
else {
88-
self_
89-
.derive_wheres
90-
.push(DeriveWhere::from_attr(span, data, attr)?)
78+
// Anything list that isn't using `,` as seperator, is because we expect
79+
// `A, B; C`.
80+
else {
81+
self_
82+
.derive_wheres
83+
.push(DeriveWhere::from_attr(span, data, attr)?)
84+
}
85+
} else {
86+
return Err(Error::option_syntax(attr.meta.span()));
9187
}
9288
}
9389
}
@@ -479,15 +475,18 @@ impl DeriveTrait {
479475
}
480476
}
481477

482-
match meta {
478+
match &meta {
483479
Meta::Path(path) => Ok((path.span(), trait_.default_derive_trait())),
484480
Meta::List(list) => {
485-
if list.nested.is_empty() {
481+
let nested =
482+
list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
483+
484+
if nested.is_empty() {
486485
return Err(Error::option_empty(list.span()));
487486
}
488487

489488
// This will return an error if no options are supported.
490-
Ok((list.span(), trait_.parse_derive_trait(list)?))
489+
Ok((list.span(), trait_.parse_derive_trait(meta.span(), nested)?))
491490
}
492491
Meta::NameValue(name_value) => Err(Error::option_syntax(name_value.span())),
493492
}

src/attr/skip.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::default::Default;
44

5-
use syn::{spanned::Spanned, Meta, NestedMeta, Path, Result};
5+
use syn::{punctuated::Punctuated, spanned::Spanned, Meta, Path, Result, Token};
66

77
use crate::{DeriveWhere, Error, Trait};
88

@@ -78,8 +78,11 @@ impl Skip {
7878
}
7979
}
8080
Meta::List(list) => {
81+
let nested =
82+
list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
83+
8184
// Don't allow an empty list.
82-
if list.nested.is_empty() {
85+
if nested.is_empty() {
8386
return Err(Error::option_empty(list.span()));
8487
}
8588

@@ -100,8 +103,8 @@ impl Skip {
100103
Skip::Traits(traits) => traits,
101104
};
102105

103-
for nested_meta in &list.nested {
104-
if let NestedMeta::Meta(Meta::Path(path)) = nested_meta {
106+
for nested_meta in &nested {
107+
if let Meta::Path(path) = nested_meta {
105108
let skip_group = SkipGroup::from_path(path)?;
106109

107110
// Don't allow to skip the same trait twice.

src/attr/variant.rs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Attribute parsing for variants.
22
3-
use syn::{spanned::Spanned, Attribute, Fields, Meta, NestedMeta, Result, Variant};
3+
use syn::{
4+
punctuated::Punctuated, spanned::Spanned, Attribute, Fields, Meta, Result, Token, Variant,
5+
};
46

57
use crate::{Default, DeriveWhere, Error, Incomparable, Skip, DERIVE_WHERE};
68

@@ -26,11 +28,8 @@ impl VariantAttr {
2628
let mut self_ = VariantAttr::default();
2729

2830
for attr in attrs {
29-
if attr.path.is_ident(DERIVE_WHERE) {
30-
match attr.parse_meta() {
31-
Ok(meta) => self_.add_meta(&meta, derive_wheres, variant)?,
32-
Err(error) => return Err(Error::attribute_syntax(attr.span(), error)),
33-
}
31+
if attr.path().is_ident(DERIVE_WHERE) {
32+
self_.add_meta(&attr.meta, derive_wheres, variant)?
3433
}
3534
}
3635

@@ -47,36 +46,31 @@ impl VariantAttr {
4746
debug_assert!(meta.path().is_ident(DERIVE_WHERE));
4847

4948
if let Meta::List(list) = meta {
50-
if list.nested.is_empty() {
49+
let nested = list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
50+
51+
if nested.is_empty() {
5152
return Err(Error::empty(list.span()));
5253
}
5354

54-
for nested_meta in &list.nested {
55-
match nested_meta {
56-
NestedMeta::Meta(meta) => {
57-
if meta.path().is_ident(Skip::SKIP_INNER) {
58-
// Don't allow `skip_inner` on empty variants.
59-
match &variant.fields {
60-
Fields::Named(fields) if fields.named.is_empty() => {
61-
return Err(Error::option_skip_empty(variant.span()))
62-
}
63-
Fields::Unnamed(fields) if fields.unnamed.is_empty() => {
64-
return Err(Error::option_skip_empty(variant.span()))
65-
}
66-
Fields::Unit => {
67-
return Err(Error::option_skip_empty(variant.span()))
68-
}
69-
_ => self.skip_inner.add_attribute(derive_wheres, None, meta)?,
70-
}
71-
} else if meta.path().is_ident(Default::DEFAULT) {
72-
self.default.add_attribute(meta, derive_wheres)?;
73-
} else if meta.path().is_ident(Incomparable::INCOMPARABLE) {
74-
self.incomparable.add_attribute(meta, derive_wheres)?;
75-
} else {
76-
return Err(Error::option(meta.path().span()));
55+
for meta in &nested {
56+
if meta.path().is_ident(Skip::SKIP_INNER) {
57+
// Don't allow `skip_inner` on empty variants.
58+
match &variant.fields {
59+
Fields::Named(fields) if fields.named.is_empty() => {
60+
return Err(Error::option_skip_empty(variant.span()))
61+
}
62+
Fields::Unnamed(fields) if fields.unnamed.is_empty() => {
63+
return Err(Error::option_skip_empty(variant.span()))
7764
}
65+
Fields::Unit => return Err(Error::option_skip_empty(variant.span())),
66+
_ => self.skip_inner.add_attribute(derive_wheres, None, meta)?,
7867
}
79-
_ => return Err(Error::option_syntax(nested_meta.span())),
68+
} else if meta.path().is_ident(Default::DEFAULT) {
69+
self.default.add_attribute(meta, derive_wheres)?;
70+
} else if meta.path().is_ident(Incomparable::INCOMPARABLE) {
71+
self.incomparable.add_attribute(meta, derive_wheres)?;
72+
} else {
73+
return Err(Error::option(meta.path().span()));
8074
}
8175
}
8276

src/attr/zeroize_fqs.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Attribute parsing for the `Zeroize(fqs)` option.
22
3-
use syn::{spanned::Spanned, Meta, NestedMeta, Result};
3+
use syn::{punctuated::Punctuated, spanned::Spanned, Meta, Result, Token};
44

55
use crate::{DeriveWhere, Error, Trait, TraitImpl};
66

@@ -26,24 +26,26 @@ impl ZeroizeFqs {
2626

2727
match meta {
2828
Meta::List(list) => {
29-
if list.nested.is_empty() {
29+
let nested =
30+
list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
31+
32+
if nested.is_empty() {
3033
return Err(Error::option_empty(list.span()));
3134
}
3235

33-
for nested_meta in &list.nested {
34-
match nested_meta {
35-
NestedMeta::Meta(Meta::Path(path)) => {
36-
if path.is_ident(Self::FQS) {
37-
if self.0 {
38-
return Err(Error::option_duplicate(path.span(), Self::FQS));
39-
} else {
40-
self.0 = true
41-
}
36+
for meta in &nested {
37+
if let Meta::Path(path) = meta {
38+
if path.is_ident(Self::FQS) {
39+
if self.0 {
40+
return Err(Error::option_duplicate(path.span(), Self::FQS));
4241
} else {
43-
return Err(Error::option(path.span()));
42+
self.0 = true
4443
}
44+
} else {
45+
return Err(Error::option(path.span()));
4546
}
46-
_ => return Err(Error::option_syntax(nested_meta.span())),
47+
} else {
48+
return Err(Error::option_syntax(meta.span()));
4749
}
4850
}
4951

0 commit comments

Comments
 (0)