Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic associated types break lifetime checks #97515

Open
101arrowz opened this issue May 29, 2022 · 3 comments
Open

Generic associated types break lifetime checks #97515

101arrowz opened this issue May 29, 2022 · 3 comments
Labels
A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@101arrowz
Copy link

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
@101arrowz 101arrowz added the C-bug Category: This is a bug. label May 29, 2022
@bstrie bstrie added F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs T-types Relevant to the types team, which will review and decide on the PR/issue. labels Jun 2, 2022
@tavianator
Copy link
Contributor

tavianator commented Jun 17, 2022

I think I ran into this a couple days ago. Here's a reduced version of what I was trying:

#![feature(generic_associated_types)]

trait Trait {
    type Gat<'a> where Self: 'a;
    
    fn method<F>(&self, f: F)
    where
        F: for<'a> FnOnce(Self::Gat<'a>);
}

struct Struct;

impl Trait for &Struct {
    type Gat<'a> = &'a Struct;
    
    fn method<F>(&self, f: F)
    where
        F: for<'a> FnOnce(Self::Gat<'a>)
    {
        f(self)
    }
}

Which gives the error

error[E0477]: the type `&Struct` does not fulfill the required lifetime
  --> src/lib.rs:16:8
   |
16 |     fn method<F>(&self, f: F)
   |        ^^^^^^

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

This is at least a diagnostics issue, since it doesn't say which lifetime "the required lifetime" is.

Removing the where Self: 'a bound fixes the reduced test case, but I think I needed it in the original.

@tavianator
Copy link
Contributor

Also, the playground links to https://doc.rust-lang.org/nightly/error-index.html#E0477, which incorrectly says

Note: this error code is no longer emitted by the compiler.

@AlexSherbinin
Copy link

Is there progress toward fixing it?

@fmease fmease added A-GATs Area: Generic associated types (GATs) and removed F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs labels Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants