Skip to content

Closures with GATs produce obscure messages #102540

@tgross35

Description

@tgross35

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=92487d2b295e1d57d3e2c86443c94c3c

#![allow(unused)]

use std::mem;

fn main() {}

trait Proc {
    type Returns<'a>
    where
        Self: 'a;

    fn process<'a>() -> Option<Self::Returns<'a>>;
}

unsafe fn dummy_fn<T>(x: T) -> i32
where
    for<'a> T: Proc<Returns<'a> = i32>,
{
    let res = T::process();

    res.unwrap_or_else(|| 0)
}

The current output is:

    Compiling playground v0.0.1 (/playground)
error: `T` does not live long enough
  --> src/main.rs:21:5
   |
21 |     res.unwrap_or_else(|| 0)
   |     ^^^^^^^^^^^^^^^^^^^^^^^^

error: `T` does not live long enough
  --> src/main.rs:21:24
   |
21 |     res.unwrap_or_else(|| 0)
   |                        ^^^^

error: could not compile `playground` due to 2 previous errors

It is difficult to understand what is going on in the output, and I am still not quite sure what the problem is. Returning res.unwrap() instead of the unwrap_or_else line works fine, so the issue appears to be in the closure.

I believe this issue is related to GATs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler 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