Skip to content

Commit e708cbd

Browse files
committed
Auto merge of #81717 - Aaron1011:fix/closure-diag, r=estebank
Fix panic when emitting diagnostic for closure mutable binding error Fixes #81700 The upvar borrow kind may be `ty::BorrowKind::UniqueImmBorrow`, which is still a mutable borrow for the purposes of this diagnostic code.
2 parents 120b2a7 + bc84e21 commit e708cbd

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -514,15 +514,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
514514
let upvar = ty::place_to_string_for_capture(tcx, place);
515515
match tables.upvar_capture(upvar_id) {
516516
ty::UpvarCapture::ByRef(ty::UpvarBorrow {
517-
kind: ty::BorrowKind::MutBorrow,
517+
kind: ty::BorrowKind::MutBorrow | ty::BorrowKind::UniqueImmBorrow,
518518
..
519519
}) => {
520520
format!("mutable borrow of `{}`", upvar)
521521
}
522522
ty::UpvarCapture::ByValue(_) => {
523523
format!("possible mutation of `{}`", upvar)
524524
}
525-
_ => bug!("upvar `{}` borrowed, but not mutably", upvar),
525+
val => bug!("upvar `{}` borrowed, but not mutably: {:?}", upvar, val),
526526
}
527527
} else {
528528
bug!("not an upvar")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn foo(x: &mut u32) {
2+
let bar = || { foo(x); };
3+
bar(); //~ ERROR cannot borrow
4+
}
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0596]: cannot borrow `bar` as mutable, as it is not declared as mutable
2+
--> $DIR/issue-81700-mut-borrow.rs:3:5
3+
|
4+
LL | let bar = || { foo(x); };
5+
| --- - calling `bar` requires mutable binding due to mutable borrow of `x`
6+
| |
7+
| help: consider changing this to be mutable: `mut bar`
8+
LL | bar();
9+
| ^^^ cannot borrow as mutable
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)