Repository navigation
Closed
Labels
A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
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.
Reacted by Léo Lanteri Thauvin, tesuji, León Orell Valerian Liehr, Ömer Sinan Ağacan, 大风吹 and Matt Piekenbrock
Metadata
Metadata
Assignees
Labels
A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
camelid commentedon Dec 23, 2020
So it looks like this is the function that needs to be modified:
rust/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs
Lines 23 to 30 in 89886e6
It seems I might be able to get the information I need from
self.used_mut_upvars?camelid commentedon Dec 23, 2020
Actually,
self.used_mut_upvarswon't work here because it's only used when we're borrow-checking the closure, not uses of it (self.used_mut_upvarsis empty in this context).sledgehammervampire commentedon Jan 11, 2021
@rustbot claim
sledgehammervampire commentedon Jan 11, 2021
Is anyone going to work on this issue? If not, I want to work on this to help me learn how Rust works.
Aaron1011 commentedon Jan 11, 2021
@1000teslas Go ahead! If you need any help, feel free to reach out to me on Zulip or Discord
camelid commentedon Jan 11, 2021
I was working on this, but I guess I should have pinged Aaron: https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/finding.20mutated.20upvars.20in.20borrowck. Oh well :)
@1000teslas you can work on this, I have other things that I've been meaning to get to anyway.
sledgehammervampire commentedon Jan 13, 2021
@Aaron1011 What is the intended error? Is it meant to point to my_var, similarly to room_ref in #78938?
Aaron1011 commentedon Jan 13, 2021
Yes - I was thinking of displaying a message like "a mutable borrow is required because the closure modifies
my_var", located at the usage ofmy_var. Additionally, we could add another note pointing to the definition ofmy_var.sledgehammervampire commentedon Jan 17, 2021
I'm not sure how given a
LocationorPlaceof a call, we can obtain theLocalpointing to the definition forself.body.local_decls.Aaron1011 commentedon Jan 17, 2021
@1000teslas I think the information you need already exists in
closure_kind_origins().You can see an example of retrieving this here:
rust/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs
Lines 295 to 298 in edeb631
and of using it:
rust/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Lines 591 to 612 in edeb631
I don't think you'll need to access the MIR
LocalRollup merge of rust-lang#81158 - 1000teslas:issue-80313-fix, r=Aaron…
Rollup merge of rust-lang#81158 - 1000teslas:issue-80313-fix, r=Aaron…
Rollup merge of rust-lang#81158 - 1000teslas:issue-80313-fix, r=Aaron…
Rollup merge of rust-lang#81158 - 1000teslas:issue-80313-fix, r=Aaron…
JohnTitor commentedon Jan 29, 2021
Fixed by #81158