Skip to content

Commit

Permalink
Merge pull request #206 from dtolnay/paren
Browse files Browse the repository at this point in the history
Parenthesize other cases that require parens around impl Trait
  • Loading branch information
dtolnay committed Jun 2, 2022
2 parents e26dad7 + bcdc758 commit a181d9e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/lifetime.rs
Expand Up @@ -2,8 +2,8 @@ use proc_macro2::{Span, TokenStream};
use std::mem;
use syn::visit_mut::{self, VisitMut};
use syn::{
parse_quote_spanned, token, Expr, GenericArgument, Lifetime, Receiver, Type, TypeImplTrait,
TypeParen, TypeReference,
parse_quote_spanned, token, Expr, GenericArgument, Lifetime, Receiver, ReturnType, Type,
TypeBareFn, TypeImplTrait, TypeParen, TypePtr, TypeReference,
};

pub struct CollectLifetimes {
Expand Down Expand Up @@ -81,19 +81,35 @@ impl VisitMut for AddLifetimeToImplTrait {
}

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),
});
}
parenthesize_impl_trait(&mut ty.elem, ty.and_token.span);
visit_mut::visit_type_reference_mut(self, ty);
}

fn visit_type_ptr_mut(&mut self, ty: &mut TypePtr) {
parenthesize_impl_trait(&mut ty.elem, ty.star_token.span);
visit_mut::visit_type_ptr_mut(self, ty);
}

fn visit_type_bare_fn_mut(&mut self, ty: &mut TypeBareFn) {
if let ReturnType::Type(arrow, return_type) = &mut ty.output {
parenthesize_impl_trait(return_type, arrow.spans[0]);
}
visit_mut::visit_type_bare_fn_mut(self, ty);
}

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 }]);
}
}

fn parenthesize_impl_trait(elem: &mut Type, paren_span: Span) {
if let Type::ImplTrait(_) = *elem {
let placeholder = Type::Verbatim(TokenStream::new());
*elem = Type::Paren(TypeParen {
paren_token: token::Paren(paren_span),
elem: Box::new(mem::replace(elem, placeholder)),
});
}
}
1 change: 1 addition & 0 deletions tests/test.rs
Expand Up @@ -1447,5 +1447,6 @@ pub mod issue204 {
#[async_trait]
pub trait Trait {
async fn f(arg: &impl Trait);
async fn g(arg: *const impl Trait);
}
}

0 comments on commit a181d9e

Please sign in to comment.