Skip to content

Commit 82d6990

Browse files
committed
Add 'async_trait lifetime to impl Trait arguments
1 parent a263414 commit 82d6990

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ proc-macro = true
1717
[dependencies]
1818
proc-macro2 = "1.0"
1919
quote = "1.0"
20-
syn = { version = "1.0.84", features = ["full", "visit-mut"] }
20+
syn = { version = "1.0.96", features = ["full", "visit-mut"] }
2121

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

src/expand.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::lifetime::CollectLifetimes;
1+
use crate::lifetime::{AddLifetimeToImplTrait, CollectLifetimes};
22
use crate::parse::Item;
33
use crate::receiver::{has_self_in_block, has_self_in_sig, mut_pat, ReplaceSelf};
44
use proc_macro2::TokenStream;
@@ -283,6 +283,7 @@ fn transform_sig(
283283
let m = mut_pat(&mut arg.pat);
284284
arg.pat = parse_quote!(#m #positional);
285285
}
286+
AddLifetimeToImplTrait.visit_type_mut(&mut arg.ty);
286287
}
287288
}
288289
}

src/lifetime.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use proc_macro2::Span;
22
use syn::visit_mut::{self, VisitMut};
3-
use syn::{GenericArgument, Lifetime, Receiver, TypeReference};
3+
use syn::{
4+
parse_quote_spanned, Expr, GenericArgument, Lifetime, Receiver, TypeImplTrait, TypeReference,
5+
};
46

57
pub struct CollectLifetimes {
68
pub elided: Vec<Lifetime>,
@@ -62,3 +64,22 @@ impl VisitMut for CollectLifetimes {
6264
visit_mut::visit_generic_argument_mut(self, gen);
6365
}
6466
}
67+
68+
pub struct AddLifetimeToImplTrait;
69+
70+
impl VisitMut for AddLifetimeToImplTrait {
71+
fn visit_type_impl_trait_mut(&mut self, ty: &mut TypeImplTrait) {
72+
let span = ty.impl_token.span;
73+
let lifetime = parse_quote_spanned!(span=> 'async_trait);
74+
ty.bounds.insert(0, lifetime);
75+
if let Some(punct) = ty.bounds.pairs_mut().next().unwrap().punct_mut() {
76+
punct.span = span;
77+
}
78+
}
79+
80+
fn visit_expr_mut(&mut self, _e: &mut Expr) {
81+
// Do not recurse into impl Traits inside of an array length expression.
82+
//
83+
// fn outer(arg: [u8; { fn inner(_: impl Trait) {}; 0 }]);
84+
}
85+
}

0 commit comments

Comments
 (0)