`gen fn` with lifetime issue yields nonsensical suggestion
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the example with the cited nightly compiler and compare the diagnostic for the gen fn case with the direct-return case. Trace the compiler's lifetime diagnostic and suggestion handling for generators; done means the incorrect move suggestion is removed and, if supported, the return type suggestion is appropriate.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100