Skip to content

GAT: lifetime mismatch when requiring lifetime bounds despite type not using the lifetimes at all #87748

Description

@athre0z

Compiling this code:

#![feature(generic_associated_types)]

trait MyTrait {
    type Assoc<'a, 'b> where 'b: 'a;
    fn do_sth(arg: Self::Assoc<'_, '_>);
}

struct Foo;

impl MyTrait for Foo {
    type Assoc<'a, 'b> where 'b: 'a = u32;
    
    fn do_sth(_: u32) {}
    // fn do_sth(_: Self::Assoc<'static, 'static>) {}
    // fn do_sth(_: Self::Assoc<'_, '_>) {}
}

I'd expect all of these three versions to compile. The associated type has two lifetimes as well as a lifetime bound 'b: 'a, but u32 doesn't use the lifetime. However, I'm getting a compiler error:

error[E0478]: lifetime bound not satisfied
  --> src/lib.rs:13:5
   |
13 |     fn do_sth(_: u32) {}
   |     ^^^^^^^^^^^^^^^^^
   |
note: lifetime parameter instantiated with the anonymous lifetime #2 defined on the method body at 13:5
  --> src/lib.rs:13:5
   |
13 |     fn do_sth(_: u32) {}
   |     ^^^^^^^^^^^^^^^^^
note: but lifetime parameter must outlive the anonymous lifetime #1 defined on the method body at 13:5
  --> src/lib.rs:13:5
   |
13 |     fn do_sth(_: u32) {}
   |     ^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0478`.
error: could not compile `playground` due to previous error

Without the where 'b: 'a, all three compile fine.

Playground Link

Activity

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. GATsS-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions