Skip to content

Covariance-related GAT lifetime mismatch #89352

Closed
@QuineDot

Description

@QuineDot

I tried this code:

#![feature(generic_associated_types)]

use core::marker::PhantomData;

pub trait GenAssoc<T> {
    type Iter<'a>;
    fn iter(&self) -> Self::Iter<'_>;
    fn reborrow<'long: 'short, 'short>(iter: Self::Iter<'long>) -> Self::Iter<'short>;
}

pub struct Wrapper<'a, T: 'a, A: GenAssoc<T>> {
    a: A::Iter<'a>,
    _p: PhantomData<T>,
}

impl<'a, T: 'a, A: GenAssoc<T>> GenAssoc<T> for Wrapper<'a, T, A>
where
    A::Iter<'a>: Clone,
{
    type Iter<'b> = ();
    fn iter<'s>(&'s self) -> Self::Iter<'s> {
        let a = A::reborrow::<'a, 's>(self.a.clone());
    }

    fn reborrow<'long: 'short, 'short>(iter: Self::Iter<'long>) -> Self::Iter<'short> {
        ()
    }
}

I expected to see this happen: Successful compilation.

Instead, this happened: Errors as follows.


error[E0308]: mismatched types
  --> src/lib.rs:23:13
   |
23 |         let a = A::reborrow::<'a, 's>(self.a.clone());
   |             ^ lifetime mismatch
   |
   = note: expected type `Sized`
              found type `Sized`
note: the lifetime `'s` as defined on the method body at 22:13...
  --> src/lib.rs:22:13
   |
22 |     fn iter<'s>(&'s self) -> Self::Iter<'s> {
   |             ^^
note: ...does not necessarily outlive the lifetime `'a` as defined on the impl at 16:6
  --> src/lib.rs:16:6
   |
16 | impl<'a, T: 'a, A: GenAssoc<T>> GenAssoc<T> for Wrapper<'a, T, A>
   |      ^^

For more information about this error, try `rustc --explain E0308`.

Note that it expected and found "type Sized", which doesn't make sense. Additionally, I don't see what is restricting 's to outlive 'a; and indeed, the error can be worked around.

Meta

Playground:

Nightly channel
Build using the Nightly version: 1.57.0-nightly
(2021-09-28 8f8092cc32ec171becef)

The original use-case tied the lifetime parameter of the GAT to the type in typical fashion. Discovered in this URLO thread.

@rustbot label +F-generic_associated_types +A-lifetimes +requires-nightly +A-diagnostics

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.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsS-waiting-on-reviewStatus: Awaiting review from the assignee but also interested parties.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