]> git.lizzy.rs Git - rust.git/commitdiff
Create a helper function to retrieve the FakeReadClause at a location
authorRemy Rakic <remy.rakic@gmail.com>
Fri, 14 Sep 2018 20:22:02 +0000 (22:22 +0200)
committerRemy Rakic <remy.rakic@gmail.com>
Tue, 18 Sep 2018 12:36:37 +0000 (14:36 +0200)
src/librustc_mir/borrow_check/error_reporting.rs
src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs

index 4671332f2824c57e113fa00ea4c33cdf6b9b8f96..de2c2164a1f8f14495ccf4c4947f109a35768062 100644 (file)
@@ -12,7 +12,7 @@
 use rustc::middle::region::ScopeTree;
 use rustc::mir::VarBindingForm;
 use rustc::mir::{BindingForm, BorrowKind, ClearCrossCrate, Field, Local};
-use rustc::mir::{LocalDecl, LocalKind, Location, Operand, Place};
+use rustc::mir::{FakeReadCause, LocalDecl, LocalKind, Location, Operand, Place};
 use rustc::mir::{ProjectionElem, Rvalue, Statement, StatementKind};
 use rustc::ty;
 use rustc_data_structures::fx::FxHashSet;
@@ -989,6 +989,21 @@ pub fn is_place_thread_local(&self, place: &Place<'tcx>) -> bool {
             false
         }
     }
+
+    /// Returns the `FakeReadCause` at this location if it is a `FakeRead` statement.
+    pub(super) fn retrieve_fake_read_cause_for_location(
+        &self,
+        location: &Location,
+    ) -> Option<FakeReadCause> {
+        let stmt = self.mir.basic_blocks()[location.block]
+            .statements
+            .get(location.statement_index)?;
+        if let StatementKind::FakeRead(cause, _) = stmt.kind {
+            Some(cause)
+        } else {
+            None
+        }
+    }
 }
 
 // The span(s) associated to a use of a place.
index a62e608a8758f607f3ba4f78bd874bd3778337b1..c094350757e2fcb026e1b1f1325d8117aa0f0b55 100644 (file)
@@ -11,7 +11,7 @@
 use borrow_check::borrow_set::BorrowData;
 use borrow_check::nll::region_infer::Cause;
 use borrow_check::{Context, MirBorrowckCtxt, WriteKind};
-use rustc::mir::{FakeReadCause, Local, Location, Place, StatementKind, TerminatorKind};
+use rustc::mir::{FakeReadCause, Local, Location, Place, TerminatorKind};
 use rustc_errors::DiagnosticBuilder;
 use rustc::ty::Region;
 
@@ -145,27 +145,9 @@ pub(in borrow_check) fn find_why_borrow_contains_point(
                         // Check if the location represents a `FakeRead`, and adapt the error
                         // message to the `FakeReadCause` it is from: in particular,
                         // the ones inserted in optimized `let var = <expr>` patterns.
-                        let is_fake_read_for_let = match self.mir.basic_blocks()[location.block]
-                            .statements
-                            .get(location.statement_index)
-                        {
-                            None => false,
-                            Some(stmt) => {
-                                if let StatementKind::FakeRead(ref cause, _) = stmt.kind {
-                                    match cause {
-                                        FakeReadCause::ForLet => true,
-                                        _ => false,
-                                    }
-                                } else {
-                                    false
-                                }
-                            }
-                        };
-
-                        if is_fake_read_for_let {
-                            "borrow later stored here"
-                        } else {
-                            "borrow later used here"
+                        match self.retrieve_fake_read_cause_for_location(&location) {
+                            Some(FakeReadCause::ForLet) => "borrow later stored here",
+                            _ => "borrow later used here"
                         }
                     }
                 };