Lifetime error message is backwards: lifetime of reference outlives lifetime of borrowed content #44470
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
A-lifetimes
Area: Lifetimes / regions
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
I think this diagnostic can be improved.
When compiling this code:
We get this diagnostic:
The Problematic Code
The code is trying to return a reference with lifetime
'a
where'b
should be, and the two are not compatible in any direction.Equivalently, either or:
Problems With the Diagnostic
The first line is:
lifetime of reference outlives lifetime of borrowed content...
The error doesn't mention "return type", it probably should to be clear
The error is subjectively “backwards”, it feels a bit alien. It's telling us that the return type specifies a lifetime that is longer than the borrow that was passed.
“
reference
” in this sentence refers to the reference in the return type (&'b str
); but the user will often think in concrete terms. If you say reference, they will think ofa
, the variable that holds a referenceProposed Solution
Change error message to something like:
or
This is inspired by a discussion in #rust-beginners on irc.
The text was updated successfully, but these errors were encountered: