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

when temporary value is a fn call argument, highlighting can be misleading #36279

Closed
nikomatsakis opened this issue Sep 5, 2016 · 6 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Sep 5, 2016

In an example where the temporary value is dropped at the end of the statement, such as when the & is an argument to a fn call, the resulting error can be somewhat misleading:

error: borrowed value does not live long enough
  --> <anon>:21:22
   |
21 |     let r = map.get(&'a');
   |                      ^^^ - borrowed value only valid until here
   |                      |
   |                      does not live long enough
22 |     assert_eq!(r, Some(&string));
23 | }
   | - borrowed value must be valid until here
   |

In particular, the - borrowed value only valid until here is intended to be "attached" to the ;, but it kind of looks like it is attached to the ^^^.

One simple way to improve this would be to change the text -- temporaries are freed at one of two kinds of places:

  • innermost enclosing statement (usually);
    • note though that the tail expr of a block is contained in the stmt that contains the block (transitively)
  • end of block (if syntactically evident that they are assigned into a let binding)

I imagine we could detect which of these two categories we fall into and say "only valid until end of this statement" in the first case or "end of this block" in the second. That might help, though I guess people might not know what a "statement" is.

@nikomatsakis
Copy link
Contributor Author

cc @jonathandturner

@brson brson added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 7, 2016
@mikhail-m1
Copy link
Contributor

mikhail-m1 commented Oct 27, 2016

@jonathandturner I'am ready to fix it after merge #37405

bors added a commit that referenced this issue Nov 12, 2016
Improve "Doesn't live long enough" error

case with temporary variable

issue #36279 part of #35233

r? @jonathandturner
@sophiajt
Copy link
Contributor

@nikomatsakis - can you confirm this is fixed now? I think there were some improvements that landed recently

@steveklabnik steveklabnik added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 9, 2017
@estebank
Copy link
Contributor

Current output:

rustc 1.17.0-nightly (cab4bff3d 2017-03-21)
error: borrowed value does not live long enough
  --> <anon>:21:26
   |
21 |     let r = map.get(&'a');
   |                      --- ^ temporary value dropped here while still borrowed
   |                      |
   |                      temporary value created here
22 |     assert_eq!(r, Some(&string));
23 | }
   | - temporary value needs to live until here
   |
   = note: consider using a `let` binding to increase its lifetime

error: aborting due to previous error

@nikomatsakis
Copy link
Contributor Author

This does seem somewhat better.

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

This code compiles successfully now.

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

7 participants