Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'async_trait lifetime to impl Trait arguments #201

Merged
merged 3 commits into from Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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