Skip to content

Unexpected lifetime bound in async trait method #104529

Closed
@fakedrake

Description

@fakedrake

I have this program

#![allow(incomplete_features)]
#![feature(async_fn_in_trait)]
use std::marker::PhantomData;

struct Y<'s,'e:'s>(PhantomData<(&'s (), &'e mut &'e ())>);
trait Tr<'e> {
    // Removing async works fine
    async fn f<'s>(&self, y: Y<'s, 'e>) -> Y<'s,'e>;
}

playground

That throws this strange compilation error on nightly

Compiling playground v0.0.1 (/playground)
error[[E0478]](https://doc.rust-lang.org/nightly/error-index.html#E0478): lifetime bound not satisfied
 --> src/lib.rs:8:44
  |
8 |     async fn f<'s>(&self, y: Y<'s, 'e>) -> Y<'s,'e>;
  |                                            ^^^^^^^^
  |
note: lifetime parameter instantiated with the lifetime `'e` as defined here
 --> src/lib.rs:6:10
  |
6 | trait Tr<'e> {
  |          ^^
note: but lifetime parameter must outlive the lifetime `'s` as defined here
 --> src/lib.rs:8:16
  |
8 |     async fn f<'s>(&self, y: Y<'s, 'e>) -> Y<'s,'e>;
  |                ^^

For more information about this error, try `rustc --explain E0478`.
error: could not compile `playground` due to previous error

If I remove async it works fine. Any idea why this happens?

Edit: this program compiles normally:

use std::marker::PhantomData;

struct Y<'s,'e:'s>(PhantomData<(&'s (), &'e mut &'e ())>);
struct A<'e>(PhantomData<&'e ()>);

impl<'e> A<'e> {
    async fn f<'s>(&self, y: Y<'s, 'e>) -> Y<'s,'e> {
        y
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    F-async_fn_in_traitStatic async fn in traitsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions