Skip to content

Commit

Permalink
Merge pull request #201 from dtolnay/impltrait
Browse files Browse the repository at this point in the history
Add 'async_trait lifetime to impl Trait arguments
  • Loading branch information
dtolnay committed Jun 2, 2022
2 parents 9a323ed + 1e2a850 commit c3ef648
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -17,7 +17,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0.84", features = ["full", "visit-mut"] }
syn = { version = "1.0.96", features = ["full", "visit-mut"] }

[dev-dependencies]
futures = "0.3"
Expand Down
3 changes: 2 additions & 1 deletion src/expand.rs
@@ -1,4 +1,4 @@
use crate::lifetime::CollectLifetimes;
use crate::lifetime::{AddLifetimeToImplTrait, CollectLifetimes};
use crate::parse::Item;
use crate::receiver::{has_self_in_block, has_self_in_sig, mut_pat, ReplaceSelf};
use proc_macro2::TokenStream;
Expand Down Expand Up @@ -283,6 +283,7 @@ fn transform_sig(
let m = mut_pat(&mut arg.pat);
arg.pat = parse_quote!(#m #positional);
}
AddLifetimeToImplTrait.visit_type_mut(&mut arg.ty);
}
}
}
Expand Down
23 changes: 22 additions & 1 deletion src/lifetime.rs
@@ -1,6 +1,8 @@
use proc_macro2::Span;
use syn::visit_mut::{self, VisitMut};
use syn::{GenericArgument, Lifetime, Receiver, TypeReference};
use syn::{
parse_quote_spanned, Expr, GenericArgument, Lifetime, Receiver, TypeImplTrait, TypeReference,
};

pub struct CollectLifetimes {
pub elided: Vec<Lifetime>,
Expand Down Expand Up @@ -62,3 +64,22 @@ impl VisitMut for CollectLifetimes {
visit_mut::visit_generic_argument_mut(self, gen);
}
}

pub struct AddLifetimeToImplTrait;

impl VisitMut for AddLifetimeToImplTrait {
fn visit_type_impl_trait_mut(&mut self, ty: &mut TypeImplTrait) {
let span = ty.impl_token.span;
let lifetime = parse_quote_spanned!(span=> 'async_trait);
ty.bounds.insert(0, lifetime);
if let Some(punct) = ty.bounds.pairs_mut().next().unwrap().punct_mut() {
punct.span = span;
}
}

fn visit_expr_mut(&mut self, _e: &mut Expr) {
// Do not recurse into impl Traits inside of an array length expression.
//
// fn outer(arg: [u8; { fn inner(_: impl Trait) {}; 0 }]);
}
}
17 changes: 17 additions & 0 deletions tests/test.rs
Expand Up @@ -1374,6 +1374,23 @@ pub mod issue169 {
pub fn test(_t: &dyn Trait) {}
}

// https://github.com/dtolnay/async-trait/issues/177
pub mod issue177 {
use async_trait::async_trait;

#[async_trait]
pub trait Trait {
async fn foo(&self, _callback: impl FnMut(&str) + Send) {}
}

pub struct Struct;

#[async_trait]
impl Trait for Struct {
async fn foo(&self, _callback: impl FnMut(&str) + Send) {}
}
}

// https://github.com/dtolnay/async-trait/issues/183
pub mod issue183 {
#![deny(clippy::shadow_same)]
Expand Down

0 comments on commit c3ef648

Please sign in to comment.