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

GATs unable to determine if item will live long enough when combined with HRTBs #88683

Closed
Michael-F-Bryan opened this issue Sep 6, 2021 · 2 comments
Labels
A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs

Comments

@Michael-F-Bryan
Copy link

(I'm not sure whether this is a diagnostics issue or a bug in the way Generic Associated Types are implemented)

Following this comment on the user forums, I was wondering if it's possible to create a trait which encapsulates the convention that (&foo).into_iter() is equivalent to foo.iter().

This is what I came up with:

#![feature(generic_associated_types)]

trait Iter {
    type Iterator<'a>: Iterator + 'a;
    type Item<'a>: 'a;

    fn iter(&self) -> Self::Iterator<'_>;
}

impl<I> Iter for I
where
    for<'a> &'a I: IntoIterator,
    for<'a> <&'a I as IntoIterator>::IntoIter: Iterator,
{
    type Iterator<'a> where I: 'a = <&'a I as IntoIterator>::IntoIter;
    type Item<'a> where I: 'a = <&'a I as IntoIterator>::Item;

    fn iter(&self) -> Self::Iterator<'_> {
        IntoIterator::into_iter(self)
    }
}

(playground)

Which generates this output:

   Compiling playground v0.0.1 (/playground)
error[E0309]: the parameter type `I` may not live long enough
  --> src/lib.rs:15:5
   |
15 |     type Iterator<'a> where I: 'a = <&'a I as IntoIterator>::IntoIter;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |                            |
   |     |                            help: consider adding a where clause: `, I: 'a`
   |     ...so that the type `I` will meet its required lifetime bounds

error[E0309]: the parameter type `I` may not live long enough
  --> src/lib.rs:16:5
   |
16 |     type Item<'a> where I: 'a = <&'a I as IntoIterator>::Item;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |                        |
   |     |                        help: consider adding a where clause: `, I: 'a`
   |     ...so that the type `I` will meet its required lifetime bounds

For more information about this error, try `rustc --explain E0309`.

Here I've already said type Iterator<'a> where I: 'a, yet it seems like the compiler isn't able to prove that I will live long enough so it suggests adding another I: 'a to the trait bound. I tried using type Iterator<'a> where I: 'a, I: 'a, ..., I: 'a like the compiler suggests

Meta

This was done on the playground using rustc version:

1.56.0-nightly (2021-09-01 50171c310cd15e1b2d37)
@Michael-F-Bryan Michael-F-Bryan added the C-bug Category: This is a bug. label Sep 6, 2021
@jackh726 jackh726 added the F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs label Sep 7, 2021
@jackh726
Copy link
Member

This is a diagnostics issue. The trait and impl types need a where Self: 'a bound. #89914 should fix the abysmally poor diagnostic, and the outlives lint should prevent running into issues like this.

@jackh726
Copy link
Member

I'm going to go ahead and close as a duplicate of existing issues. There is a better diagnostic that has landed for mismatched where clauses and the outlives lint is also in progress.

@fmease fmease added the A-GATs Area: Generic associated types (GATs) label Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs
Projects
None yet
Development

No branches or pull requests

3 participants