rust-lang / rust-lang/rust

`gen fn` with lifetime issue yields nonsensical suggestion

Open
#130,942 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
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

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.