Skip to content

Commit

Permalink
Update syn requirement from 1.0.81 to 2.0.6 (#251)
Browse files Browse the repository at this point in the history
Updates the requirements on [syn](https://github.com/dtolnay/syn) to
permit the latest version.

Fixes #249

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: tyranron <[email protected]>
  • Loading branch information
dependabot[bot] and tyranron authored Jun 12, 2023
1 parent 70c43f9 commit 1fb07f1
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 125 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- `#[automatically_derived]` is now emitted from all macro expansions. This
should prevent code style linters from attempting to modify the generated
code.
- Upgrade to `syn` 2.0.

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = "1.0.81"
syn = "2.0"
convert_case = { version = "0.6", optional = true }
unicode-xid = { version = "0.2.2", optional = true }

Expand Down
37 changes: 11 additions & 26 deletions impl/src/fmt/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ impl ContainerAttributes {
attrs
.as_ref()
.iter()
.filter(|attr| attr.path.is_ident("debug"))
.filter(|attr| attr.path().is_ident("debug"))
.try_fold(ContainerAttributes::default(), |mut attrs, attr| {
let attr = syn::parse2::<ContainerAttributes>(attr.tokens.clone())?;
let attr = attr.parse_args::<ContainerAttributes>()?;
attrs.bounds.0.extend(attr.bounds.0);
Ok(attrs)
})
Expand All @@ -165,16 +165,9 @@ impl ContainerAttributes {

impl Parse for ContainerAttributes {
fn parse(input: ParseStream) -> Result<Self> {
use proc_macro2::Delimiter::Parenthesis;
BoundsAttribute::check_legacy_fmt(input)?;

let error_span = input.cursor().group(Parenthesis).map(|(_, span, _)| span);
let content;
syn::parenthesized!(content in input);
let error_span = error_span.unwrap_or_else(|| unreachable!());

BoundsAttribute::check_legacy_fmt(&content, error_span)?;

content.parse().map(|bounds| ContainerAttributes { bounds })
input.parse().map(|bounds| ContainerAttributes { bounds })
}
}

Expand Down Expand Up @@ -202,10 +195,10 @@ impl FieldAttribute {
Ok(attrs
.as_ref()
.iter()
.filter(|attr| attr.path.is_ident("debug"))
.filter(|attr| attr.path().is_ident("debug"))
.try_fold(None, |mut attrs, attr| {
let field_attr = syn::parse2::<FieldAttribute>(attr.tokens.clone())?;
if let Some((path, _)) = attrs.replace((&attr.path, field_attr)) {
let field_attr = attr.parse_args::<FieldAttribute>()?;
if let Some((path, _)) = attrs.replace((attr.path(), field_attr)) {
Err(Error::new(
path.span(),
"only single `#[debug(...)]` attribute is allowed here",
Expand All @@ -220,19 +213,12 @@ impl FieldAttribute {

impl Parse for FieldAttribute {
fn parse(input: ParseStream) -> Result<Self> {
use proc_macro2::Delimiter::Parenthesis;

let error_span = input.cursor().group(Parenthesis).map(|(_, span, _)| span);
let content;
syn::parenthesized!(content in input);
let error_span = error_span.unwrap_or_else(|| unreachable!());
FmtAttribute::check_legacy_fmt(input)?;

FmtAttribute::check_legacy_fmt(&content, error_span)?;

if content.peek(syn::LitStr) {
content.parse().map(Self::Fmt)
if input.peek(syn::LitStr) {
input.parse().map(Self::Fmt)
} else {
let _ = content.parse::<syn::Path>().and_then(|p| {
let _ = input.parse::<syn::Path>().and_then(|p| {
if ["skip", "ignore"].into_iter().any(|i| p.is_ident(i)) {
Ok(p)
} else {
Expand All @@ -242,7 +228,6 @@ impl Parse for FieldAttribute {
))
}
})?;

Ok(Self::Skip)
}
}
Expand Down
49 changes: 21 additions & 28 deletions impl/src/fmt/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,22 +219,22 @@ impl Attributes {
attrs
.as_ref()
.iter()
.filter(|attr| attr.path.is_ident(trait_name_to_attribute_name(trait_name)))
.filter(|attr| attr.path().is_ident(trait_name_to_attribute_name(trait_name)))
.try_fold(Attributes::default(), |mut attrs, attr| {
let attr = syn::parse2::<Attribute>(attr.tokens.clone())?;
match attr {
Attribute::Bounds(more) => {
attrs.bounds.0.extend(more.0);
}
Attribute::Fmt(fmt) => {
attrs.fmt.replace(fmt).map_or(Ok(()), |dup| Err(Error::new(
dup.span(),
format!(
"Multiple `#[{}(\"...\", ...)]` attributes aren't allowed",
trait_name_to_attribute_name(trait_name),
))))?;
}
};
let attr = attr.parse_args::<Attribute>()?;
match attr {
Attribute::Bounds(more) => {
attrs.bounds.0.extend(more.0);
}
Attribute::Fmt(fmt) => {
attrs.fmt.replace(fmt).map_or(Ok(()), |dup| Err(Error::new(
dup.span(),
format!(
"Multiple `#[{}(\"...\", ...)]` attributes aren't allowed",
trait_name_to_attribute_name(trait_name),
))))?;
}
};
Ok(attrs)
})
}
Expand All @@ -251,21 +251,14 @@ enum Attribute {
}

impl Parse for Attribute {
fn parse(input: ParseStream) -> Result<Self> {
use proc_macro2::Delimiter::Parenthesis;
fn parse(input: ParseStream<'_>) -> Result<Self> {
BoundsAttribute::check_legacy_fmt(input)?;
FmtAttribute::check_legacy_fmt(input)?;

let error_span = input.cursor().group(Parenthesis).map(|(_, span, _)| span);
let content;
syn::parenthesized!(content in input);
let error_span = error_span.unwrap_or_else(|| unreachable!());

BoundsAttribute::check_legacy_fmt(&content, error_span)?;
FmtAttribute::check_legacy_fmt(&content, error_span)?;

if content.peek(syn::LitStr) {
content.parse().map(Attribute::Fmt)
if input.peek(syn::LitStr) {
input.parse().map(Attribute::Fmt)
} else {
content.parse().map(Attribute::Bounds)
input.parse().map(Attribute::Bounds)
}
}
}
Expand Down
22 changes: 10 additions & 12 deletions impl/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ mod parsing;

use std::{iter, mem};

use proc_macro2::{Ident, Span, TokenStream, TokenTree};
use proc_macro2::{Ident, TokenStream, TokenTree};
use quote::ToTokens;
use syn::{
buffer::Cursor,
parse::{Parse, ParseBuffer, ParseStream},
parse::{Parse, ParseStream},
punctuated::Punctuated,
spanned::Spanned as _,
Error, Result,
token, Error, Result,
};

/// Representation of a macro attribute expressing additional trait bounds.
Expand All @@ -26,13 +26,12 @@ struct BoundsAttribute(Punctuated<syn::WherePredicate, syn::token::Comma>);

impl BoundsAttribute {
/// Errors in case legacy syntax is encountered: `bound = "..."`.
fn check_legacy_fmt(input: &ParseBuffer<'_>, error_span: Span) -> Result<()> {
fn check_legacy_fmt(input: ParseStream<'_>) -> Result<()> {
let fork = input.fork();

let path = fork
.parse::<syn::Path>()
.and_then(|path| fork.parse::<syn::token::Eq>().map(|_| path));

match path {
Ok(path) if path.is_ident("bound") => fork
.parse::<syn::Lit>()
Expand All @@ -43,7 +42,7 @@ impl BoundsAttribute {
})
.map_or(Ok(()), |bound| {
Err(Error::new(
error_span,
input.span(),
format!("legacy syntax, use `bound({bound})` instead"),
))
}),
Expand All @@ -53,7 +52,7 @@ impl BoundsAttribute {
}

impl Parse for BoundsAttribute {
fn parse(input: ParseStream) -> Result<Self> {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let _ = input.parse::<syn::Path>().and_then(|p| {
if ["bound", "bounds", "where"]
.into_iter()
Expand All @@ -72,7 +71,7 @@ impl Parse for BoundsAttribute {
syn::parenthesized!(content in input);

content
.parse_terminated(syn::WherePredicate::parse)
.parse_terminated(syn::WherePredicate::parse, token::Comma)
.map(Self)
}
}
Expand Down Expand Up @@ -130,17 +129,16 @@ impl FmtAttribute {
}

/// Errors in case legacy syntax is encountered: `fmt = "...", (arg),*`.
fn check_legacy_fmt(input: &ParseBuffer<'_>, error_span: Span) -> Result<()> {
fn check_legacy_fmt(input: ParseStream<'_>) -> Result<()> {
let fork = input.fork();

let path = fork
.parse::<syn::Path>()
.and_then(|path| fork.parse::<syn::token::Eq>().map(|_| path));

match path {
Ok(path) if path.is_ident("fmt") => (|| {
let args = fork
.parse_terminated::<_, syn::token::Comma>(syn::Lit::parse)
.parse_terminated(syn::Lit::parse, token::Comma)
.ok()?
.into_iter()
.enumerate()
Expand All @@ -157,7 +155,7 @@ impl FmtAttribute {
})()
.map_or(Ok(()), |fmt| {
Err(Error::new(
error_span,
input.span(),
format!(
"legacy syntax, remove `fmt =` and use `{}` instead",
fmt.join(", "),
Expand Down
Loading

0 comments on commit 1fb07f1

Please sign in to comment.