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
Open

impl Trait does not capture lifetime of generic argument #65805

jonhoo opened this issue Oct 25, 2019 · 0 comments
Labels
A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: lifetime related A-typesystem Area: The type system T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@jonhoo
Copy link
Contributor

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.

@Centril Centril added A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. T-lang Relevant to the language team, which will review and decide on the PR/issue. A-lifetimes Area: lifetime related A-typesystem Area: The type system labels Oct 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: lifetime related A-typesystem Area: The type system T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants