Skip to content

Behavior of async fn and return position impl Future differs #109572

Closed as not planned
@xiaoas

Description

@xiaoas

I tried this code:

use std::future::Future;

async fn call() {
    outer(inner).await;
}
async fn outer<F, Fut>(handler: F)
where
    F: Fn(&()) -> Fut,
    Fut: Future<Output = ()>,
{
    handler(&()).await;
}
async fn inner(_: &()) {
}

I expected to behave identically when I change the function inner to:

fn inner(_: &()) -> impl Future<Output = ()> {
    async {}
}

According to async-lifetimes, that is.

Instead, the -> impl Future version compiles fine, and the async function version give this error on current stable(1.68.1) & beta(1.69.0-beta.3):

error[E0308]: mismatched types
 --> src/lib.rs:4:5
  |
4 |     outer(inner).await;
  |     ^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected trait `for<'a> <for<'a> fn(&'a ()) -> impl Future<Output = ()> {inner} as FnOnce<(&'a (),)>>`
             found trait `for<'a> <for<'a> fn(&'a ()) -> impl Future<Output = ()> {inner} as FnOnce<(&'a (),)>>`
note: the lifetime requirement is introduced here
 --> src/lib.rs:8:19
  |
8 |     F: Fn(&()) -> Fut,
  |                   ^^^

For more information about this error, try `rustc --explain E0308`.

And this message on nightly:

error[E0308]: mismatched types
 --> src/lib.rs:4:5
  |
4 |     outer(inner).await;
  |     ^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected opaque type `impl for<'a> Future<Output = ()>`
             found opaque type `impl Future<Output = ()>`
  = help: consider `await`ing on both `Future`s
  = note: distinct uses of `impl Trait` result in different opaque types
note: the lifetime requirement is introduced here
 --> src/lib.rs:8:19
  |
8 |     F: Fn(&()) -> Fut,
  |                   ^^^

For more information about this error, try `rustc --explain E0308`.

Meta

rustc --version --verbose:
I tried stable, beta and nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions