Skip to content

Suggest async {} for async || {} #76011

@jyn514

Description

@jyn514

This code (playground):

fn main() {
    let my_future = async || {
        1
    };
}

prints out

error[E0658]: async closures are unstable
 --> src/main.rs:2:21
  |
2 |     let my_future = async || {
  |                     ^^^^^
  |
  = note: see issue #62290 <https://github.com/rust-lang/rust/issues/62290> for more information

error: aborting due to previous error

This is pretty unhelpful for beginners - normally this happens when they try to use await in a closure, which tells them they need async:

error[E0728]: `await` is only allowed inside `async` functions and blocks
 --> src/main.rs:3:9
  |
2 |     let my_future = || {
  |                     -- this is not `async`
3 |         1.await
  |         ^^^^^^^ only allowed inside `async` functions and blocks

But when they do they get an error about async being unstable, which they have no idea how to solve. The fix is to remove the pipe symbols:

async { }

However, this requires knowledge that async blocks desugar internally into closures, which is not at all clear from the syntax. It would be better for the compiler to suggest it:

error[E0658]: async closures are unstable
 --> src/main.rs:2:21
  |
2 |     let my_future = async || {
  |                     ^^^^^^^^^^ help: to use an async block, remove the `||`: `async {`
  |
  = note: see issue #62290 <https://github.com/rust-lang/rust/issues/62290> for more information

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions