Skip to content

GAT lifetime mismatch with type parametrised over dyn trait object with restricted lifetime #78113

Closed
@dhardy

Description

@dhardy
#![feature(generic_associated_types)]

pub trait A {}
impl A for &dyn A {}
impl A for Box<dyn A> {}

pub struct P<'a, T: ?Sized>(&'a T);
impl<'a, T: ?Sized> A for P<'a, T> {}

pub struct Q<T: ?Sized> {
    _pd: std::marker::PhantomData<T>,
}
impl A for Q<dyn A> {}

pub trait B {
    type T<'a>: A;
}

impl B for () {
    // this is ok:
    // type T<'a> = &'a dyn A;
    
    // so is this:
    // type T<'a> = P<'a, dyn A + 'a>;
    
    // this is not:
    type T<'a> = Box<dyn A + 'a>;
    
    // nor is this:
    // type T<'a> = Q<dyn A + 'a>;
}

// Note that this is also fine:
fn _f<'a>(_x: &'a ()) -> Box<dyn A + 'a> { todo!() }
// and so is this:
fn _g<'a>(_x: &'a ()) -> Q<dyn A + 'a> { todo!() }

Error:

error[E0308]: mismatched types
  --> src/lib.rs:27:5
   |
27 |     type T<'a> = Box<dyn A + 'a>;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected trait `A`
              found trait `A`
note: the lifetime `'a` as defined on the associated item at 27:12...
  --> src/lib.rs:27:12
   |
27 |     type T<'a> = Box<dyn A + 'a>;
   |            ^^
   = note: ...does not necessarily outlive the static lifetime

Note also that a few months back, the nightly available did accept this type of code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions