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

Existential type captures lifetime that doesn't appear in bounds even though it does appear #57188

Closed
Palladinium opened this issue Dec 29, 2018 · 7 comments · Fixed by #72080
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-trait-system Area: Trait system F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` requires-nightly This issue requires a nightly compiler in some way.

Comments

@Palladinium
Copy link

#![feature(existential_type)]

struct Baz<'a> {
    source: &'a str,
}

trait Foo<'a> {
    type T: Iterator<Item = Baz<'a>> + 'a;
    fn foo(source: &'a str) -> Self::T;
}

struct Bar;
impl<'a> Foo<'a> for Bar {
    existential type T: Iterator<Item = Baz<'a>> + 'a;
    fn foo(source: &'a str) -> Self::T {
        std::iter::once(Baz { source })
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
  --> src/lib.rs:15:5
   |
15 |     existential type T: Iterator<Item = Baz<'a>> + 'a;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: hidden type `std::iter::Once<Baz<'a>>` captures the lifetime 'a as defined on the impl at 14:6
  --> src/lib.rs:14:6
   |
14 | impl<'a> Foo<'a> for Bar {
   |      ^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0700`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

May be related to #42940, but I'm not entirely sure.

@Centril Centril added A-trait-system Area: Trait system A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. labels Dec 29, 2018
@Centril
Copy link
Contributor

Centril commented Dec 29, 2018

cc @alexreg

@alexreg
Copy link
Contributor

alexreg commented Dec 29, 2018

@Centril I believe this is the same as #42940 as you suggest, but maybe @cramertj can confirm.

@cramertj
Copy link
Member

cramertj commented Jan 2, 2019

It doesn't seem the same as #42940 to me-- the only lifetime I see here is 'a, which is explicitly named in the bounds of the existential type. cc @oli-obk

@Arnavion
Copy link

As a workaround, moving the existential type out and referencing it directly in the return type compiles fine:

#![feature(existential_type)]

struct Baz<'a> {
    source: &'a str,
}

trait Foo<'a> {
    type T: Iterator<Item = Baz<'a>> + 'a;
    fn foo(source: &'a str) -> Self::T;
}

existential type BarFooT<'a>: Iterator<Item = Baz<'a>> + 'a;

struct Bar;
impl<'a> Foo<'a> for Bar {
    type T = BarFooT<'a>;

    fn foo(source: &'a str) -> BarFooT<'a> { // Note: *not* Self::T
        std::iter::once(Baz { source })
    }
}

@alexreg
Copy link
Contributor

alexreg commented Jan 29, 2019

@Arnavion: @cramertj is working on a fix for a related issue. He thinks this is separate (as stated above), but who knows, a fix may cover this too, so stay tuned.

@chpio
Copy link
Contributor

chpio commented Jun 20, 2019

@alexreg What is the current status of that work? Do you have a link to that?

@alexreg
Copy link
Contributor

alexreg commented Jun 20, 2019

@chpio #60670

Want to work on it? ;-)

@Centril Centril added F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` requires-nightly This issue requires a nightly compiler in some way. labels Jul 28, 2019
RalfJung added a commit to RalfJung/rust that referenced this issue Jun 11, 2020
…=nikomatsakis

Clean up type alias impl trait implementation

- Removes special case for top-level impl trait
- Removes associated opaque types
- Forbid lifetime elision in let position impl trait. This is consistent with the behavior for inferred types.
- Handle lifetimes in type alias impl trait more uniformly with other parameters

cc rust-lang#69323
cc rust-lang#63063
Closes rust-lang#57188
Closes rust-lang#62988
Closes rust-lang#69136
@bors bors closed this as completed in ce6d3a7 Jun 15, 2020
flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 23, 2020
…ikomatsakis

Clean up type alias impl trait implementation

- Removes special case for top-level impl trait
- Removes associated opaque types
- Forbid lifetime elision in let position impl trait. This is consistent with the behavior for inferred types.
- Handle lifetimes in type alias impl trait more uniformly with other parameters

cc rust-lang#69323
cc rust-lang#63063
Closes rust-lang#57188
Closes rust-lang#62988
Closes rust-lang#69136
Closes rust-lang#73061
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-trait-system Area: Trait system F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` requires-nightly This issue requires a nightly compiler in some way.
Development

Successfully merging a pull request may close this issue.

6 participants