Skip to content

Commit

Permalink
macros: always emit return statement (#4636)
Browse files Browse the repository at this point in the history
Fixes #4635
  • Loading branch information
taiki-e committed Apr 27, 2022
1 parent 4818343 commit fa665b9
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 67 deletions.
2 changes: 1 addition & 1 deletion tests-build/tests/fail/macros_core_no_default.stderr
@@ -1,5 +1,5 @@
error: The default runtime flavor is `multi_thread`, but the `rt-multi-thread` feature is disabled.
--> $DIR/macros_core_no_default.rs:3:1
--> tests/fail/macros_core_no_default.rs:3:1
|
3 | #[tokio::main]
| ^^^^^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions tests-build/tests/fail/macros_dead_code.stderr
@@ -1,11 +1,11 @@
error: function is never used: `f`
--> $DIR/macros_dead_code.rs:6:10
--> tests/fail/macros_dead_code.rs:6:10
|
6 | async fn f() {}
| ^
|
note: the lint level is defined here
--> $DIR/macros_dead_code.rs:1:9
--> tests/fail/macros_dead_code.rs:1:9
|
1 | #![deny(dead_code)]
| ^^^^^^^^^
2 changes: 2 additions & 0 deletions tests-build/tests/fail/macros_invalid_input.rs
@@ -1,3 +1,5 @@
#![deny(duplicate_macro_attributes)]

use tests_build::tokio;

#[tokio::main]
Expand Down
74 changes: 39 additions & 35 deletions tests-build/tests/fail/macros_invalid_input.stderr
@@ -1,97 +1,101 @@
error: the `async` keyword is missing from the function declaration
--> $DIR/macros_invalid_input.rs:4:1
--> tests/fail/macros_invalid_input.rs:6:1
|
4 | fn main_is_not_async() {}
6 | fn main_is_not_async() {}
| ^^

error: Unknown attribute foo is specified; expected one of: `flavor`, `worker_threads`, `start_paused`, `crate`
--> $DIR/macros_invalid_input.rs:6:15
--> tests/fail/macros_invalid_input.rs:8:15
|
6 | #[tokio::main(foo)]
8 | #[tokio::main(foo)]
| ^^^

error: Must have specified ident
--> $DIR/macros_invalid_input.rs:9:15
|
9 | #[tokio::main(threadpool::bar)]
| ^^^^^^^^^^^^^^^
--> tests/fail/macros_invalid_input.rs:11:15
|
11 | #[tokio::main(threadpool::bar)]
| ^^^^^^^^^^^^^^^

error: the `async` keyword is missing from the function declaration
--> $DIR/macros_invalid_input.rs:13:1
--> tests/fail/macros_invalid_input.rs:15:1
|
13 | fn test_is_not_async() {}
15 | fn test_is_not_async() {}
| ^^

error: Unknown attribute foo is specified; expected one of: `flavor`, `worker_threads`, `start_paused`, `crate`
--> $DIR/macros_invalid_input.rs:15:15
--> tests/fail/macros_invalid_input.rs:17:15
|
15 | #[tokio::test(foo)]
17 | #[tokio::test(foo)]
| ^^^

error: Unknown attribute foo is specified; expected one of: `flavor`, `worker_threads`, `start_paused`, `crate`
--> $DIR/macros_invalid_input.rs:18:15
--> tests/fail/macros_invalid_input.rs:20:15
|
18 | #[tokio::test(foo = 123)]
20 | #[tokio::test(foo = 123)]
| ^^^^^^^^^

error: Failed to parse value of `flavor` as string.
--> $DIR/macros_invalid_input.rs:21:24
--> tests/fail/macros_invalid_input.rs:23:24
|
21 | #[tokio::test(flavor = 123)]
23 | #[tokio::test(flavor = 123)]
| ^^^

error: No such runtime flavor `foo`. The runtime flavors are `current_thread` and `multi_thread`.
--> $DIR/macros_invalid_input.rs:24:24
--> tests/fail/macros_invalid_input.rs:26:24
|
24 | #[tokio::test(flavor = "foo")]
26 | #[tokio::test(flavor = "foo")]
| ^^^^^

error: The `start_paused` option requires the `current_thread` runtime flavor. Use `#[tokio::test(flavor = "current_thread")]`
--> $DIR/macros_invalid_input.rs:27:55
--> tests/fail/macros_invalid_input.rs:29:55
|
27 | #[tokio::test(flavor = "multi_thread", start_paused = false)]
29 | #[tokio::test(flavor = "multi_thread", start_paused = false)]
| ^^^^^

error: Failed to parse value of `worker_threads` as integer.
--> $DIR/macros_invalid_input.rs:30:57
--> tests/fail/macros_invalid_input.rs:32:57
|
30 | #[tokio::test(flavor = "multi_thread", worker_threads = "foo")]
32 | #[tokio::test(flavor = "multi_thread", worker_threads = "foo")]
| ^^^^^

error: The `worker_threads` option requires the `multi_thread` runtime flavor. Use `#[tokio::test(flavor = "multi_thread")]`
--> $DIR/macros_invalid_input.rs:33:59
--> tests/fail/macros_invalid_input.rs:35:59
|
33 | #[tokio::test(flavor = "current_thread", worker_threads = 4)]
35 | #[tokio::test(flavor = "current_thread", worker_threads = 4)]
| ^

error: Failed to parse value of `crate` as ident.
--> $DIR/macros_invalid_input.rs:36:23
--> tests/fail/macros_invalid_input.rs:38:23
|
36 | #[tokio::test(crate = 456)]
38 | #[tokio::test(crate = 456)]
| ^^^

error: Failed to parse value of `crate` as ident: "456"
--> $DIR/macros_invalid_input.rs:39:23
--> tests/fail/macros_invalid_input.rs:41:23
|
39 | #[tokio::test(crate = "456")]
41 | #[tokio::test(crate = "456")]
| ^^^^^

error: Failed to parse value of `crate` as ident: "abc::edf"
--> $DIR/macros_invalid_input.rs:42:23
--> tests/fail/macros_invalid_input.rs:44:23
|
42 | #[tokio::test(crate = "abc::edf")]
44 | #[tokio::test(crate = "abc::edf")]
| ^^^^^^^^^^

error: second test attribute is supplied
--> $DIR/macros_invalid_input.rs:46:1
--> tests/fail/macros_invalid_input.rs:48:1
|
46 | #[test]
48 | #[test]
| ^^^^^^^

error: duplicated attribute
--> $DIR/macros_invalid_input.rs:46:1
--> tests/fail/macros_invalid_input.rs:48:1
|
46 | #[test]
48 | #[test]
| ^^^^^^^
|
= note: `-D duplicate-macro-attributes` implied by `-D warnings`
note: the lint level is defined here
--> tests/fail/macros_invalid_input.rs:1:9
|
1 | #![deny(duplicate_macro_attributes)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
9 changes: 9 additions & 0 deletions tests-build/tests/fail/macros_type_mismatch.rs
Expand Up @@ -23,4 +23,13 @@ async fn extra_semicolon() -> Result<(), ()> {
Ok(());
}

// https://github.com/tokio-rs/tokio/issues/4635
#[allow(redundant_semicolons)]
#[rustfmt::skip]
#[tokio::main]
async fn issue_4635() {
return 1;
;
}

fn main() {}
26 changes: 17 additions & 9 deletions tests-build/tests/fail/macros_type_mismatch.stderr
@@ -1,18 +1,16 @@
error[E0308]: mismatched types
--> $DIR/macros_type_mismatch.rs:5:5
--> tests/fail/macros_type_mismatch.rs:5:5
|
4 | async fn missing_semicolon_or_return_type() {
| - possibly return type missing here?
5 | Ok(())
| ^^^^^^- help: consider using a semicolon here: `;`
| |
| expected `()`, found enum `Result`
| ^^^^^^ expected `()`, found enum `Result`
|
= note: expected unit type `()`
found enum `Result<(), _>`

error[E0308]: mismatched types
--> $DIR/macros_type_mismatch.rs:10:5
--> tests/fail/macros_type_mismatch.rs:10:5
|
9 | async fn missing_return_type() {
| - possibly return type missing here?
Expand All @@ -23,7 +21,7 @@ error[E0308]: mismatched types
found enum `Result<(), _>`

error[E0308]: mismatched types
--> $DIR/macros_type_mismatch.rs:23:5
--> tests/fail/macros_type_mismatch.rs:23:5
|
14 | async fn extra_semicolon() -> Result<(), ()> {
| -------------- expected `Result<(), ()>` because of return type
Expand All @@ -33,8 +31,18 @@ error[E0308]: mismatched types
|
= note: expected enum `Result<(), ()>`
found unit type `()`
help: try adding an expression at the end of the block
help: try wrapping the expression in a variant of `Result`
|
23 ~ Ok(());;
24 + Ok(())
23 | Ok(Ok(());)
| +++ +
23 | Err(Ok(());)
| ++++ +

error[E0308]: mismatched types
--> tests/fail/macros_type_mismatch.rs:32:5
|
30 | async fn issue_4635() {
| - possibly return type missing here?
31 | return 1;
32 | ;
| ^ expected `()`, found integer
28 changes: 8 additions & 20 deletions tokio-macros/src/entry.rs
Expand Up @@ -383,29 +383,17 @@ 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 (tail_return, tail_semicolon) = match body.stmts.last() {
Some(syn::Stmt::Semi(syn::Expr::Return(_), _)) => (quote! { return }, quote! { ; }),
Some(syn::Stmt::Semi(..)) | Some(syn::Stmt::Local(..)) | None => {
match &input.sig.output {
syn::ReturnType::Type(_, ty) if matches!(&**ty, syn::Type::Tuple(ty) if ty.elems.is_empty()) =>
{
(quote! {}, quote! { ; }) // unit
}
syn::ReturnType::Default => (quote! {}, quote! { ; }), // unit
syn::ReturnType::Type(..) => (quote! {}, quote! {}), // ! or another
}
}
_ => (quote! {}, quote! {}),
};
input.block = syn::parse2(quote_spanned! {last_stmt_end_span=>
{
let body = async #body;
#[allow(clippy::expect_used)]
#tail_return #rt
.enable_all()
.build()
.expect("Failed building the Runtime")
.block_on(body)#tail_semicolon
#[allow(clippy::expect_used, clippy::diverging_sub_expression)]
{
return #rt
.enable_all()
.build()
.expect("Failed building the Runtime")
.block_on(body);
}
}
})
.expect("Parsing failure");
Expand Down

0 comments on commit fa665b9

Please sign in to comment.