Skip to content

Commit 0bc7248

Browse files
authored
Merge pull request #32 from kas-gui/work
0.9.0: update to syn v2
2 parents 45990e9 + 98ab3a8 commit 0bc7248

11 files changed

Lines changed: 59 additions & 110 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
33
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
44

5-
## [0.8.0] — 2022-02-07
5+
## [0.9.0] — 2023-06-28
6+
7+
- Update to syn v2.0.0
8+
9+
## [0.8.0] — 2023-02-07
610

711
- Bump MSRV to 1.58.0 (#31)
812
- `#[autoimpl(Clone, Debug, PartialEq, Eq, Hash)]` now all support enums

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "impl-tools"
3-
version = "0.8.0"
3+
version = "0.9.0"
44
authors = ["Diggory Hardy <git@dhardy.name>"]
55
edition = "2021"
66
license = "MIT/Apache-2.0"
@@ -18,10 +18,10 @@ proc-macro = true
1818
proc-macro-error = "1.0"
1919

2020
[dependencies.syn]
21-
version = "1.0.14"
21+
version = "2.0.0"
2222

2323
[dependencies.impl-tools-lib]
24-
version = "0.8.0"
24+
version = "0.9.0"
2525
path = "lib"
2626

2727
[dev-dependencies]

lib/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "impl-tools-lib"
3-
version = "0.8.0"
3+
version = "0.9.0"
44
authors = ["Diggory Hardy <git@dhardy.name>"]
55
edition = "2021"
66
license = "MIT/Apache-2.0"
@@ -16,7 +16,7 @@ proc-macro2 = { version = "1.0" }
1616
proc-macro-error = "1.0"
1717

1818
[dependencies.syn]
19-
version = "1.0.14"
19+
version = "2.0.0"
2020
# We need 'extra-traits' for equality testing
2121
# We need 'full' for parsing macros within macro arguments
2222
features = ["extra-traits", "full", "visit", "visit-mut"]

lib/src/autoimpl.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,15 @@ impl ImplTraits {
297297
item: Toks,
298298
find_impl: impl Fn(&Path) -> Option<&'static dyn ImplTrait>,
299299
) -> Toks {
300-
let mut toks = Toks::new();
301300
match parse2::<Item>(item) {
302-
Ok(Item::Enum(item)) => toks = self.expand_enum(item, find_impl),
303-
Ok(Item::Struct(item)) => toks = self.expand_struct(item, find_impl),
304-
Ok(item) => emit_error!(item, "expected struct"),
305-
Err(err) => emit_error!(err),
301+
Ok(Item::Enum(item)) => self.expand_enum(item, find_impl),
302+
Ok(Item::Struct(item)) => self.expand_struct(item, find_impl),
303+
Ok(item) => {
304+
emit_error!(item, "expected struct");
305+
Toks::new()
306+
}
307+
Err(err) => err.into_compile_error(),
306308
}
307-
toks
308309
}
309310

310311
fn expand_enum(

lib/src/autoimpl/impl_using.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl ImplTrait for ImplDeref {
144144
}) => {
145145
let mut result = None;
146146
for arg in args {
147-
if let syn::GenericArgument::Binding(b) = arg {
147+
if let syn::GenericArgument::AssocType(b) = arg {
148148
if b.ident == "Target" && result.is_none() {
149149
result = Some(b.ty.clone());
150150
continue;

lib/src/default.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use proc_macro_error::emit_error;
1111
use quote::quote;
1212
use syn::parse::{Error, Parse, ParseStream, Result};
1313
use syn::spanned::Spanned;
14-
use syn::{parse2, Attribute, Expr, Generics, Ident, Item, Token};
14+
use syn::{parse2, Attribute, Expr, Generics, Ident, Item, Meta, Token};
1515

1616
/// `#[impl_default]` attribute
1717
pub struct ImplDefault {
@@ -83,14 +83,18 @@ impl ImplDefault {
8383
}
8484

8585
fn parse_attr(attr: Attribute) -> Result<Self> {
86-
if attr.tokens.is_empty() {
87-
return Ok(ImplDefault {
86+
match attr.meta {
87+
Meta::Path(_) => Ok(ImplDefault {
8888
expr: None,
8989
where_clause: None,
9090
span: attr.span(),
91-
});
91+
}),
92+
Meta::List(list) => list.parse_args(),
93+
Meta::NameValue(meta) => Err(Error::new_spanned(
94+
meta,
95+
"expected #[impl_default] or #[impl_default(EXPR)]",
96+
)),
9297
}
93-
attr.parse_args()
9498
}
9599
}
96100

lib/src/fields.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub(crate) mod parsing {
9393
let brace_token = braced!(content in input);
9494
Ok(FieldsNamed {
9595
brace_token,
96-
fields: content.parse_terminated(Field::parse_named)?,
96+
fields: content.parse_terminated(Field::parse_named, Token![,])?,
9797
})
9898
}
9999
}
@@ -104,7 +104,7 @@ pub(crate) mod parsing {
104104
let paren_token = parenthesized!(content in input);
105105
Ok(FieldsUnnamed {
106106
paren_token,
107-
fields: content.parse_terminated(Field::parse_unnamed)?,
107+
fields: content.parse_terminated(Field::parse_unnamed, Token![,])?,
108108
})
109109
}
110110
}
@@ -186,7 +186,7 @@ pub(crate) mod parsing {
186186
fn parse_braced(input: ParseStream) -> Result<FieldsNamed> {
187187
let content;
188188
let brace_token = braced!(content in input);
189-
let fields = content.parse_terminated(Field::parse_named)?;
189+
let fields = content.parse_terminated(Field::parse_named, Token![,])?;
190190
Ok(FieldsNamed {
191191
brace_token,
192192
fields,

lib/src/for_deref.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use quote::{quote, ToTokens, TokenStreamExt};
1212
use std::{iter, slice};
1313
use syn::punctuated::Punctuated;
1414
use syn::spanned::Spanned;
15-
use syn::token::{Colon2, Comma, Eq};
15+
use syn::token::{Comma, Eq, PathSep};
1616
use syn::{parse_quote, Attribute, FnArg, Ident, Item, Token, TraitItem, Type, TypePath};
1717

1818
/// Autoimpl for types supporting `Deref`
@@ -151,10 +151,7 @@ impl ForDeref {
151151
emit_error!(item, "expected trait");
152152
return TokenStream::new();
153153
}
154-
Err(err) => {
155-
emit_error!(err);
156-
return TokenStream::new();
157-
}
154+
Err(err) => return err.into_compile_error(),
158155
};
159156

160157
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
@@ -182,7 +179,7 @@ impl ForDeref {
182179
match item {
183180
TraitItem::Const(item) => {
184181
for attr in item.attrs.iter() {
185-
if attr.path == parse_quote! { cfg } {
182+
if *attr.path() == parse_quote! { cfg } {
186183
attr.to_tokens(tokens);
187184
}
188185
}
@@ -194,14 +191,14 @@ impl ForDeref {
194191

195192
Eq::default().to_tokens(tokens);
196193
definitive.to_tokens(tokens);
197-
Colon2::default().to_tokens(tokens);
194+
PathSep::default().to_tokens(tokens);
198195
item.ident.to_tokens(tokens);
199196

200197
item.semi_token.to_tokens(tokens);
201198
}
202-
TraitItem::Method(item) => {
199+
TraitItem::Fn(item) => {
203200
for attr in item.attrs.iter() {
204-
if attr.path == parse_quote! { cfg } {
201+
if *attr.path() == parse_quote! { cfg } {
205202
attr.to_tokens(tokens);
206203
}
207204
}
@@ -255,7 +252,7 @@ impl ForDeref {
255252
}
256253
TraitItem::Type(item) => {
257254
for attr in item.attrs.iter() {
258-
if attr.path == parse_quote! { cfg } {
255+
if *attr.path() == parse_quote! { cfg } {
259256
attr.to_tokens(tokens);
260257
}
261258
}
@@ -275,7 +272,7 @@ impl ForDeref {
275272

276273
Eq::default().to_tokens(tokens);
277274
definitive.to_tokens(tokens);
278-
Colon2::default().to_tokens(tokens);
275+
PathSep::default().to_tokens(tokens);
279276
item.ident.to_tokens(tokens);
280277
ty_generics.to_tokens(tokens);
281278

lib/src/generics.rs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use proc_macro2::TokenStream;
99
use quote::{quote, ToTokens, TokenStreamExt};
1010
use syn::parse::{Parse, ParseStream, Result};
1111
use syn::punctuated::{Pair, Punctuated};
12-
use syn::{parenthesized, token};
13-
use syn::{Attribute, ConstParam, LifetimeDef, PredicateLifetime, TraitBound};
12+
use syn::token;
13+
use syn::{Attribute, ConstParam, LifetimeParam, PredicateLifetime};
1414
use syn::{BoundLifetimes, Ident, Lifetime, Token, Type};
1515

1616
/// Lifetimes and type parameters attached an item
@@ -50,7 +50,7 @@ pub enum GenericParam {
5050
/// Type parameter
5151
Type(TypeParam),
5252
/// Lifetime parameter
53-
Lifetime(LifetimeDef),
53+
Lifetime(LifetimeParam),
5454
/// `const` parameter
5555
Const(ConstParam),
5656
}
@@ -77,16 +77,13 @@ pub struct TypeParam {
7777

7878
/// A trait or lifetime used as a bound on a type parameter.
7979
///
80-
/// This is a custom variant of [`syn::TypeParamBound`]
81-
/// which supports `trait` as a parameter bound.
80+
/// This is a superset of [`syn::TypeParamBound`].
8281
#[derive(Debug)]
8382
pub enum TypeParamBound {
84-
/// A named trait used as a bound
85-
Trait(TraitBound),
8683
/// `trait` used as a bound (substituted for the trait name by [`ToTokensSubst`])
8784
TraitSubst(Token![trait]),
88-
/// A lifetime bound
89-
Lifetime(Lifetime),
85+
/// Everything else
86+
Other(syn::TypeParamBound),
9087
}
9188

9289
/// A `where` clause in a definition: `where T: Deserialize<'de>, D: 'static`.
@@ -152,7 +149,7 @@ mod parsing {
152149
let attrs = input.call(Attribute::parse_outer)?;
153150
let lookahead = input.lookahead1();
154151
if lookahead.peek(Lifetime) {
155-
params.push_value(GenericParam::Lifetime(LifetimeDef {
152+
params.push_value(GenericParam::Lifetime(LifetimeParam {
156153
attrs,
157154
..input.parse()?
158155
}));
@@ -239,23 +236,11 @@ mod parsing {
239236

240237
impl Parse for TypeParamBound {
241238
fn parse(input: ParseStream) -> Result<Self> {
242-
if input.peek(Lifetime) {
243-
return input.parse().map(TypeParamBound::Lifetime);
244-
}
245-
246239
if input.peek(Token![trait]) {
247-
return input.parse().map(TypeParamBound::TraitSubst);
248-
}
249-
250-
if input.peek(token::Paren) {
251-
let content;
252-
let paren_token = parenthesized!(content in input);
253-
let mut bound: TraitBound = content.parse()?;
254-
bound.paren_token = Some(paren_token);
255-
return Ok(TypeParamBound::Trait(bound));
240+
input.parse().map(TypeParamBound::TraitSubst)
241+
} else {
242+
syn::TypeParamBound::parse(input).map(TypeParamBound::Other)
256243
}
257-
258-
input.parse().map(TypeParamBound::Trait)
259244
}
260245
}
261246

@@ -470,19 +455,15 @@ mod printing_subst {
470455
impl ToTokensSubst for TypeParamBound {
471456
fn to_tokens_subst(&self, tokens: &mut TokenStream, subst: &TokenStream) {
472457
match self {
473-
TypeParamBound::Trait(t) => t.to_tokens(tokens),
474458
TypeParamBound::TraitSubst(_) => tokens.append_all(quote! { #subst }),
475-
TypeParamBound::Lifetime(lt) => lt.to_tokens(tokens),
459+
TypeParamBound::Other(bound) => bound.to_tokens(tokens),
476460
}
477461
}
478462
}
479463
}
480464

481465
fn map_type_param_bound(bound: &syn::TypeParamBound) -> TypeParamBound {
482-
match bound {
483-
syn::TypeParamBound::Trait(bound) => TypeParamBound::Trait(bound.clone()),
484-
syn::TypeParamBound::Lifetime(bound) => TypeParamBound::Lifetime(bound.clone()),
485-
}
466+
TypeParamBound::Other(bound.clone())
486467
}
487468

488469
fn map_generic_param(param: &syn::GenericParam) -> GenericParam {

lib/src/scope.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl Scope {
154154

155155
let mut i = 0;
156156
while i < self.attrs.len() {
157-
if let Some(rule) = find_rule(&self.attrs[i].path) {
157+
if let Some(rule) = find_rule(&self.attrs[i].path()) {
158158
let attr = self.attrs.remove(i);
159159

160160
if !rule.support_repetition() {
@@ -215,7 +215,7 @@ mod parsing {
215215
use crate::fields::parsing::data_struct;
216216
use syn::parse::{Parse, ParseStream};
217217
use syn::spanned::Spanned;
218-
use syn::{braced, bracketed, AttrStyle, Error, Field, Lifetime, Path, TypePath, WhereClause};
218+
use syn::{braced, Error, Field, Lifetime, Path, TypePath, WhereClause};
219219

220220
impl Parse for Scope {
221221
fn parse(input: ParseStream) -> Result<Self> {
@@ -371,7 +371,7 @@ mod parsing {
371371

372372
let content;
373373
let brace_token = braced!(content in input);
374-
parse_attrs_inner(&content, &mut attrs)?;
374+
attrs.extend(Attribute::parse_inner(&content)?);
375375

376376
let mut items = Vec::new();
377377
while !content.is_empty() {
@@ -398,7 +398,7 @@ mod parsing {
398398

399399
let content;
400400
let brace = braced!(content in input);
401-
let variants = content.parse_terminated(Variant::parse)?;
401+
let variants = content.parse_terminated(Variant::parse, Token![,])?;
402402

403403
Ok((where_clause, brace, variants))
404404
}
@@ -412,28 +412,9 @@ mod parsing {
412412
pub(crate) fn parse_braced(input: ParseStream) -> Result<FieldsNamed> {
413413
let content;
414414
let brace_token = braced!(content in input);
415-
let named = content.parse_terminated(Field::parse_named)?;
415+
let named = content.parse_terminated(Field::parse_named, Token![,])?;
416416
Ok(FieldsNamed { brace_token, named })
417417
}
418-
419-
fn parse_attrs_inner(input: ParseStream, attrs: &mut Vec<Attribute>) -> Result<()> {
420-
while input.peek(Token![#]) && input.peek2(Token![!]) {
421-
let pound_token = input.parse()?;
422-
let style = AttrStyle::Inner(input.parse()?);
423-
let content;
424-
let bracket_token = bracketed!(content in input);
425-
let path = content.call(Path::parse_mod_style)?;
426-
let tokens = content.parse()?;
427-
attrs.push(Attribute {
428-
pound_token,
429-
style,
430-
bracket_token,
431-
path,
432-
tokens,
433-
});
434-
}
435-
Ok(())
436-
}
437418
}
438419

439420
mod printing {

0 commit comments

Comments
 (0)