Skip to content

Commit

Permalink
Merge pull request #262 from dtolnay/asyncspan
Browse files Browse the repository at this point in the history
Minimize use of `async` token's span in expanded code
  • Loading branch information
dtolnay authored Mar 16, 2024
2 parents 2f5343c + c98a408 commit 8d51928
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
10 changes: 6 additions & 4 deletions src/bound.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote_spanned;
use quote::{quote, ToTokens};
use syn::punctuated::Punctuated;
use syn::{Token, TypeParamBound};

Expand Down Expand Up @@ -34,10 +34,12 @@ impl InferredBound {
InferredBound::Sync => "Sync",
}
}
}

pub fn spanned_path(&self, span: Span) -> TokenStream {
let ident = Ident::new(self.as_str(), span);
quote_spanned!(span=> ::core::marker::#ident)
impl ToTokens for InferredBound {
fn to_tokens(&self, tokens: &mut TokenStream) {
let ident = Ident::new(self.as_str(), Span::call_site());
quote!(::core::marker::#ident).to_tokens(tokens);
}
}

Expand Down
29 changes: 10 additions & 19 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ pub fn expand(input: &mut Item, is_local: bool) {
fn lint_suppress_with_body() -> Attribute {
parse_quote! {
#[allow(
unused_qualifications,
clippy::async_yields_async,
clippy::diverging_sub_expression,
clippy::let_unit_value,
Expand All @@ -141,7 +140,6 @@ fn lint_suppress_with_body() -> Attribute {
fn lint_suppress_without_body() -> Attribute {
parse_quote! {
#[allow(
unused_qualifications,
clippy::type_complexity,
clippy::type_repetition_in_bounds
)]
Expand All @@ -168,11 +166,10 @@ fn transform_sig(
has_default: bool,
is_local: bool,
) {
let default_span = sig.asyncness.take().unwrap().span;
sig.fn_token.span = default_span;
sig.fn_token.span = sig.asyncness.take().unwrap().span;

let (ret_arrow, ret) = match &sig.output {
ReturnType::Default => (Token![->](default_span), quote_spanned!(default_span=> ())),
ReturnType::Default => (Token![->](Span::call_site()), quote!(())),
ReturnType::Type(arrow, ret) => (*arrow, quote!(#ret)),
};

Expand Down Expand Up @@ -234,9 +231,7 @@ fn transform_sig(
.push(parse_quote_spanned!(elided.span()=> #elided: 'async_trait));
}

sig.generics
.params
.push(parse_quote_spanned!(default_span=> 'async_trait));
sig.generics.params.push(parse_quote!('async_trait));

if has_self {
let bounds: &[InferredBound] = if is_local {
Expand Down Expand Up @@ -272,21 +267,17 @@ fn transform_sig(
&[InferredBound::Send]
};

let bounds = bounds.iter().filter_map(|bound| {
let bounds = bounds.iter().filter(|bound| {
let assume_bound = match context {
Context::Trait { supertraits, .. } => !has_default || has_bound(supertraits, bound),
Context::Impl { .. } => true,
};
if assume_bound {
None
} else {
Some(bound.spanned_path(default_span))
}
!assume_bound
});

where_clause_or_default(&mut sig.generics.where_clause)
.predicates
.push(parse_quote_spanned! {default_span=>
.push(parse_quote! {
Self: #(#bounds +)* 'async_trait
});
}
Expand Down Expand Up @@ -318,11 +309,11 @@ fn transform_sig(
}

let bounds = if is_local {
quote_spanned!(default_span=> 'async_trait)
quote!('async_trait)
} else {
quote_spanned!(default_span=> ::core::marker::Send + 'async_trait)
quote!(::core::marker::Send + 'async_trait)
};
sig.output = parse_quote_spanned! {default_span=>
sig.output = parse_quote! {
#ret_arrow ::core::pin::Pin<Box<
dyn ::core::future::Future<Output = #ret> + #bounds
>>
Expand Down Expand Up @@ -420,7 +411,7 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
quote!(#(#decls)* { #(#stmts)* })
}
} else {
quote_spanned! {block.brace_token.span=>
quote! {
if let ::core::option::Option::Some(__ret) = ::core::option::Option::None::<#ret> {
return __ret;
}
Expand Down
1 change: 1 addition & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,7 @@ pub mod issue158 {
fn f() {}

#[async_trait]
#[allow(unused_qualifications)]
pub trait Trait {
async fn f(&self) {
self::f();
Expand Down

0 comments on commit 8d51928

Please sign in to comment.