Skip to content

impl Trait does not capture lifetime of generic argument #65805

@jonhoo

Description

@jonhoo

According to RFC 1951, impl Trait in return position should capture the lifetime of any associated generic parameters. However, in this code, that logic appears not to work (playground):

use std::future::Future;
use std::pin::Pin;

struct A;

impl A {
    fn a<T>(&self, t: T) -> impl Future<Output = ()> {
        let _ = t;
        async {}
    }
}

struct B(A);

impl B {
    fn works<'a, T: 'a>(
        self,
        t: T,
    ) -> impl Future<Output = (Self, impl Future<Output = ()> + 'a)> + 'a {
        async move {
            let f = Pin::from(Box::new(self.0.a(t)) as Box<dyn Future<Output = _>>);
            (self, f)
        }
    }

    fn doesnt_work<T>(self, t: T) -> impl Future<Output = (Self, impl Future<Output = ()>)> {
        async move {
            let f = Pin::from(Box::new(self.0.a(t)) as Box<dyn Future<Output = _>>);
            (self, f)
        }
    }
}

Specifically, the compiler gives two almost identical errors for the doesnt_work method:

error[E0310]: the parameter type `T` may not live long enough
  --> src/lib.rs:28:31
   |
26 |     fn doesnt_work<T>(self, t: T) -> impl Future<Output = (Self, impl Future<Output = ()>)> {
   |                    - help: consider adding an explicit lifetime bound `T: 'static`...
27 |         async move {
28 |             let f = Pin::from(Box::new(self.0.a(t)) as Box<dyn Future<Output = _>>);
   |                               ^^^^^^^^^^^^^^^^^^^^^
   |
note: ...so that the type `impl std::future::Future` will meet its required lifetime bounds
  --> src/lib.rs:28:31
   |
28 |             let f = Pin::from(Box::new(self.0.a(t)) as Box<dyn Future<Output = _>>);
   |                               ^^^^^^^^^^^^^^^^^^^^^

and

error[E0310]: the parameter type `T` may not live long enough
  --> src/lib.rs:28:31
   |
26 |     fn doesnt_work<T>(self, t: T) -> impl Future<Output = (Self, impl Future<Output = ()>)> {
   |                    - help: consider adding an explicit lifetime bound `T: 'static`...
27 |         async move {
28 |             let f = Pin::from(Box::new(self.0.a(t)) as Box<dyn Future<Output = _>>);
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: ...so that it can be closed over into an object
  --> src/lib.rs:28:31
   |
28 |             let f = Pin::from(Box::new(self.0.a(t)) as Box<dyn Future<Output = _>>);
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I was (perhaps mistakenly) under the impression that the semantics of impl Trait in return position should be as the explicit lifetimes on the works method indicate, but that doesn't seem to be the case? When the lifetimes are explicitly annotated, as in the works method, the method compiles just fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-lifetimesArea: Lifetimes / regionsA-type-systemArea: Type systemT-langRelevant to the language teamT-typesRelevant to the types 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