Skip to content

Commit 9611f6f

Browse files
committed
Work around lifetime that does not appear in bounds
1 parent aee7a9e commit 9611f6f

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/expand.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::lifetime::CollectLifetimes;
1+
use crate::lifetime::{has_async_lifetime, CollectLifetimes};
22
use crate::parse::Item;
33
use crate::receiver::{has_self_in_block, has_self_in_sig, ReplaceReceiver};
44
use proc_macro2::{Span, TokenStream};
@@ -249,11 +249,13 @@ fn transform_block(context: Context, sig: &mut MethodSig, block: &mut Block, has
249249
.extend(where_clause.predicates);
250250
}
251251

252-
standalone
253-
.decl
254-
.generics
255-
.params
256-
.push(parse_quote!('async_trait));
252+
if has_async_lifetime(&mut standalone, block) {
253+
standalone
254+
.decl
255+
.generics
256+
.params
257+
.push(parse_quote!('async_trait));
258+
}
257259

258260
let mut types = standalone
259261
.decl

src/lifetime.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
use proc_macro2::Span;
22
use syn::visit_mut::{self, VisitMut};
3-
use syn::{ArgSelfRef, GenericArgument, Lifetime, TypeReference};
3+
use syn::{ArgSelfRef, Block, GenericArgument, Item, Lifetime, MethodSig, TypeReference};
4+
5+
pub fn has_async_lifetime(sig: &mut MethodSig, block: &mut Block) -> bool {
6+
let mut visitor = HasAsyncLifetime(false);
7+
visitor.visit_method_sig_mut(sig);
8+
visitor.visit_block_mut(block);
9+
visitor.0
10+
}
11+
12+
struct HasAsyncLifetime(bool);
13+
14+
impl VisitMut for HasAsyncLifetime {
15+
fn visit_lifetime_mut(&mut self, life: &mut Lifetime) {
16+
self.0 |= life.to_string() == "'async_trait";
17+
}
18+
19+
fn visit_item_mut(&mut self, _: &mut Item) {
20+
// Do not recurse into nested items.
21+
}
22+
}
423

524
pub struct CollectLifetimes {
625
pub lifetimes: Vec<Lifetime>,

0 commit comments

Comments
 (0)