Fail gracefully if mutating on a use closure and the closure it not declared mut
This commit is contained in:
parent
6eb6ff62f7
commit
aa58439f87
3 changed files with 28 additions and 1 deletions
|
@ -823,7 +823,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
|
|||
) => {
|
||||
capture_reason = format!("mutable borrow of `{upvar}`");
|
||||
}
|
||||
ty::UpvarCapture::ByValue => {
|
||||
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {
|
||||
capture_reason = format!("possible mutation of `{upvar}`");
|
||||
}
|
||||
_ => bug!("upvar `{upvar}` borrowed, but not mutably"),
|
||||
|
|
10
tests/ui/ergonomic-clones/closure/mutation2.rs
Normal file
10
tests/ui/ergonomic-clones/closure/mutation2.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
#![feature(ergonomic_clones)]
|
||||
|
||||
fn main() {
|
||||
let mut my_var = false;
|
||||
let callback = use || {
|
||||
my_var = true;
|
||||
};
|
||||
callback();
|
||||
//~^ ERROR cannot borrow `callback` as mutable, as it is not declared as mutable [E0596]
|
||||
}
|
17
tests/ui/ergonomic-clones/closure/mutation2.stderr
Normal file
17
tests/ui/ergonomic-clones/closure/mutation2.stderr
Normal file
|
@ -0,0 +1,17 @@
|
|||
error[E0596]: cannot borrow `callback` as mutable, as it is not declared as mutable
|
||||
--> $DIR/mutation2.rs:8:5
|
||||
|
|
||||
LL | my_var = true;
|
||||
| ------ calling `callback` requires mutable binding due to possible mutation of `my_var`
|
||||
LL | };
|
||||
LL | callback();
|
||||
| ^^^^^^^^ cannot borrow as mutable
|
||||
|
|
||||
help: consider changing this to be mutable
|
||||
|
|
||||
LL | let mut callback = use || {
|
||||
| +++
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0596`.
|
Loading…
Add table
Add a link
Reference in a new issue