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

GAT: Self: 'a bound cannot be added for types which store a generic T that is further constrained by the impl #92280

Closed
kahomayo opened this issue Dec 25, 2021 · 3 comments · Fixed by #92710
Labels
C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kahomayo
Copy link

Within an impl<T: Trait1> Trait2 for Struct<T::Foo>, a GAT bound of Self: 'a does not compile, asking me to add an explicit T::Foo: 'a bound.

Playground

#![feature(generic_associated_types)]
#![allow(non_camel_case_types)]

trait HasAssoc {
    type Assoc;
}

trait Iterate<S: HasAssoc> {
    type Iter<'a> where Self: 'a;
    fn iter<'a>(&'a self) -> Self::Iter<'a>;
}

struct KeySegment_Broken<T>{ key: T }
impl<S: HasAssoc> Iterate<S> for KeySegment_Broken<S::Assoc> {
    type Iter<'a> where Self: 'a = ();
    fn iter<'a>(&'a self) -> Self::Iter<'a> {
        todo!()
    }
}

This code results in two errors:

   Compiling playground v0.0.1 (/playground)
error: `impl` associated type signature for `Iter` doesn't match `trait` associated type signature
  --> src/lib.rs:17:5
   |
9  |     type Iter<'a> where Self: 'a;
   |     ----------------------------- expected
...
17 |     type Iter<'a> where Self: 'a = ();
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found

error[E0309]: the associated type `<S as HasAssoc>::Assoc` may not live long enough
  --> src/lib.rs:17:36
   |
17 |     type Iter<'a> where Self: 'a = ();
   |                                 -  ^^ ...so that the type `KeySegment_Broken<<S as HasAssoc>::Assoc>` will meet its required lifetime bounds
   |                                 |
   |                                 help: consider adding a where clause: `, <S as HasAssoc>::Assoc: 'a`

I believe that the E0309 error is a bug, as since key is known in this impl to be S::String, S::String must outlive Self and the suggested bound should thus already be implied by Self: 'a. Applying the suggested fix removes this error, but obviously cannot compile (as the GAT impl now has a stricter where than in the trait declaration). The first error seems incorrect too, as there is no visible difference between the highlighted lines other than = ():

struct KeySegment_SuggestedFix<T>{ key: T }
impl<S: HasAssoc> Iterate<S> for KeySegment_SuggestedFix<S::Assoc> {
    // error: `impl` associated type signature for `Iter` doesn't match `trait` associated type signature
    type Iter<'a>
    where
        Self: 'a,
        S::Assoc: 'a
    = ();
    
    fn iter<'a>(&'a self) -> Self::Iter<'a> {
        todo!()
    }
}

Adding the type bound on the struct declaration avoids these errors, but is less generic.

struct KeySegment<S: OnceTypeSystem> { key: S::String }
impl<S: OnceTypeSystem> PathSegment<S> for KeySegment<S> {
    type Iter<'a> where Self: 'a = ();
    fn iter<'a>(&'a self) -> Self::Iter<'a> {
        todo!()
    }
}

Meta

rustc --version --verbose:

rustc 1.59.0-nightly (475b00aa4 2021-12-24)
binary: rustc
commit-hash: 475b00aa4037461b506539a06d15ca6091b461a7
commit-date: 2021-12-24
host: x86_64-pc-windows-msvc
release: 1.59.0-nightly
LLVM version: 13.0.0

I have also encountered this bug on 1.59.0-nightly (e100ec5bc 2021-12-21).

@kahomayo kahomayo added the C-bug Category: This is a bug. label Dec 25, 2021
@BGR360
Copy link
Contributor

BGR360 commented Dec 26, 2021

@rustbot label +F-generic_associated_types +T-compiler

@rustbot rustbot added F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 26, 2021
@jackh726

This comment has been minimized.

@rust-lang rust-lang deleted a comment from pro465 Jan 9, 2022
@pro465
Copy link
Contributor

pro465 commented Jan 10, 2022

@jackh726 I forgot what I said before, but if I said something bad, I'm really sorry for that 😞

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 15, 2022
Include Projections when elaborating TypeOutlives

Fixes rust-lang#92280

In `Elaborator`, we elaborate that `Foo<<Bar as Baz>::Assoc>: 'a` -> `<Bar as Baz>::Assoc: 'a`. This is the same rule that would be applied to any other `Param`. If there are escaping vars, we continue to do nothing.

r? `@nikomatsakis`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 15, 2022
Include Projections when elaborating TypeOutlives

Fixes rust-lang#92280

In `Elaborator`, we elaborate that `Foo<<Bar as Baz>::Assoc>: 'a` -> `<Bar as Baz>::Assoc: 'a`. This is the same rule that would be applied to any other `Param`. If there are escaping vars, we continue to do nothing.

r? ``@nikomatsakis``
@bors bors closed this as completed in 9835b90 Jan 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants