Skip to content

Commit d770695

Browse files
committed
Improve diagnostics for mismatched type in async main
Previously, we wrapped the body of the `async main` function in an `async` block, which we passed to `block_on`. However, `block_on` is generic, so an incorrect return type ends up creating a diagnostic pointing a `block_on`, not the user's code. Since the call to `block_on` is generated by the `#[tokio::main]` macro, it ended up with a span of the `#[tokio::main]` attribute, producing a confusing diagnostic. We now wrap the body of the `async main` function in a new `async main_inner` function. This asserts a return type of `()` earlier on, producing a diagnostic. Given this code: ```rust #[tokio::main] async fn main() { Ok(()) } ``` We currently produce the error: ``` error[E0308]: mismatched types --> src/main.rs:1:1 | 1 | #[tokio::main] | ^^^^^^^^^^^^^^- help: try adding a semicolon: `;` | | | expected `()`, found enum `std::result::Result` | = note: expected unit type `()` found enum `std::result::Result<(), _>` = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info) ``` With this PR, we produce: ``` error[E0308]: mismatched types --> src/main.rs:3:5 | 3 | Ok(()) | ^^^^^^- help: try adding a semicolon: `;` | | | expected `()`, found enum `std::result::Result` | = note: expected unit type `()` found enum `std::result::Result<(), _>` ```
1 parent ce173fd commit d770695

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

tokio-macros/src/entry.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,15 @@ fn parse_knobs(
244244
#header
245245
#(#attrs)*
246246
#vis #sig {
247+
// Assert a return type of `()`,
248+
// which produces a better diagnostic
249+
async fn main_inner() -> () #body
250+
247251
#rt
248252
.enable_all()
249253
.build()
250254
.unwrap()
251-
.block_on(async { #body })
255+
.block_on(main_inner())
252256
}
253257
};
254258

0 commit comments

Comments
 (0)