From 16ee042100d252d1c59c80c797a481be340151b3 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Sun, 31 Mar 2019 15:14:50 +0100 Subject: [PATCH] Fixes for shallow borrows * Don't promote these borrows if we're going to remove them before codegen * Correctly mark unreachable code --- src/librustc_mir/borrow_check/error_reporting.rs | 8 ++------ src/librustc_mir/transform/qualify_consts.rs | 5 ++++- src/test/mir-opt/match_false_edges.rs | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/librustc_mir/borrow_check/error_reporting.rs b/src/librustc_mir/borrow_check/error_reporting.rs index 95701204cab6d..a94350f9cde00 100644 --- a/src/librustc_mir/borrow_check/error_reporting.rs +++ b/src/librustc_mir/borrow_check/error_reporting.rs @@ -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!(), }; diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 0b9ad85e6b1c7..4400717fabc53 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -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. diff --git a/src/test/mir-opt/match_false_edges.rs b/src/test/mir-opt/match_false_edges.rs index 0cbf048697aac..7ac36a22274f3 100644 --- a/src/test/mir-opt/match_false_edges.rs +++ b/src/test/mir-opt/match_false_edges.rs @@ -70,8 +70,8 @@ fn main() { // } // bb8: { // binding1 and guard // StorageLive(_6); -// _6 = &(((promoted[1]: std::option::Option) as Some).0: i32); -// _4 = &shallow (promoted[0]: std::option::Option); +// _6 = &(((promoted[0]: std::option::Option) as Some).0: i32); +// _4 = &shallow _2; // StorageLive(_7); // _7 = const guard() -> [return: bb9, unwind: bb1]; // }