Skip to content

Commit 5a35a5e

Browse files
authored
Merge pull request #16 from dtolnay/life
Work around lifetime that does not appear in bounds
2 parents 1884438 + 9611f6f commit 5a35a5e

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-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};
@@ -267,11 +267,13 @@ fn transform_block(
267267
.extend(where_clause.predicates);
268268
}
269269

270-
standalone
271-
.decl
272-
.generics
273-
.params
274-
.push(parse_quote!('async_trait));
270+
if has_async_lifetime(&mut standalone, block) {
271+
standalone
272+
.decl
273+
.generics
274+
.params
275+
.push(parse_quote!('async_trait));
276+
}
275277

276278
let mut types = standalone
277279
.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>,

tests/test.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,16 @@ mod issue11 {
191191
async fn example(self: Arc<Self>) {}
192192
}
193193
}
194+
195+
// https://github.com/dtolnay/async-trait/issues/15
196+
mod issue15 {
197+
use async_trait::async_trait;
198+
use std::marker::PhantomData;
199+
200+
trait Trait {}
201+
202+
#[async_trait]
203+
trait Issue15 {
204+
async fn myfn(&self, _: PhantomData<dyn Trait + Send>) {}
205+
}
206+
}

0 commit comments

Comments
 (0)