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 · 2 comments

Comments

Projects
None yet
3 participants
@RalfJung
Copy link
Member

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

This comment has been minimized.

Copy link
Member

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

@eddyb eddyb added the A-impl-trait label Sep 20, 2018

@Arnavion

This comment has been minimized.

Copy link

Arnavion commented Sep 21, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment