Skip to content

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

Closed

Description

@timothee-haudebourg

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
{}

Activity

  1. compiler-errors commented on Apr 14, 2022

    @compiler-errors
    Contributor

    This is due to #92191

  2. added a commit that references this issue on May 2, 2022
    393705b
  3. added a commit that references this issue on May 5, 2022
    bdcb6a9
  4. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    and removed
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Sep 11, 2022
  5. added
    A-GATsArea: Generic associated types (GATs)
    on Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)C-bugCategory: This is a bug.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions