Skip to content

Commit

Permalink
Merge pull request #205 from dtolnay/paren
Browse files Browse the repository at this point in the history
Use parentheses to disambiguate impl Trait
  • Loading branch information
dtolnay committed Jun 2, 2022
2 parents a4c76a6 + 91401c8 commit e26dad7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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);
}
}

0 comments on commit e26dad7

Please sign in to comment.