Skip to content

Commit d7a9cae

Browse files
authored
Merge pull request #241 from dtolnay/syn
Update to syn 2
2 parents 15fd282 + 032c150 commit d7a9cae

File tree

7 files changed

+57
-92
lines changed

7 files changed

+57
-92
lines changed

.clippy.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
msrv = "1.39.0"
1+
msrv = "1.56.0"

.github/workflows/ci.yml

-11
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,6 @@ jobs:
3939
if: matrix.rust == 'nightly'
4040
- run: cargo test
4141

42-
msrv:
43-
name: Rust 1.39.0
44-
needs: pre_ci
45-
if: needs.pre_ci.outputs.continue
46-
runs-on: ubuntu-latest
47-
timeout-minutes: 45
48-
steps:
49-
- uses: actions/checkout@v3
50-
- uses: dtolnay/rust-toolchain@1.39.0
51-
- run: cargo check
52-
5342
clippy:
5443
name: Clippy
5544
runs-on: ubuntu-latest

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ edition = "2018"
99
keywords = ["async"]
1010
license = "MIT OR Apache-2.0"
1111
repository = "https://github.com/dtolnay/async-trait"
12-
rust-version = "1.39"
12+
rust-version = "1.56"
1313

1414
[lib]
1515
proc-macro = true
1616

1717
[dependencies]
1818
proc-macro2 = "1.0"
1919
quote = "1.0"
20-
syn = { version = "1.0.96", features = ["full", "visit-mut"] }
20+
syn = { version = "2.0", features = ["full", "visit-mut"] }
2121

2222
[dev-dependencies]
2323
futures = "0.3"

src/expand.rs

+51-70
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syn::punctuated::Punctuated;
1010
use syn::visit_mut::{self, VisitMut};
1111
use syn::{
1212
parse_quote, parse_quote_spanned, Attribute, Block, FnArg, GenericArgument, GenericParam,
13-
Generics, Ident, ImplItem, Lifetime, LifetimeDef, Pat, PatIdent, PathArguments, Receiver,
13+
Generics, Ident, ImplItem, Lifetime, LifetimeParam, Pat, PatIdent, PathArguments, Receiver,
1414
ReturnType, Signature, Stmt, Token, TraitItem, Type, TypePath, WhereClause,
1515
};
1616

@@ -36,7 +36,7 @@ enum Context<'a> {
3636
}
3737

