Skip to content

Implementation of FnOnce is not general enough for Flatten iterator of futures outliving an .await point #98380

Open
@Veetaha

Description

@Veetaha

I tried this code:

let _: &(dyn Future<Output = ()> + Send) = &async {
    let _drop_after_await = std::iter::empty()
        .map(|_: &()| [async {}])
        .flatten();

    async {}.await;
};

Playground link

I expected that the code should compile without errors.

However, it produces a compile error:

error: implementation of `FnOnce` is not general enough
  --> src/main.rs:4:48
   |
4  |       let _: &(dyn Future<Output = ()> + Send) = &async {
   |  ________________________________________________^
5  | |         let _drop_after_await = std::iter::empty()
6  | |             .map(|_: &()| [async {}])
7  | |             .flatten();
8  | |
9  | |         async {}.await;
10 | |     };
   | |_____^ implementation of `FnOnce` is not general enough
   |
   = note: closure with signature `fn(&'0 ()) -> [impl Future<Output = ()>; 1]` must implement `FnOnce<(&'1 (),)>`, for any two lifetimes `'0` and `'1`...
   = note: ...but it actually implements `FnOnce<(&(),)>`

The bug doesn't reproduce if we remove the Send requirement or .flatten() combinator 🤔.

Meta

rustc -V:

rustc 1.61.0 (fe5b13d68 2022-05-18)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.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.WG-asyncWorking group: Async & await

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions