Skip to content

Commit

Permalink
Fixes for shallow borrows
Browse files Browse the repository at this point in the history
* Don't promote these borrows if we're going to remove them before
  codegen
* Correctly mark unreachable code
  • Loading branch information
matthewjasper committed Mar 31, 2019
1 parent cee58fd commit 16ee042
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
8 changes: 2 additions & 6 deletions src/librustc_mir/borrow_check/error_reporting.rs
Expand Up @@ -511,14 +511,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
)
}

(BorrowKind::Shallow, _, _, BorrowKind::Unique, _, _)
| (BorrowKind::Shallow, _, _, BorrowKind::Mut { .. }, _, _) => {
// Shallow borrows are uses from the user's point of view.
self.report_use_while_mutably_borrowed(context, (place, span), issued_borrow);
return;
}
(BorrowKind::Shared, _, _, BorrowKind::Shared, _, _)
| (BorrowKind::Shared, _, _, BorrowKind::Shallow, _, _)
| (BorrowKind::Shallow, _, _, BorrowKind::Mut { .. }, _, _)
| (BorrowKind::Shallow, _, _, BorrowKind::Unique, _, _)
| (BorrowKind::Shallow, _, _, BorrowKind::Shared, _, _)
| (BorrowKind::Shallow, _, _, BorrowKind::Shallow, _, _) => unreachable!(),
};
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_mir/transform/qualify_consts.rs
Expand Up @@ -728,7 +728,10 @@ impl<'a, 'tcx> Checker<'a, 'tcx> {
interior mutability, create a static instead");
}
}
} else {
} else if let BorrowKind::Mut { .. } | BorrowKind::Shared = kind {
// Don't promote BorrowKind::Shallow borrows, as they don't
// reach codegen.

// We might have a candidate for promotion.
let candidate = Candidate::Ref(location);
// We can only promote interior borrows of promotable temps.
Expand Down
4 changes: 2 additions & 2 deletions src/test/mir-opt/match_false_edges.rs
Expand Up @@ -70,8 +70,8 @@ fn main() {
// }
// bb8: { // binding1 and guard
// StorageLive(_6);
// _6 = &(((promoted[1]: std::option::Option<i32>) as Some).0: i32);
// _4 = &shallow (promoted[0]: std::option::Option<i32>);
// _6 = &(((promoted[0]: std::option::Option<i32>) as Some).0: i32);
// _4 = &shallow _2;
// StorageLive(_7);
// _7 = const guard() -> [return: bb9, unwind: bb1];
// }
Expand Down

0 comments on commit 16ee042

Please sign in to comment.