Skip to content

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

Closed
@timothee-haudebourg

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

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