3838
impl Context<'_> {
39-
fn lifetimes<'a>(&'a self, used: &'a [Lifetime]) -> impl Iterator<Item = &'a LifetimeDef> {
39+
fn lifetimes<'a>(&'a self, used: &'a [Lifetime]) -> impl Iterator<Item = &'a LifetimeParam> {
4040
let generics = match self {
4141
Context::Trait { generics, .. } => generics,
4242
Context::Impl { impl_generics, .. } => impl_generics,
@@ -60,7 +60,7 @@ pub fn expand(input: &mut Item, is_local: bool) {
6060
supertraits: &input.supertraits,
6161
};
6262
for inner in &mut input.items {
63-
if let TraitItem::Method(method) = inner {
63+
if let TraitItem::Fn(method) = inner {
6464
let sig = &mut method.sig;
6565
if sig.asyncness.is_some() {
6666
let block = &mut method.default;
@@ -94,7 +94,7 @@ pub fn expand(input: &mut Item, is_local: bool) {
9494
associated_type_impl_traits: &associated_type_impl_traits,
9595
};
9696
for inner in &mut input.items {
97-
if let ImplItem::Method(method) = inner {
97+
if let ImplItem::Fn(method) = inner {
9898
let sig = &mut method.sig;
9999
if sig.asyncness.is_some() {
100100
let block = &mut method.block;
@@ -208,7 +208,7 @@ fn transform_sig(
208208
sig.generics.lt_token = Some(Token![<](sig.ident.span()));
209209
}
210210
if sig.generics.gt_token.is_none() {
211-
sig.generics.gt_token = Some(Token![>](sig.paren_token.span));
211+
sig.generics.gt_token = Some(Token![>](sig.paren_token.span.join()));
212212
}
213213

214214
for elided in lifetimes.elided {
@@ -223,46 +223,35 @@ fn transform_sig(
223223
.push(parse_quote_spanned!(default_span=> 'async_trait));
224224

225225
if has_self {
226-
let bounds: &[InferredBound] = match sig.inputs.iter().next() {
227-
Some(FnArg::Receiver(Receiver {
228-
reference: Some(_),
229-
mutability: None,
230-
..
231-
})) => &[InferredBound::Sync],
232-
Some(FnArg::Typed(arg))
233-
if match arg.pat.as_ref() {
234-
Pat::Ident(pat) => pat.ident == "self",
235-
_ => false,
236-
} =>
237-
{
238-
match arg.ty.as_ref() {
239-
// self: &Self
240-
Type::Reference(ty) if ty.mutability.is_none() => &[InferredBound::Sync],
241-
// self: Arc<Self>
242-
Type::Path(ty)
243-
if {
244-
let segment = ty.path.segments.last().unwrap();
245-
segment.ident == "Arc"
246-
&& match &segment.arguments {
247-
PathArguments::AngleBracketed(arguments) => {
248-
arguments.args.len() == 1
249-
&& match &arguments.args[0] {
250-
GenericArgument::Type(Type::Path(arg)) => {
251-
arg.path.is_ident("Self")
252-
}
253-
_ => false,
226+
let bounds: &[InferredBound] = if let Some(receiver) = sig.receiver() {
227+
match receiver.ty.as_ref() {
228+
// self: &Self
229+
Type::Reference(ty) if ty.mutability.is_none() => &[InferredBound::Sync],
230+
// self: Arc<Self>
231+
Type::Path(ty)
232+
if {
233+
let segment = ty.path.segments.last().unwrap();
234+
segment.ident == "Arc"
235+
&& match &segment.arguments {
236+
PathArguments::AngleBracketed(arguments) => {
237+
arguments.args.len() == 1
238+
&& match &arguments.args[0] {
239+
GenericArgument::Type(Type::Path(arg)) => {
240+
arg.path.is_ident("Self")
254241
}
255-
}
256-
_ => false,
242+
_ => false,
243+
}
257244
}
258-
} =>
259-
{
260-
&[InferredBound::Sync, InferredBound::Send]
261-
}
262-
_ => &[InferredBound::Send],
245+
_ => false,
246+
}
247+
} =>
248+
{
249+
&[InferredBound::Sync, InferredBound::Send]
263250
}
251+
_ => &[InferredBound::Send],
264252
}
265-
_ => &[InferredBound::Send],
253+
} else {
254+
&[InferredBound::Send]
266255
};
267256

268257
let bounds = bounds.iter().filter_map(|bound| {
@@ -286,24 +275,24 @@ fn transform_sig(
286275

287276
for (i, arg) in sig.inputs.iter_mut().enumerate() {
288277
match arg {
289-
FnArg::Receiver(Receiver {
290-
reference: Some(_), ..
291-
}) => {}
292-
FnArg::Receiver(arg) => arg.mutability = None,
278+
FnArg::Receiver(receiver) => {
279+
if receiver.reference.is_none() {
280+
receiver.mutability = None;
281+
}
282+
}
293283
FnArg::Typed(arg) => {
294-
let type_is_reference = match *arg.ty {
295-
Type::Reference(_) => true,
296-
_ => false,
297-
};
298-
if let Pat::Ident(pat) = &mut *arg.pat {
299-
if pat.ident == "self" || !type_is_reference {
284+
if match *arg.ty {
285+
Type::Reference(_) => false,
286+
_ => true,
287+
} {
288+
if let Pat::Ident(pat) = &mut *arg.pat {
300289
pat.by_ref = None;
301290
pat.mutability = None;
291+
} else {
292+
let positional = positional_arg(i, &arg.pat);
293+
let m = mut_pat(&mut arg.pat);
294+
arg.pat = parse_quote!(#m #positional);
302295
}
303-
} else if !type_is_reference {
304-
let positional = positional_arg(i, &arg.pat);
305-
let m = mut_pat(&mut arg.pat);
306-
arg.pat = parse_quote!(#m #positional);
307296
}
308297
AddLifetimeToImplTrait.visit_type_mut(&mut arg.ty);
309298
}
@@ -366,26 +355,18 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
366355
// the parameter, forward it to the variable.
367356
//
368357
// This is currently not applied to the `self` parameter.
369-
let attrs = arg.attrs.iter().filter(|attr| attr.path.is_ident("cfg"));
358+
let attrs = arg.attrs.iter().filter(|attr| attr.path().is_ident("cfg"));
370359

371-
if let Pat::Ident(PatIdent {
360+
if let Type::Reference(_) = *arg.ty {
361+
quote!()
362+
} else if let Pat::Ident(PatIdent {
372363
ident, mutability, ..
373364
}) = &*arg.pat
374365
{
375-
if ident == "self" {
376-
self_span = Some(ident.span());
377-
let prefixed = Ident::new("__self", ident.span());
378-
quote!(let #mutability #prefixed = #ident;)
379-
} else if let Type::Reference(_) = *arg.ty {
380-
quote!()
381-
} else {
382-
quote! {
383-
#(#attrs)*
384-
let #mutability #ident = #ident;
385-
}
366+
quote! {
367+
#(#attrs)*
368+
let #mutability #ident = #ident;
386369
}
387-
} else if let Type::Reference(_) = *arg.ty {
388-
quote!()
389370
} else {
390371
let pat = &arg.pat;
391372
let ident = positional_arg(i, pat);

src/lifetime.rs

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ impl VisitMut for CollectLifetimes {
4646
fn visit_receiver_mut(&mut self, arg: &mut Receiver) {
4747
if let Some((reference, lifetime)) = &mut arg.reference {
4848
self.visit_opt_lifetime(*reference, lifetime);
49+
} else {
50+
visit_mut::visit_type_mut(self, &mut arg.ty);
4951
}
5052
}
5153

src/receiver.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use proc_macro2::{Group, Span, TokenStream, TokenTree};
22
use std::iter::FromIterator;
33
use syn::visit_mut::{self, VisitMut};
44
use syn::{
5-
Block, ExprPath, Ident, Item, Macro, Pat, PatIdent, PatPath, Path, Receiver, Signature, Token,
6-
TypePath,
5+
Block, ExprPath, Ident, Item, Macro, Pat, PatIdent, Path, Receiver, Signature, Token, TypePath,
76
};
87

98
pub fn has_self_in_sig(sig: &mut Signature) -> bool {
@@ -60,11 +59,6 @@ impl VisitMut for HasSelf {
6059
visit_mut::visit_expr_path_mut(self, expr);
6160
}
6261

63-
fn visit_pat_path_mut(&mut self, pat: &mut PatPath) {
64-
self.0 |= pat.path.segments[0].ident == "Self";
65-
visit_mut::visit_pat_path_mut(self, pat);
66-
}
67-
6862
fn visit_type_path_mut(&mut self, ty: &mut TypePath) {
6963
self.0 |= ty.path.segments[0].ident == "Self";
7064
visit_mut::visit_type_path_mut(self, ty);

tests/test.rs

-1
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,6 @@ pub mod issue57 {
741741

742742
// https://github.com/dtolnay/async-trait/issues/68
743743
pub mod issue68 {
744-
#[rustversion::since(1.40)] // procedural macros cannot expand to macro definitions in 1.39.
745744
#[async_trait::async_trait]
746745
pub trait Example {
747746
async fn method(&self) {

0 commit comments

Comments
 (0)