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 should be able to capture long-lived associated types even if the trait contains a lifetime #54385

Open
RalfJung opened this issue Sep 20, 2018 · 4 comments

Comments

@RalfJung
Copy link
Member

@RalfJung RalfJung commented Sep 20, 2018

Basically, this should compile:

trait Trait<'foo> { type Assoc: 'static; }

fn test<'foo, T: Trait<'foo>>(x: T::Assoc) -> impl FnOnce() {
    move || { let _x = x; }
}

Thanks to @eddyb I can work around this, writing

trait Trait<'foo> { type Assoc: 'static; }

fn test<'foo, A: 'static, T: Trait<'foo, Assoc=A>>(x: T::Assoc) -> impl FnOnce() {
    move || { let _x = x; }
}

but that is almost impossible to discover.

@eddyb
Copy link
Member

@eddyb eddyb commented Sep 20, 2018

It doesn't compile right now because it contains <T as Trait<'foo>>::Assoc, which, despite being 'static, contains the lifetime 'foo, so for soundness reasons, the impl FnOnce() opaque type must also be parametrized over 'foo (to recover the concrete return type).

We could try some erasure scheme, but the "exporting" of the concrete type is already pretty complicated, and there's a huge risk here, of opening up a soundness hole.

cc @nikomatsakis @cramertj @oli-obk

@Arnavion
Copy link

@Arnavion Arnavion commented Sep 21, 2018

(Unexpected lifetime capture by impl-trait is #42940 )

@RalfJung
Copy link
Member Author

@RalfJung RalfJung commented Jul 11, 2019

@Mark-Simulacrum AFAIK this bug here is specifically about associated types, unlike #42940. Why did you close?

@Mark-Simulacrum
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum commented Jul 11, 2019

I got the impression that the underlying bug is the same. But perhaps not. Reopening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants