diff --git a/tests-build/tests/fail/macros_invalid_input.rs b/tests-build/tests/fail/macros_invalid_input.rs index 85b4924f225..61a1823dccd 100644 --- a/tests-build/tests/fail/macros_invalid_input.rs +++ b/tests-build/tests/fail/macros_invalid_input.rs @@ -41,6 +41,9 @@ async fn test_crate_not_path_int() {} #[tokio::test(crate = "456")] async fn test_crate_not_path_invalid() {} +#[tokio::test(flavor = "multi_thread", unhandled_panic = "shutdown_runtime")] +async fn test_worker_threads_not_int() {} + #[tokio::test] #[test] async fn test_has_second_test_attr() {} diff --git a/tests-build/tests/fail/macros_invalid_input.stderr b/tests-build/tests/fail/macros_invalid_input.stderr index a7e61adf487..e6e80e8114a 100644 --- a/tests-build/tests/fail/macros_invalid_input.stderr +++ b/tests-build/tests/fail/macros_invalid_input.stderr @@ -76,40 +76,46 @@ error: Failed to parse value of `crate` as path: "456" 41 | #[tokio::test(crate = "456")] | ^^^^^ +error: The `unhandled_panic` option requires the `current_thread` runtime flavor. Use `#[tokio::test(flavor = "current_thread")]` + --> tests/fail/macros_invalid_input.rs:44:58 + | +44 | #[tokio::test(flavor = "multi_thread", unhandled_panic = "shutdown_runtime")] + | ^^^^^^^^^^^^^^^^^^ + error: second test attribute is supplied, consider removing or changing the order of your test attributes - --> tests/fail/macros_invalid_input.rs:45:1 + --> tests/fail/macros_invalid_input.rs:48:1 | -45 | #[test] +48 | #[test] | ^^^^^^^ error: second test attribute is supplied, consider removing or changing the order of your test attributes - --> tests/fail/macros_invalid_input.rs:49:1 + --> tests/fail/macros_invalid_input.rs:52:1 | -49 | #[::core::prelude::v1::test] +52 | #[::core::prelude::v1::test] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: second test attribute is supplied, consider removing or changing the order of your test attributes - --> tests/fail/macros_invalid_input.rs:53:1 + --> tests/fail/macros_invalid_input.rs:56:1 | -53 | #[core::prelude::rust_2015::test] +56 | #[core::prelude::rust_2015::test] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: second test attribute is supplied, consider removing or changing the order of your test attributes - --> tests/fail/macros_invalid_input.rs:57:1 + --> tests/fail/macros_invalid_input.rs:60:1 | -57 | #[::std::prelude::rust_2018::test] +60 | #[::std::prelude::rust_2018::test] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: second test attribute is supplied, consider removing or changing the order of your test attributes - --> tests/fail/macros_invalid_input.rs:61:1 + --> tests/fail/macros_invalid_input.rs:64:1 | -61 | #[std::prelude::rust_2021::test] +64 | #[std::prelude::rust_2021::test] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: second test attribute is supplied, consider removing or changing the order of your test attributes - --> tests/fail/macros_invalid_input.rs:64:1 + --> tests/fail/macros_invalid_input.rs:67:1 | -64 | #[tokio::test] +67 | #[tokio::test] | ^^^^^^^^^^^^^^ | = note: this error originates in the attribute macro `tokio::test` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tokio-macros/src/entry.rs b/tokio-macros/src/entry.rs index 52209a76b22..acdc2610f44 100644 --- a/tokio-macros/src/entry.rs +++ b/tokio-macros/src/entry.rs @@ -75,7 +75,7 @@ struct Configuration { start_paused: Option<(bool, Span)>, is_test: bool, crate_name: Option, - unhandled_panic: Option, + unhandled_panic: Option<(UnhandledPanic, Span)>, } impl Configuration { @@ -161,7 +161,7 @@ impl Configuration { let unhandled_panic = parse_string(unhandled_panic, span, "unhandled_panic")?; let unhandled_panic = UnhandledPanic::from_str(&unhandled_panic).map_err(|err| syn::Error::new(span, err))?; - self.unhandled_panic = Some(unhandled_panic); + self.unhandled_panic = Some((unhandled_panic, span)); Ok(()) } @@ -211,12 +211,24 @@ impl Configuration { (_, None) => None, }; + let unhandled_panic = match (flavor, self.unhandled_panic) { + (F::Threaded, Some((_, unhandled_panic_span))) => { + let msg = format!( + "The `unhandled_panic` option requires the `current_thread` runtime flavor. Use `#[{}(flavor = \"current_thread\")]`", + self.macro_name(), + ); + return Err(syn::Error::new(unhandled_panic_span, msg)); + } + (F::CurrentThread, Some((unhandled_panic, _))) => Some(unhandled_panic), + (_, None) => None, + }; + Ok(FinalConfig { crate_name: self.crate_name.clone(), - unhandled_panic: self.unhandled_panic, flavor, worker_threads, start_paused, + unhandled_panic, }) } } diff --git a/tokio-macros/src/lib.rs b/tokio-macros/src/lib.rs index 0ef745c9396..4b7072943cf 100644 --- a/tokio-macros/src/lib.rs +++ b/tokio-macros/src/lib.rs @@ -208,6 +208,8 @@ use proc_macro::TokenStream; /// Available options are `shutdown_runtime` and `ignore`. For more details, see /// [`Builder::unhandled_panic`]. /// +/// This option is only compatible with the `current_thread` runtime. +/// /// ```ignore /// #[tokio::main(flavor = "current_thread", unhandled_panic = "shutdown_runtime")] /// async fn main() { @@ -467,6 +469,8 @@ pub fn main_rt(args: TokenStream, item: TokenStream) -> TokenStream { /// Available options are `shutdown_runtime` and `ignore`. For more details, see /// [`Builder::unhandled_panic`]. /// +/// This option is only compatible with the `current_thread` runtime. +/// /// ```ignore /// #[tokio::test(flavor = "current_thread", unhandled_panic = "shutdown_runtime")] /// async fn my_test() {