Skip to content

Rust incorrectly detects move'd async closure as FnOnce instead of Fn on nightly-2024-02-07 #120957

Closed as not planned
@BigBadE

Description

@BigBadE

I tried this code:

#![feature(async_closure)]

use std::future::Future;

fn main() {
    let variable = 12;
    let reference = &variable;
    let example = async move || {
        println!("{}", reference);
    };
    test(example);
}

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

}

I expected to see this happen: No compiler errors

Instead, this happened:

error[E0277]: expected a `Fn()` closure, found `{async closure@src\main.rs:8:19: 8:32}`
  --> src\main.rs:11:10
   |
11 |     test(example);
   |     ---- ^^^^^^^ expected an `Fn()` closure, found `{async closure@src\main.rs:8:19: 8:32}`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `Fn<()>` is not implemented for `{async closure@src\main.rs:8:19: 8:32}`
   = note: wrap the `{async closure@src\main.rs:8:19: 8:32}` in a closure with no arguments: `|| { /* code */ }`
   = note: `{async closure@src\main.rs:8:19: 8:32}` implements `FnOnce`, but it must implement `Fn`, which is more general

Meta

rustc --version --verbose:

rustc 1.78.0-nightly (1a648b397 2024-02-11)
binary: rustc
commit-hash: 1a648b397dedc98ada3dd3360f6d661ec2436c56
commit-date: 2024-02-11
host: x86_64-pc-windows-msvc
release: 1.78.0-nightly
LLVM version: 17.0.6

This bug was introduced between nightly-2024-02-06 (where the code builds correctly) and nightly-2024-02-27 (where the error happens), and is still present on nightly-2024-02-11 (the latest when this was released). This bug is likely related to #120886, which was fixed by #120712.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-closures`async || {}`C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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