Repository navigation
Lifetime error message is backwards: lifetime of reference outlives lifetime of borrowed content #44470
Description
I think this diagnostic can be improved.
When compiling this code:
fn foo<'a, 'b>(a: &'a str, b: &'b str) -> &'b str {
a
}We get this diagnostic:
error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> src/main.rs:4:5
|
4 | a
| ^
|
note: ...the reference is valid for the lifetime 'b as defined on the function body at 3:1...
--> src/main.rs:3:1
|
3 | / fn foo<'a, 'b>(a: &'a str, b: &'b str) -> &'b str {
4 | | a
5 | | }
| |_^
note: ...but the borrowed content is only valid for the lifetime 'a as defined on the function body at 3:1
--> src/main.rs:3:1
|
3 | / fn foo<'a, 'b>(a: &'a str, b: &'b str) -> &'b str {
4 | | a
5 | | }
| |_^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:
- The user is supplying a reference that is not valid long enough to match the signature
- The user is supplying a signature that is not matching the reference that is being returned
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.
- The forwards direction is: borrowed content does not live long enough for the lifetime of the reference (in the return type).
-
“
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 reference- Especially the note “the reference is valid for the lifetime 'b” sounds very concrete, and it's confusing that this is not referring to any variable at all, still just the return type.
Proposed Solution
Change error message to something like:
borrowed content does not live long enough for reference in the return type of function foo
or
borrowed content does not live long enough to be returned from function foo
This is inspired by a discussion in #rust-beginners on irc.
Activity
42triangles commentedon Sep 9, 2017
Wouldn't changing it to
be better? It works for every position where the user supplied type info's (in return types,
let-statements,as-expressions and functions/methods with an added::<...>) regarding lifetimes may create errors.It may not be as clear as "required by the return type", but it at least removes the confusion caused by the "is valid for".
estebank commentedon Jan 17, 2018
Current output:
We probably should suggest changing one of the lifetimes into the other, and/or adding a lifetime constraint on
'a.Enselic commentedon Oct 2, 2023
Triage: Nowadays the error looks like this:
which is what estebank suggested should be done. I did a bisect and this was fixed in nightly-2022-06-08. Most likely by #95565 which also have regression tests that as far as I can tell covers this case. Closing as fixed.