rust-lang / rust-lang/rust

"error[E0106]: missing lifetime specifiers" suggests creating unusable `&'a mut Foo<'a>` references

Open
#133,977 2 comments 7 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
struct Borrowed<'a>(&'a ());
struct Transformed<'a>(&'a ());

fn foo(_borrowed: &mut Borrowed) -> &mut Transformed {}
Current output
error[E0106]: missing lifetime specifiers
 --> src/lib.rs:4:37
  |
4 | fn foo(_borrowed: &mut Borrowed) -> &mut Transformed {}
  |                   -------------     ^    ^^^^^^^^^^^ expected named lifetime parameter
  |                                     |
  |                                     expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say which one of `_borrowed`'s 2 lifetimes it is borrowed from
help: consider introducing a named lifetime parameter
  |
4 | fn foo<'a>(_borrowed: &'a mut Borrowed<'a>) -> &'a mut Transformed<'a> {}
  |       ++++             ++             ++++      ++                ++++
Desired output
error[E0106]: missing lifetime specifiers
 --> src/lib.rs:4:37
  |
4 | fn foo(_borrowed: &mut Borrowed) -> &mut Transformed {}
  |                   -------------     ^    ^^^^^^^^^^^ expected named lifetime parameter
  |                                     |
  |                                     expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say which one of `_borrowed`'s 2 lifetimes it is borrowed from
help: consider introducing two named lifetime parameters
  |
4 | fn foo<'short, 'long>(_borrowed: &'short mut Borrowed<'long>) -> &'short mut Transformed<'long> {}
  |       +++++++++++++++             ++++++             +++++++      ++++++                +++++++
Rationale and extra context

This error trips up many new Rustaceans, and the suggestion creates unusable references which just leads to errors that cannot be resolved.

Recent example in the wild: https://users.rust-lang.org/t/how-to-reuse-a-mutably-borrowed-variable-in-rust-after-passing-it-to-a-function/122153

Other cases

Rust Version
$ rustc --version --verbose
rustc 1.85.0-nightly (c94848c04 2024-12-05)
binary: rustc
commit-hash: c94848c046d29f9a80c09aae758e27e418a289f2
commit-date: 2024-12-05
host: aarch64-apple-darwin
release: 1.85.0-nightly
LLVM version: 19.1.5
Anything else?

The erroneous suggestion is the same on the current latest stable, 1.83.0.

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 diagnostic using the Rust code shown in src/lib.rs and compare the current and desired rustc suggestions. Trace the compiler's lifetime-error suggestion handling to determine where the two lifetime parameters are selected; done means the help text suggests separate 'short and 'long parameters without producing the unusable reference form.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.