From c98a408176cae0294c5f5ba2a9e4c7375aa9cfcb Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 16 Mar 2024 16:31:26 -0700 Subject: [PATCH] Minimize use of async token's span in expanded code --- src/bound.rs | 10 ++++++---- src/expand.rs | 29 ++++++++++------------------- tests/test.rs | 1 + 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/bound.rs b/src/bound.rs index 50182f6..1210517 100644 --- a/src/bound.rs +++ b/src/bound.rs @@ -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}; @@ -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); } } diff --git a/src/expand.rs b/src/expand.rs index 867ae59..bb37597 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -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, @@ -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 )] @@ -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)), }; @@ -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 { @@ -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 }); } @@ -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 + #bounds >> @@ -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; } diff --git a/tests/test.rs b/tests/test.rs index 4b01bad..0d5aacd 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1345,6 +1345,7 @@ pub mod issue158 { fn f() {} #[async_trait] + #[allow(unused_qualifications)] pub trait Trait { async fn f(&self) { self::f();