Skip to content

Commit

Permalink
Avoid unhelpful compiler suggestion (#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte authored and davidpdrsn committed Sep 10, 2022
1 parent 0d9afd9 commit 93e1b51
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
33 changes: 20 additions & 13 deletions axum-macros/src/debug_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,35 @@ 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
#check_extractor_count
#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
}
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion axum-macros/tests/debug_handler/fail/generics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use axum_macros::debug_handler;

#[debug_handler]
async fn handler<T>() {}
async fn handler<T>(extract: T) {}

fn main() {}
13 changes: 1 addition & 12 deletions axum-macros/tests/debug_handler/fail/generics.stderr
Original file line number Diff line number Diff line change
@@ -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<T>() {}
4 | async fn handler<T>(extract: T) {}
| ^^^

error[E0282]: type annotations needed
--> tests/debug_handler/fail/generics.rs:4:10
|
4 | async fn handler<T>() {}
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `handler`
|
help: consider specifying the generic argument
|
4 | async fn handler::<T><T>() {}
| +++++

0 comments on commit 93e1b51

Please sign in to comment.