Closed
Description
The following code:
fn main() {
let mut my_var = false;
let callback = || {
my_var = true;
};
callback();
}
produces the following error:
error[E0596]: cannot borrow `callback` as mutable, as it is not declared as mutable
--> src/main.rs:6:5
|
3 | let callback = || {
| -------- help: consider changing this to be mutable: `mut callback`
...
6 | callback();
| ^^^^^^^^ cannot borrow as mutable
error: aborting due to previous error
The expression callback()
tries to mutably borrow callback
due to the mutation of my_var
. However, the error message doesn't explain this.
We should add a note to the error message pointing to the span of the upvar that caused us to need the mutable borrow.
Metadata
Metadata
Assignees
Labels
Area: Closures (`|…| { … }`)Area: Messages for errors, warnings, and lintsCategory: An issue proposing an enhancement or a PR with one.Diagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Relevant to the compiler team, which will review and decide on the PR/issue.