Skip to content

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

Closed
@nikomatsakis

Description

@nikomatsakis

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions