Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ufoscout opened this issue Nov 21, 2022 · 3 comments · Fixed by #105692
Closed
Labels
C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@ufoscout
Copy link

ufoscout commented Nov 21, 2022

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
@ufoscout ufoscout added the C-bug Category: This is a bug. label Nov 21, 2022
@compiler-errors
Copy link
Member

compiler-errors commented Nov 21, 2022

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.

@ufoscout ufoscout 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 Nov 22, 2022
@ufoscout
Copy link
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
Copy link
Contributor

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

@cjgillot cjgillot added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Nov 27, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this issue Dec 14, 2022
Signed-off-by: Yuki Okushi <jtitor@2k36.org>
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 14, 2022
…-errors

Add regression test for rust-lang#104678

Closes rust-lang#104678
r? `@compiler-errors`

Signed-off-by: Yuki Okushi <jtitor@2k36.org>
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 14, 2022
…-errors

Add regression test for rust-lang#104678

Closes rust-lang#104678
r? ``@compiler-errors``

Signed-off-by: Yuki Okushi <jtitor@2k36.org>
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 15, 2022
…-errors

Add regression test for rust-lang#104678

Closes rust-lang#104678
r? ```@compiler-errors```

Signed-off-by: Yuki Okushi <jtitor@2k36.org>
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 15, 2022
…-errors

Add regression test for rust-lang#104678

Closes rust-lang#104678
r? ````@compiler-errors````

Signed-off-by: Yuki Okushi <jtitor@2k36.org>
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 15, 2022
…iaskrgr

Rollup of 11 pull requests

Successful merges:

 - rust-lang#104592 (Ensure async trait impls are async (or otherwise return an opaque type))
 - rust-lang#105623 (Fix `-Z print-type-sizes` for generators with discriminant field ordered first)
 - rust-lang#105627 (Auto traits in `dyn Trait + Auto` are suggestable)
 - rust-lang#105633 (Make `report_projection_error` more `Term` agnostic)
 - rust-lang#105683 (Various cleanups to dest prop)
 - rust-lang#105692 (Add regression test for rust-lang#104678)
 - rust-lang#105707 (rustdoc: remove unnecessary CSS `kbd { cursor: default }`)
 - rust-lang#105715 (Do not mention long types in E0599 label)
 - rust-lang#105722 (more clippy::complexity fixes)
 - rust-lang#105724 (rustdoc: remove no-op CSS `.scrape-example .src-line-numbers { margin: 0 }`)
 - rust-lang#105730 (rustdoc: remove no-op CSS `.item-info:before { color }`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in 8111cc4 Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants