Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnostics request: suggest a closure with an async block when an async closure is encountered #71686

Closed
jebrosen opened this issue Apr 29, 2020 · 2 comments
Labels
A-async-await Area: Async & Await A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-enhancement Category: An issue proposing an enhancement or a PR with one. F-async_closure `#![feature(async_closure)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jebrosen
Copy link
Contributor

Consider the following code (playground):

use std::future::Future;

async fn wants_closure_returning_future<F, Fut, R>(f: F) -> R
where
    F: FnOnce() -> Fut,
    Fut: Future<Output=R>,
{
    f().await
}

fn main() {
    // error[E0658]: async closures are unstable
    let fut1 = wants_closure_returning_future(async || 3);
    
    // Fine, and what should have been used anyway
    let fut2 = wants_closure_returning_future(|| async { 3 });
}

With the following output:

error[E0658]: async closures are unstable
  --> src/main.rs:13:47
   |
13 |     let fut1 = wants_closure_returning_future(async || 3);
   |                                               ^^^^^
   |
   = note: see issue #62290 <https://github.com/rust-lang/rust/issues/62290> for more information

In many cases moving the async keyword fixes the error and is "good enough" despite the differences in semantics, so it would be nice if the compiler could suggest this as well:

  = help: try returning a future instead: `|| async { 3 }`

This is also easy to run into when using futures or stream combinators such as then, especially for someone who is not yet very familiar with async/await.

@Dylan-DPC-zz Dylan-DPC-zz added A-diagnostics Area: Messages for errors, warnings, and lints F-async_closure `#![feature(async_closure)]` labels Apr 29, 2020
@jonas-schievink jonas-schievink added A-async-await Area: Async & Await C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-closures Area: Closures (`|…| { … }`) labels Apr 29, 2020
@tmandry tmandry added the AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. label May 12, 2020
@Ugator
Copy link

Ugator commented Mar 10, 2021

Why does a compiler hint to not use an unstable feature depend on that unstable feature to be stabilized? It seems to me that the design pattern of "luring people into unstable" is questionable anyways, so when we can suggest an alternative that should have priority over hinting towards unstable.

@compiler-errors
Copy link
Member

I don't believe it makes sense to suggest not using async ||. They now materially behave differently after #120361.

@compiler-errors compiler-errors closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-enhancement Category: An issue proposing an enhancement or a PR with one. F-async_closure `#![feature(async_closure)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants