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

Closures with GATs produce obscure messages #102540

Open
tgross35 opened this issue Oct 1, 2022 · 1 comment
Open

Closures with GATs produce obscure messages #102540

tgross35 opened this issue Oct 1, 2022 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-GATs Area: Generic associated types (GATs) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tgross35
Copy link
Contributor

tgross35 commented Oct 1, 2022

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.

@tgross35 tgross35 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 1, 2022
@tgross35
Copy link
Contributor Author

tgross35 commented Oct 1, 2022

Even something like res.map_or_else(|| 0, |_| 1) fails to compile, but the following works OK:

if let Some(v) = res {
    0
} else {
    1
}

@fmease fmease added the A-GATs Area: Generic associated types (GATs) label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-GATs Area: Generic associated types (GATs) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants