From 93e1b51a9e1f9e7cb6fb0b402638eb30e10761f0 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 12 Aug 2022 12:06:38 +0200 Subject: [PATCH] Avoid unhelpful compiler suggestion (#1251) --- axum-macros/src/debug_handler.rs | 33 +++++++++++-------- .../tests/debug_handler/fail/generics.rs | 2 +- .../tests/debug_handler/fail/generics.stderr | 13 +------- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/axum-macros/src/debug_handler.rs b/axum-macros/src/debug_handler.rs index 7945e8bb84..cc0a682242 100644 --- a/axum-macros/src/debug_handler.rs +++ b/axum-macros/src/debug_handler.rs @@ -7,10 +7,26 @@ pub(crate) fn expand(attr: Attrs, item_fn: ItemFn) -> TokenStream { let check_request_last_extractor = check_request_last_extractor(&item_fn); let check_path_extractor = check_path_extractor(&item_fn); let check_multiple_body_extractors = check_multiple_body_extractors(&item_fn); - - let check_inputs_impls_from_request = check_inputs_impls_from_request(&item_fn, &attr.body_ty); let check_output_impls_into_response = check_output_impls_into_response(&item_fn); - let check_future_send = check_future_send(&item_fn); + + // If the function is generic, we can't reliably check its inputs or whether the future it + // returns is `Send`. Skip those checks to avoid unhelpful additional compiler errors. + let check_inputs_and_future_send = if item_fn.sig.generics.params.is_empty() { + let check_inputs_impls_from_request = + check_inputs_impls_from_request(&item_fn, &attr.body_ty); + let check_future_send = check_future_send(&item_fn); + + quote! { + #check_inputs_impls_from_request + #check_future_send + } + } else { + syn::Error::new_spanned( + &item_fn.sig.generics, + "`#[axum_macros::debug_handler]` doesn't support generic functions", + ) + .into_compile_error() + }; quote! { #item_fn @@ -18,9 +34,8 @@ pub(crate) fn expand(attr: Attrs, item_fn: ItemFn) -> TokenStream { #check_request_last_extractor #check_path_extractor #check_multiple_body_extractors - #check_inputs_impls_from_request #check_output_impls_into_response - #check_future_send + #check_inputs_and_future_send } } @@ -153,14 +168,6 @@ fn check_multiple_body_extractors(item_fn: &ItemFn) -> TokenStream { } fn check_inputs_impls_from_request(item_fn: &ItemFn, body_ty: &Type) -> TokenStream { - if !item_fn.sig.generics.params.is_empty() { - return syn::Error::new_spanned( - &item_fn.sig.generics, - "`#[axum_macros::debug_handler]` doesn't support generic functions", - ) - .into_compile_error(); - } - item_fn .sig .inputs diff --git a/axum-macros/tests/debug_handler/fail/generics.rs b/axum-macros/tests/debug_handler/fail/generics.rs index 310de31867..dd15076761 100644 --- a/axum-macros/tests/debug_handler/fail/generics.rs +++ b/axum-macros/tests/debug_handler/fail/generics.rs @@ -1,6 +1,6 @@ use axum_macros::debug_handler; #[debug_handler] -async fn handler() {} +async fn handler(extract: T) {} fn main() {} diff --git a/axum-macros/tests/debug_handler/fail/generics.stderr b/axum-macros/tests/debug_handler/fail/generics.stderr index 8be6889fd3..4a96a0e3cd 100644 --- a/axum-macros/tests/debug_handler/fail/generics.stderr +++ b/axum-macros/tests/debug_handler/fail/generics.stderr @@ -1,16 +1,5 @@ error: `#[axum_macros::debug_handler]` doesn't support generic functions --> tests/debug_handler/fail/generics.rs:4:17 | -4 | async fn handler() {} +4 | async fn handler(extract: T) {} | ^^^ - -error[E0282]: type annotations needed - --> tests/debug_handler/fail/generics.rs:4:10 - | -4 | async fn handler() {} - | ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `handler` - | -help: consider specifying the generic argument - | -4 | async fn handler::() {} - | +++++