Skip to content

Generic associated types break lifetime checks #97515

Open
@101arrowz

Description

@101arrowz

Here's some code with generic associated types and impl Trait:

#![feature(generic_associated_types)]
use std::future::{Future, ready, Ready};

pub trait Example {
    type Value<'a>: Future<Output = &'a str> where Self: 'a;
    fn example(&mut self) -> Self::Value<'_>;
}

pub struct ExImpl<'a>(&'a str);

impl Example for ExImpl<'_> {
    type Value<'a> = Ready<&'a str> where Self: 'a;
    fn example(&mut self) -> Self::Value<'_> {
        ready(self.0)
    }
}

async fn callee(mut ex: impl Example) {
    // Note that if you don't use a separate function, no errors appear
    dbg!(ex.example().await);
}

#[tokio::main]
async fn main() {
    tokio::spawn(async move {
        let ex = ExImpl("hello");
        callee(ex).await;
    });
}

Playground

Expected: ex.example().await = "hello"

Current behavior:

Compiling playground v0.0.1 (/playground)
error[[E0477]]: the type `ExImpl<'_>` does not fulfill the required lifetime
  --> src/main.rs:24:5
   |
24 |     tokio::spawn(async move {
   |     ^^^^^^^^^^^^

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

Clearly ExImpl("hello") has a static lifetime and yet borrowck still complains that the lifetime isn't static. Interestingly, if we inline the contents of callee (i.e. never write impl Example) there are no issues.

This is also a diagnostic issue: not only is the error in the wrong place, it doesn't provide any suggestions to fix and doesn't point out what lifetime ex actually needs.

Meta

rustc --version --verbose:

rustc 1.63.0-nightly (ebbcbfc23 2022-05-27)
binary: rustc
commit-hash: ebbcbfc236ced21d5e6a92269edb704692ff26b8
commit-date: 2022-05-27
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)C-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions