Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifetime error message is backwards: lifetime of reference outlives lifetime of borrowed content #44470

Closed
bluss opened this issue Sep 9, 2017 · 3 comments
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.

Comments

@bluss
Copy link
Member

bluss commented Sep 9, 2017

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...

  1. The error doesn't mention "return type", it probably should to be clear

  2. 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).
  3. 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 of a, 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.

@bluss bluss added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 9, 2017
@42triangles
Copy link

42triangles commented Sep 9, 2017

Wouldn't changing it to

the reference has to be valid for the lifetime 'b as defined [...]

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".

@TimNN TimNN added 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. labels Sep 17, 2017
@estebank
Copy link
Contributor

Current output:

error[E0623]: lifetime mismatch
 --> src/main.rs:2:5
  |
1 | fn foo<'a, 'b>(a: &'a str, b: &'b str) -> &'b str {
  |                   -------                 -------
  |                   |
  |                   this parameter and the return type are declared with different lifetimes...
2 |     a
  |     ^ ...but data from `a` is returned here

We probably should suggest changing one of the lifetimes into the other, and/or adding a lifetime constraint on 'a.

@Enselic
Copy link
Member

Enselic commented Oct 2, 2023

Triage: Nowadays the error looks like this:

error: lifetime may not live long enough
 --> src/lib.rs:2:5
  |
1 | fn foo<'a, 'b>(a: &'a str, b: &'b str) -> &'b str {
  |        --  -- lifetime `'b` defined here
  |        |
  |        lifetime `'a` defined here
2 |     a
  |     ^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
  |
  = help: consider adding the following bound: `'a: 'b`

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.

@Enselic Enselic closed this as completed Oct 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

No branches or pull requests

5 participants