From bdfaedb344fd5095b635ff12b2e8ab62b96de11f Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 20 Jan 2023 11:47:19 +0100 Subject: [PATCH] Use call-site span for future Send check in debug_handler --- axum-macros/src/debug_handler.rs | 24 +++++++++---------- .../tests/debug_handler/fail/not_send.stderr | 19 ++++++--------- .../fail/wrong_return_type.stderr | 4 ++-- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/axum-macros/src/debug_handler.rs b/axum-macros/src/debug_handler.rs index 9934e277..67fd8747 100644 --- a/axum-macros/src/debug_handler.rs +++ b/axum-macros/src/debug_handler.rs @@ -373,7 +373,7 @@ fn check_future_send(item_fn: &ItemFn) -> TokenStream { }; } - let span = item_fn.span(); + let span = item_fn.sig.ident.span(); let handler_name = &item_fn.sig.ident; @@ -383,28 +383,28 @@ fn check_future_send(item_fn: &ItemFn) -> TokenStream { let name = format_ident!("__axum_macros_check_{}_future", item_fn.sig.ident); + let do_check = quote! { + fn check(_: T) + where T: ::std::future::Future + Send + {} + check(future); + }; + if let Some(receiver) = self_receiver(item_fn) { - quote_spanned! {span=> + quote! { #[allow(warnings)] fn #name() { let future = #receiver #handler_name(#(#args),*); - fn check(_: T) - where T: ::std::future::Future + Send - {} - check(future); + #do_check } } } else { - quote_spanned! {span=> + quote! { #[allow(warnings)] fn #name() { #item_fn - let future = #handler_name(#(#args),*); - fn check(_: T) - where T: ::std::future::Future + Send - {} - check(future); + #do_check } } } diff --git a/axum-macros/tests/debug_handler/fail/not_send.stderr b/axum-macros/tests/debug_handler/fail/not_send.stderr index fd5d91e8..465fe938 100644 --- a/axum-macros/tests/debug_handler/fail/not_send.stderr +++ b/axum-macros/tests/debug_handler/fail/not_send.stderr @@ -1,11 +1,8 @@ error: future cannot be sent between threads safely - --> tests/debug_handler/fail/not_send.rs:4:1 + --> tests/debug_handler/fail/not_send.rs:3:1 | -4 | / async fn handler() { -5 | | let rc = std::rc::Rc::new(()); -6 | | async {}.await; -7 | | } - | |_^ future returned by `handler` is not `Send` +3 | #[debug_handler] + | ^^^^^^^^^^^^^^^^ future returned by `handler` is not `Send` | = help: within `impl Future`, the trait `Send` is not implemented for `Rc<()>` note: future is not `Send` as this value is used across an await @@ -18,10 +15,8 @@ note: future is not `Send` as this value is used across an await 7 | } | - `rc` is later dropped here note: required by a bound in `check` - --> tests/debug_handler/fail/not_send.rs:4:1 + --> tests/debug_handler/fail/not_send.rs:3:1 | -4 | / async fn handler() { -5 | | let rc = std::rc::Rc::new(()); -6 | | async {}.await; -7 | | } - | |_^ required by this bound in `check` +3 | #[debug_handler] + | ^^^^^^^^^^^^^^^^ required by this bound in `check` + = note: this error originates in the attribute macro `debug_handler` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/axum-macros/tests/debug_handler/fail/wrong_return_type.stderr b/axum-macros/tests/debug_handler/fail/wrong_return_type.stderr index e6a33a62..46d4673b 100644 --- a/axum-macros/tests/debug_handler/fail/wrong_return_type.stderr +++ b/axum-macros/tests/debug_handler/fail/wrong_return_type.stderr @@ -13,9 +13,9 @@ error[E0277]: the trait bound `bool: IntoResponse` is not satisfied (Response<()>, T1, R) (Response<()>, T1, T2, R) (Response<()>, T1, T2, T3, R) - and 124 others + and $N others note: required by a bound in `__axum_macros_check_handler_into_response::{closure#0}::check` --> tests/debug_handler/fail/wrong_return_type.rs:4:23 | 4 | async fn handler() -> bool { - | ^^^^ required by this bound in `__axum_macros_check_handler_into_response::{closure#0}::check` + | ^^^^ required by this bound in `check`