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

Activity

compiler-errors

compiler-errors commented on Nov 21, 2022

@compiler-errors
Member

This issue title is not related to the code that was provided -- did you copy this issue title from #103352 and forget to reword it?

Anyways, this looks like a duplicate to #102681.

changed the title [-]async_fn_in_trait: return type of method impl is not checked in default bodies[/-] [+]async_fn_in_trait: lifetime of implemented async trait fn wrongly evaluates to static[/+] on Nov 22, 2022
ufoscout

ufoscout commented on Nov 22, 2022

@ufoscout
Author

@compiler-errors
sorry, I did something wrong with the issue title.

Anyways, this looks like a duplicate to #102681.

I reported this issue because I considered it different than #102681, but you know it better than me, so I am not sure.

cjgillot

cjgillot commented on Nov 27, 2022

@cjgillot
Contributor

The code is now accepted, can be closed with a test.

added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
on Nov 27, 2022
added a commit that references this issue on Dec 14, 2022
added a commit that references this issue on Dec 14, 2022

Rollup merge of rust-lang#105692 - JohnTitor:issue-104678, r=compiler…

1c35840
added a commit that references this issue on Dec 14, 2022

Rollup merge of rust-lang#105692 - JohnTitor:issue-104678, r=compiler…

e657bbc
added a commit that references this issue on Dec 15, 2022

Rollup merge of rust-lang#105692 - JohnTitor:issue-104678, r=compiler…

111ef5f

7 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @ufoscout@cjgillot@compiler-errors

      Issue actions

        async_fn_in_trait: lifetime of implemented async trait fn wrongly evaluates to static · Issue #104678 · rust-lang/rust