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

Rust incorrectly assumes an async closure is a coroutine #120886

Closed
BigBadE opened this issue Feb 10, 2024 · 2 comments · Fixed by #120896
Closed

Rust incorrectly assumes an async closure is a coroutine #120886

BigBadE opened this issue Feb 10, 2024 · 2 comments · Fixed by #120896
Assignees
Labels
C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@BigBadE
Copy link

BigBadE commented Feb 10, 2024

I tried this code:

#![feature(async_closure)]

use std::future::Future;

fn main() {
    let example = async move || {

    };
    test(example);
}

fn test<T: Future<Output = ()>, F: Fn() -> T>(given: F) {}

I expected to see this happen: example is of type F, the behavior of Rust prior to adding coroutines

Instead, this happened:

error[E0277]: expected a `Fn()` closure, found `{coroutine-closure@src\main.rs:6:19: 6:32}`         
  --> src\main.rs:9:10                                                                              
   |                                                                                                
9  |     test(example);                                                                             
   |     ---- ^^^^^^^ expected an `Fn()` closure, found `{coroutine-closure@src\main.rs:6:19: 6:32}`
   |     |                                                                                          
   |     required by a bound introduced by this call                                                
   |                                                                                                
   = help: the trait `Fn<()>` is not implemented for `{coroutine-closure@src\main.rs:6:19: 6:32}`
   = note: wrap the `{coroutine-closure@src\main.rs:6:19: 6:32}` in a closure with no arguments: `|| { /* code */ }`

Meta

binary: rustc
commit-hash: d44e3b95cb9d410d89cb8ab3233906a33f43756a
commit-date: 2024-02-09
host: x86_64-pc-windows-msvc
release: 1.78.0-nightly
LLVM version: 17.0.6

This error started happening with the addition of coroutines, the code works on older nightly versions (don't know exactly when it started). My guess is Rust incorrectly converts all async closures into coroutines.

@BigBadE BigBadE added the C-bug Category: This is a bug. label Feb 10, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 10, 2024
@compiler-errors
Copy link
Member

It's a "coroutine-closure", which is a new thing. We can be more specific when we know what the desugaring is, though.

@compiler-errors
Copy link
Member

For the record, this specific error is fixed by #120712.

@compiler-errors compiler-errors self-assigned this Feb 10, 2024
@saethlin saethlin added T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 11, 2024
@bors bors closed this as completed in 870435b Feb 11, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 11, 2024
Rollup merge of rust-lang#120896 - compiler-errors:coro-closure-kind, r=oli-obk

Print kind of coroutine closure

Make sure that we print "async closure" when we have an async closure, rather than calling it generically a ["coroutine-closure"](rust-lang#120361).

Fixes rust-lang#120886

r? oli-obk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants