Skip to content

Commit fa665b9

Browse files
authored
macros: always emit return statement (#4636)
Fixes #4635
1 parent 4818343 commit fa665b9

File tree

7 files changed

+78
-67
lines changed

7 files changed

+78
-67
lines changed

tests-build/tests/fail/macros_core_no_default.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: The default runtime flavor is `multi_thread`, but the `rt-multi-thread` feature is disabled.
2-
--> $DIR/macros_core_no_default.rs:3:1
2+
--> tests/fail/macros_core_no_default.rs:3:1
33
|
44
3 | #[tokio::main]
55
| ^^^^^^^^^^^^^^
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: function is never used: `f`
2-
--> $DIR/macros_dead_code.rs:6:10
2+
--> tests/fail/macros_dead_code.rs:6:10
33
|
44
6 | async fn f() {}
55
| ^
66
|
77
note: the lint level is defined here
8-
--> $DIR/macros_dead_code.rs:1:9
8+
--> tests/fail/macros_dead_code.rs:1:9
99
|
1010
1 | #![deny(dead_code)]
1111
| ^^^^^^^^^

tests-build/tests/fail/macros_invalid_input.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(duplicate_macro_attributes)]
2+
13
use tests_build::tokio;
24

35
#[tokio::main]
Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,101 @@
11
error: the `async` keyword is missing from the function declaration
2-
--> $DIR/macros_invalid_input.rs:4:1
2+
--> tests/fail/macros_invalid_input.rs:6:1
33
|
4-
4 | fn main_is_not_async() {}
4+
6 | fn main_is_not_async() {}
55
| ^^
66

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

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

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

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

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

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

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

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

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

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

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

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

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

8585
error: second test attribute is supplied
86-
--> $DIR/macros_invalid_input.rs:46:1
86+
--> tests/fail/macros_invalid_input.rs:48:1
8787
|
88-
46 | #[test]
88+
48 | #[test]
8989
| ^^^^^^^
9090

9191
error: duplicated attribute
92-
--> $DIR/macros_invalid_input.rs:46:1
92+
--> tests/fail/macros_invalid_input.rs:48:1
9393
|
94-
46 | #[test]
94+
48 | #[test]
9595
| ^^^^^^^
9696
|
97-
= note: `-D duplicate-macro-attributes` implied by `-D warnings`
97+
note: the lint level is defined here
98+
--> tests/fail/macros_invalid_input.rs:1:9
99+
|
100+
1 | #![deny(duplicate_macro_attributes)]
101+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

tests-build/tests/fail/macros_type_mismatch.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,13 @@ async fn extra_semicolon() -> Result<(), ()> {
2323
Ok(());
2424
}
2525

26+
// https://github.com/tokio-rs/tokio/issues/4635
27+
#[allow(redundant_semicolons)]
28+
#[rustfmt::skip]
29+
#[tokio::main]
30+
async fn issue_4635() {
31+
return 1;
32+
;
33+
}
34+
2635
fn main() {}
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
error[E0308]: mismatched types
2-
--> $DIR/macros_type_mismatch.rs:5:5
2+
--> tests/fail/macros_type_mismatch.rs:5:5
33
|
44
4 | async fn missing_semicolon_or_return_type() {
55
| - possibly return type missing here?
66
5 | Ok(())
7-
| ^^^^^^- help: consider using a semicolon here: `;`
8-
| |
9-
| expected `()`, found enum `Result`
7+
| ^^^^^^ expected `()`, found enum `Result`
108
|
119
= note: expected unit type `()`
1210
found enum `Result<(), _>`
1311

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

2523
error[E0308]: mismatched types
26-
--> $DIR/macros_type_mismatch.rs:23:5
24+
--> tests/fail/macros_type_mismatch.rs:23:5
2725
|
2826
14 | async fn extra_semicolon() -> Result<(), ()> {
2927
| -------------- expected `Result<(), ()>` because of return type
@@ -33,8 +31,18 @@ error[E0308]: mismatched types
3331
|
3432
= note: expected enum `Result<(), ()>`
3533
found unit type `()`
36-
help: try adding an expression at the end of the block
34+
help: try wrapping the expression in a variant of `Result`
3735
|
38-
23 ~ Ok(());;
39-
24 + Ok(())
36+
23 | Ok(Ok(());)
37+
| +++ +
38+
23 | Err(Ok(());)
39+
| ++++ +
40+
41+
error[E0308]: mismatched types
42+
--> tests/fail/macros_type_mismatch.rs:32:5
4043
|
44+
30 | async fn issue_4635() {
45+
| - possibly return type missing here?
46+
31 | return 1;
47+
32 | ;
48+
| ^ expected `()`, found integer

tokio-macros/src/entry.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -383,29 +383,17 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To
383383

384384
let body = &input.block;
385385
let brace_token = input.block.brace_token;
386-
let (tail_return, tail_semicolon) = match body.stmts.last() {
387-
Some(syn::Stmt::Semi(syn::Expr::Return(_), _)) => (quote! { return }, quote! { ; }),
388-
Some(syn::Stmt::Semi(..)) | Some(syn::Stmt::Local(..)) | None => {
389-
match &input.sig.output {
390-
syn::ReturnType::Type(_, ty) if matches!(&**ty, syn::Type::Tuple(ty) if ty.elems.is_empty()) =>
391-
{
392-
(quote! {}, quote! { ; }) // unit
393-
}
394-
syn::ReturnType::Default => (quote! {}, quote! { ; }), // unit
395-
syn::ReturnType::Type(..) => (quote! {}, quote! {}), // ! or another
396-
}
397-
}
398-
_ => (quote! {}, quote! {}),
399-
};
400386
input.block = syn::parse2(quote_spanned! {last_stmt_end_span=>
401387
{
402388
let body = async #body;
403-
#[allow(clippy::expect_used)]
404-
#tail_return #rt
405-
.enable_all()
406-
.build()
407-
.expect("Failed building the Runtime")
408-
.block_on(body)#tail_semicolon
389+
#[allow(clippy::expect_used, clippy::diverging_sub_expression)]
390+
{
391+
return #rt
392+
.enable_all()
393+
.build()
394+
.expect("Failed building the Runtime")
395+
.block_on(body);
396+
}
409397
}
410398
})
411399
.expect("Parsing failure");

0 commit comments

Comments
 (0)