Skip to content

Wrong diagnostic when calling #[tokio::main] before a non-main function #144129

@0xdidu

Description

@0xdidu

Code

#[tokio::main]
async fn test(){
    
}

#[tokio::main]
async fn main() {
    test().await;
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0277]: `()` is not a future
 --> src/main.rs:8:12
  |
8 |     test().await;
  |     ------ ^^^^^ `()` is not a future
  |     |
  |     this call returns `()`
  |
  = help: the trait `Future` is not implemented for `()`
  = note: () must be a future or must implement `IntoFuture` to be awaited
  = note: required for `()` to implement `IntoFuture`
help: remove the `.await`
  |
8 -     test().await;
8 +     test();
  |
help: alternatively, consider making `fn test` asynchronous
  |
2 | async async fn test(){
  |       +++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

The error report should point to the fact that #[tokio::main] should not be added before the first function.

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-pc-windows-msvc
release: 1.88.0
LLVM version: 20.1.5

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions