Rust shouldn't suggest using a single lifetime everywhere
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
pub fn foo(x: &i32, y: &i32) -> &i32 {
x
}
Current output
error[E0106]: missing lifetime specifier
--> src/lib.rs:1:33
|
1 | pub fn foo(x: &i32, y: &i32) -> &i32 {
| ---- ---- ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y`
help: consider introducing a named lifetime parameter
|
1 | pub fn foo<'a>(x: &'a i32, y: &'a i32) -> &'a i32 {
| ++++ ++ ++ ++
For more information about this error, try `rustc --explain E0106`.
Desired output
...
pub fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 {
...
Rationale and extra context
The compiler is telling the user to apply the same lifetime everywhere. This is incorrect, and will make the code seemingly work, but then will fail later.
This issue of incorrectly using a single lifetime everywhere is a very common beginner pitfall, and the compiler teaching that to users does not help.
Other cases
Rust Version
Reproducible on the playground with version 1.93.0-nightly
(2025-10-29 292be5c7c05138d753bb)
Anything else?
Ideally, the compiler should be able to look at the function body to determine how to correctly annotate the lifetime. But if that's not feasible, the compiler should at least suggest annotating the lifetime only one time, with each possible place as alternatives in the suggestion.
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
Run the src/lib.rs reproducer on the stated Rust nightly and read the E0106 explanation first. Trace the diagnostic that emits the lifetime suggestion, then verify that the output avoids applying one lifetime everywhere and matches the desired alternatives; the payload names no existing test file.
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
- Clearly specified
- Newbie friendliness
- 45/100