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: lifetime is inconsistently required with Higher-Rank Trait Bounds #93262

Closed
timothee-haudebourg opened this issue Jan 24, 2022 · 1 comment · Fixed by #96593
Closed

GAT: lifetime is inconsistently required with Higher-Rank Trait Bounds #93262

timothee-haudebourg opened this issue Jan 24, 2022 · 1 comment · Fixed by #96593
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

@timothee-haudebourg
Copy link

I'm getting weird error messages by combining GATs with HRTBs. Here is an example:

#![feature(generic_associated_types)]
pub trait Trait {
    type Assoc<'a> where Self: 'a;
}

pub trait Foo<T: Trait>
where
    for<'a> T::Assoc<'a>: Clone
{}

pub struct Type;

impl<T: Trait> Foo<T> for Type
where
    for<'a> T::Assoc<'a>: Clone
{}

This was working fine with rustc 1.58. Now with 1.60 I get the following error message:

error[E0311]: the parameter type `T` may not live long enough
  --> src/lib.rs:13:16
   |
13 | impl<T: Trait> Foo<T> for Type
   |      --        ^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
   |      |
   |      help: consider adding an explicit lifetime bound...: `T: 'a +`
   |
note: ...that is required by this bound
  --> src/lib.rs:8:38
   |
8  |     for<'a> <T as Trait>::Assoc<'a>: Clone
   |                                      ^^^^^

There are a couple of weird things going on:

  • First, it was my understanding that the for<'a> <T as Trait>::Assoc<'a>: ... bound is well formed and should be accepted by the compiler even though the where Self: 'a bound on Trait::Assoc means that T must outlive 'a. I might be wrong here.
  • The error message disappears if we remove the HRTB from the trait definition but not from the trait implementation.
  • We can define a function with the same HRTB, and it compiles fine:
pub fn foo<T: Trait>()
where
    for<'a> T::Assoc<'a>: Clone
{}
@timothee-haudebourg timothee-haudebourg added the C-bug Category: This is a bug. label Jan 24, 2022
@jackh726 jackh726 added the F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs label Jan 24, 2022
@compiler-errors
Copy link
Member

This is due to #92191

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue May 2, 2022
…rors

Revert "Prefer projection candidates instead of param_env candidates for Sized predicates"

Fixes rust-lang#93262
Reopens rust-lang#89352

This was a hack that seemed to have no negative side-effects at the time. Given that the latter has a workaround and likely less common than the former, it makes sense to revert this change.

r? `@compiler-errors`
@bors bors closed this as completed in bdcb6a9 May 5, 2022
@jackh726 jackh726 added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. labels Sep 11, 2022
@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
4 participants