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

impl Trait does not capture lifetime of generic argument #65805

Open
jonhoo opened this issue Oct 25, 2019 · 0 comments

Comments

@jonhoo
Copy link
Contributor

@jonhoo jonhoo commented Oct 25, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.