rust-lang / rust-lang/rust

Vague error message with elided lifetime in impl block

Open
#119,330 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-newcomer-roadblock D-terse T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

Code
struct Borrow<'a, T>(&'a T);

impl<T> Borrow<'_, T> {
    pub fn new(t: &T) -> Self {
        Self(t)
    }
}
Current output
error: lifetime may not live long enough
 --> src/main.rs:7:9
  |
6 |     pub fn new(t: &T) -> Self {
  |                   -      ---- return type is Borrow<'2, T>
  |                   |
  |                   let's call the lifetime of this reference `'1`
7 |         Self(t)
  |         ^^^^^^^ associated function was supposed to return data with lifetime `'2` but it is returning data with lifetime `'1`
Desired output
The original output with the addition of something like:
impl<T> Borrow<'_, T> {
                ^ Let's call this elided lifetime '2

help: consider explicitly naming the lifetime:
      impl<'a, T> Borrow<'a, T> {
Rationale and extra context

In this error message it's not clear that the '2 lifetime comes from the '_ at the impl block definition, and the error does not suggest that that explicitly naming the lifetime might fix the problem.

Other cases

No response

Anything else?

If adding these changes seems like a desirable change, I would be happy to prepare a PR. I've never contributed to the compiler before, so some pointers on where to start looking to make a change would be appreciated :)

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

Start with the reproducer in src/main.rs and compare the current diagnostic with the desired output for the elided lifetime in the impl block. Trace how the compiler labels that lifetime and produces lifetime suggestions; done means the diagnostic identifies the source of '2 and suggests explicitly naming it.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.