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

Comments

@Palladinium
Copy link

@Palladinium Palladinium commented Dec 29, 2018

#![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
Copy link
Contributor

@Centril Centril commented Dec 29, 2018

@alexreg
Copy link
Contributor

@alexreg 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 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

@Arnavion Arnavion commented Jan 29, 2019

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 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 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 alexreg commented Jun 20, 2019

@chpio #60670

Want to work on it? ;-)

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 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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

6 participants