Skip to content

Commit 638acd3

Browse files
committed
Fallout from allowing some mutation in guards.
For some reason, allowing restricted mutation in match arms exposed an obvious case where a unique borrow can indeed fail, namely something like: ```rust match b { ... ref mut r if { (|| { let bar = &mut *r; **bar = false; })(); false } => { &mut *r } // ~~~~~~~ // | // This ends up holding a `&unique` borrow of `r`, but there ends up being an // implicit shared borrow in the guard thanks to rust-lang#49870 ... } ```
1 parent 5c30dc8 commit 638acd3

File tree

1 file changed

+10
-8
lines changed
  • src/librustc_mir/borrow_check

1 file changed

+10
-8
lines changed

src/librustc_mir/borrow_check/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,14 +1697,16 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
16971697
);
16981698
let mut error_reported = false;
16991699
match kind {
1700-
Reservation(WriteKind::MutableBorrow(BorrowKind::Unique))
1701-
| Write(WriteKind::MutableBorrow(BorrowKind::Unique)) => {
1702-
if let Err(_place_err) = self.is_mutable(place, LocalMutationIsAllowed::Yes) {
1703-
span_bug!(span, "&unique borrow for {:?} should not fail", place);
1704-
}
1705-
}
1706-
Reservation(WriteKind::MutableBorrow(BorrowKind::Mut { .. }))
1707-
| Write(WriteKind::MutableBorrow(BorrowKind::Mut { .. })) => {
1700+
Reservation(WriteKind::MutableBorrow(borrow_kind @ BorrowKind::Unique))
1701+
| Reservation(WriteKind::MutableBorrow(borrow_kind @ BorrowKind::Mut { .. }))
1702+
| Write(WriteKind::MutableBorrow(borrow_kind @ BorrowKind::Unique))
1703+
| Write(WriteKind::MutableBorrow(borrow_kind @ BorrowKind::Mut { .. })) =>
1704+
{
1705+
let is_local_mutation_allowed = match borrow_kind {
1706+
BorrowKind::Unique => LocalMutationIsAllowed::Yes,
1707+
BorrowKind::Mut { .. } => is_local_mutation_allowed,
1708+
BorrowKind::Shared => unreachable!(),
1709+
};
17081710
match self.is_mutable(place, is_local_mutation_allowed) {
17091711
Ok(root_place) => self.add_used_mut(root_place, flow_state),
17101712
Err(place_err) => {

0 commit comments

Comments
 (0)