From b6d02a0acbc938aedf0879981d6b09dc7d276f12 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Wed, 30 Nov 2022 02:43:42 +0900 Subject: [PATCH 1/2] Add regression test for issue 5243 --- tokio/tests/macros_test.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tokio/tests/macros_test.rs b/tokio/tests/macros_test.rs index cf3892ec9d0..85279b7edc0 100644 --- a/tokio/tests/macros_test.rs +++ b/tokio/tests/macros_test.rs @@ -71,3 +71,18 @@ pub mod clippy_semicolon_if_nothing_returned { // To trigger clippy::semicolon_if_nothing_returned lint, the block needs to contain newline. } } + +// https://github.com/tokio-rs/tokio/issues/5243 +pub mod issue_5243 { + macro_rules! mac { + (async fn $name:ident() $b:block) => { + #[::tokio::test] + async fn $name() { + $b + } + }; + } + mac!( + async fn foo() {} + ); +} From cd6d1b98ce52b13b9d788c4f21a0a3a59e992d60 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Wed, 30 Nov 2022 02:46:19 +0900 Subject: [PATCH 2/2] macros: fix span of body variable --- tokio-macros/src/entry.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tokio-macros/src/entry.rs b/tokio-macros/src/entry.rs index 21164d5d5d9..6460e70afaa 100644 --- a/tokio-macros/src/entry.rs +++ b/tokio-macros/src/entry.rs @@ -383,6 +383,7 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To let body = &input.block; let brace_token = input.block.brace_token; + let body_ident = quote! { body }; let block_expr = quote_spanned! {last_stmt_end_span=> #[allow(clippy::expect_used, clippy::diverging_sub_expression)] { @@ -390,7 +391,7 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To .enable_all() .build() .expect("Failed building the Runtime") - .block_on(body); + .block_on(#body_ident); } }; @@ -479,7 +480,7 @@ pub(crate) fn test(args: TokenStream, item: TokenStream, rt_multi_thread: bool) }; let config = if let Some(attr) = input.attrs.iter().find(|attr| attr.path.is_ident("test")) { let msg = "second test attribute is supplied"; - Err(syn::Error::new_spanned(&attr, msg)) + Err(syn::Error::new_spanned(attr, msg)) } else { AttributeArgs::parse_terminated .parse(args)