Skip to content

async_fn_in_trait: lifetime of implemented async trait fn wrongly evaluates to static #104678

Closed
@ufoscout

Description

@ufoscout

In the following code, the trait compiles but not the struct implementing it:

#![feature(async_fn_in_trait)]

use std::future::Future;
pub trait Pool {
    type Conn;

    async fn async_callback<'a,
        F: FnOnce(&'a Self::Conn) -> Fut,
        Fut: Future<Output = ()>,
    >(&'a self, callback: F) -> ();
}

pub struct PoolImpl;
pub struct ConnImpl;

impl Pool for PoolImpl {
    type Conn = ConnImpl;

    async fn async_callback<'a,
        F: FnOnce(&'a Self::Conn) -> Fut,
        Fut: Future<Output = ()>,
    >(&'a self, callback: F) -> () {
        todo!()
    }
}

I expected to see this happen:
The code should compile and the expected lifetime of Self::Conn should be 'a.

Instead, this happened:
The compilation fails. The error says that the expected lifetime of Self::Conn in the struct is 'static even if it is declared as 'a in the trait:

error[E0308]: mismatched types
  --> src/lib.rs:19:5
   |
19 | /     async fn async_callback<'a,
20 | |         F: FnOnce(&'a Self::Conn) -> Fut,
21 | |         Fut: Future<Output = ()>,
22 | |     >(&'a self, callback: F) -> () {
   | |__________________________________^ lifetime mismatch
   |
   = note: expected trait `FnOnce<(&'static ConnImpl,)>`
              found trait `FnOnce<(&'a ConnImpl,)>`
note: the lifetime `'a` as defined here...
  --> src/lib.rs:19:29
   |
19 |     async fn async_callback<'a,
   |                             ^^
   = note: ...does not necessarily outlive the static lifetime

Please note that the code compiles if you remove the async modifier from both the trait and the struct.

Meta

rustc --version --verbose:

rustc 1.67.0-nightly (83356b78c 2022-11-17)
binary: rustc
commit-hash: 83356b78c4ff3e7d84e977aa6143793545967301
commit-date: 2022-11-17
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions