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

Use parentheses to disambiguate impl Trait #205

Merged
merged 2 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
17 changes: 15 additions & 2 deletions src/lifetime.rs
@@ -1,7 +1,9 @@
use proc_macro2::Span;
use proc_macro2::{Span, TokenStream};
use std::mem;
use syn::visit_mut::{self, VisitMut};
use syn::{
parse_quote_spanned, Expr, GenericArgument, Lifetime, Receiver, TypeImplTrait, TypeReference,
parse_quote_spanned, token, Expr, GenericArgument, Lifetime, Receiver, Type, TypeImplTrait,
TypeParen, TypeReference,
};

pub struct CollectLifetimes {
Expand Down Expand Up @@ -78,6 +80,17 @@ impl VisitMut for AddLifetimeToImplTrait {
visit_mut::visit_type_impl_trait_mut(self, ty);
}

fn visit_type_reference_mut(&mut self, ty: &mut TypeReference) {
if let Type::ImplTrait(_) = *ty.elem {
let elem = mem::replace(&mut *ty.elem, Type::Verbatim(TokenStream::new()));
*ty.elem = Type::Paren(TypeParen {
paren_token: token::Paren(ty.and_token.span),
elem: Box::new(elem),
});
}
visit_mut::visit_type_reference_mut(self, ty);
}

fn visit_expr_mut(&mut self, _e: &mut Expr) {
// Do not recurse into impl Traits inside of an array length expression.
//
Expand Down
10 changes: 10 additions & 0 deletions tests/test.rs
Expand Up @@ -1439,3 +1439,13 @@ pub mod issue199 {
assert_eq!(counter.get(), 1);
}
}

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

#[async_trait]
pub trait Trait {
async fn f(arg: &impl Trait);
}
}