mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-25 00:00:18 +02:00
macros: forward input arguments in #[tokio::test] (#3691)
Fixes #2388 Previously `#[tokio::test]` would error on functions that took arguments. That meant other attribute macros couldn't do further transformations on them. This changes that so arguments are forwarded as is. Whatever else might be included on the function is forwarded as well. For example return type, generics, etc. Worth noting that this is only for compatibility with other macros. `#[test]`s that take arguments will still fail to compile. A bit odd that [trybuild] tests don't fail `#[test]` functions with arguments which is why the new tests are run with `t.pass(...)`. They do actually fail if part of a real crate. [trybuild]: https://crates.io/crates/trybuild
This commit is contained in:
+15
-27
@@ -190,18 +190,11 @@ fn parse_knobs(
|
||||
is_test: bool,
|
||||
rt_multi_thread: bool,
|
||||
) -> Result<TokenStream, syn::Error> {
|
||||
let sig = &mut input.sig;
|
||||
let body = &input.block;
|
||||
let attrs = &input.attrs;
|
||||
let vis = input.vis;
|
||||
|
||||
if sig.asyncness.is_none() {
|
||||
if input.sig.asyncness.take().is_none() {
|
||||
let msg = "the `async` keyword is missing from the function declaration";
|
||||
return Err(syn::Error::new_spanned(sig.fn_token, msg));
|
||||
return Err(syn::Error::new_spanned(input.sig.fn_token, msg));
|
||||
}
|
||||
|
||||
sig.asyncness = None;
|
||||
|
||||
let mut config = Configuration::new(is_test, rt_multi_thread);
|
||||
let macro_name = config.macro_name();
|
||||
|
||||
@@ -300,20 +293,17 @@ fn parse_knobs(
|
||||
rt = quote! { #rt.start_paused(#v) };
|
||||
}
|
||||
|
||||
let header = {
|
||||
if is_test {
|
||||
quote! {
|
||||
#[::core::prelude::v1::test]
|
||||
}
|
||||
} else {
|
||||
quote! {}
|
||||
let header = if is_test {
|
||||
quote! {
|
||||
#[::core::prelude::v1::test]
|
||||
}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
let result = quote! {
|
||||
#header
|
||||
#(#attrs)*
|
||||
#vis #sig {
|
||||
let body = &input.block;
|
||||
input.block = syn::parse_quote! {
|
||||
{
|
||||
#rt
|
||||
.enable_all()
|
||||
.build()
|
||||
@@ -322,6 +312,11 @@ fn parse_knobs(
|
||||
}
|
||||
};
|
||||
|
||||
let result = quote! {
|
||||
#header
|
||||
#input
|
||||
};
|
||||
|
||||
Ok(result.into())
|
||||
}
|
||||
|
||||
@@ -353,12 +348,5 @@ pub(crate) fn test(args: TokenStream, item: TokenStream, rt_multi_thread: bool)
|
||||
}
|
||||
}
|
||||
|
||||
if !input.sig.inputs.is_empty() {
|
||||
let msg = "the test function cannot accept arguments";
|
||||
return syn::Error::new_spanned(&input.sig.inputs, msg)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
}
|
||||
|
||||
parse_knobs(input, args, true, rt_multi_thread).unwrap_or_else(|e| e.to_compile_error().into())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user