Skip to content

gen fn with lifetime issue yields nonsensical suggestion #130942

New issue

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

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

Already on GitHub? # to your account

Open
narpfel opened this issue Sep 27, 2024 · 1 comment
Open

gen fn with lifetime issue yields nonsensical suggestion #130942

narpfel opened this issue Sep 27, 2024 · 1 comment
Labels
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.

Comments

@narpfel
Copy link
Contributor

narpfel commented Sep 27, 2024

Code

#![feature(gen_blocks)]

struct Value<'a>(&'a ());

struct Container<'a> {
    x: Value<'a>,
}

impl<'a> Container<'a> {
    gen fn f(&self) -> &'a Value {
        yield &self.x
    }
}

Current output

error: lifetime may not live long enough
  --> <source>:10:34
   |
9  |   impl<'a> Container<'a> {
   |        -- lifetime `'a` defined here
10 |       gen fn f(&self) -> &'a Value {
   |  ______________-___________________^
   | |              |
   | |              let's call the lifetime of this reference `'1`
11 | |         yield &self.x
12 | |     }
   | |_____^ method was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`
   |
help: consider adding 'move' keyword before the nested closure
   |
10 |     gen fn f(&self) -> &'a Value move {
   |                                  ++++

Desired output

<the `move` suggestion should not be given>

Rationale and extra context

Godbolt link

The correct suggestion would be to suggest &'a Value<'a> as f’s return type (or no suggestion at all).

Additionally, I don’t understand why this is an error in the first place? Given that Self contains lifetime 'a, the elided lifetime of &self must clearly be outlived by 'a? For reference, this works when directly returning &'a Value without a generator (Godbolt link):

#![allow(unused)]

struct Value<'a>(&'a ());

struct Container<'a> {
    x: Value<'a>,
}

impl<'a> Container<'a> {
    fn f(&self) -> &'a Value {
        &self.x
    }
}

Tracking issue: #117078

Other cases

No response

Rust Version

rustc 1.83.0-nightly (2bd1e894e 2024-09-26)
binary: rustc
commit-hash: 2bd1e894efde3b6be857ad345914a3b1cea51def
commit-date: 2024-09-26
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

Anything else?

No response

@narpfel narpfel 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 Sep 27, 2024
@narpfel
Copy link
Contributor Author

narpfel commented Sep 27, 2024

I don’t understand why this is an error in the first place?

I think this is the cause? #130935

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
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.
Projects
None yet
Development

No branches or pull requests

1 participant