Lifetime ignored by async trait fn

I know that this feature is still unstable, anyway, why is the trait compiling here but not the struct that implements it?

#![feature(async_fn_in_trait)]
#![feature(return_position_impl_trait_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!()
    }
}

The compilation error says that the expected lifetime of Self::Conn is static even if it is declared as 'a in the trait:

| /     async fn async_callback<'a,
| |         F: FnOnce(&'a Self::Conn) -> Fut,
| |         Fut: Future<Output = ()>,
  |
| |         callback: F,
| |     ) -> () {
| |___________^ lifetime mismatch
|
= note: expected trait `FnOnce<(&'static ConnImpl,)>`
           found trait `FnOnce<(&'a ConnImpl,)>`

Is this a bug?

1 Like

Most probably. I'm guessing it's something weird about how the compiler assigns lifetimes or inference isn't implemented fully so the compiler decides to go with 'static.

1 Like

Is there a reason that async trait functions might require a 'static lifetime? or a reason why they shouldn't?... perhaps said differently, is there any documentation that describes the variance rules for async trait functions? (expanding on the Rust Reference docs on variance)

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.