about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCorey Farwell <coreyf@rwell.org>2017-09-23 00:29:18 -0400
committerGitHub <noreply@github.com>2017-09-23 00:29:18 -0400
commitec5342ba8733a7c7b350ae51bee2599f88136cdd (patch)
treec1a4f2cad05cb8393ac169ecc8b06a0042d319a1
parente103ecd09724684c434fcd40dd0c1e33a54fd632 (diff)
parent8c1b958cf7ee4143deaf963249f6bbfd2446121e (diff)
downloadrust-ec5342ba8733a7c7b350ae51bee2599f88136cdd.tar.gz
rust-ec5342ba8733a7c7b350ae51bee2599f88136cdd.zip
Rollup merge of #44749 - zilbuz:issue-44596/E0503, r=pnkfelix
MIR-borrowck: Adding notes to E0503

This PR adds notes to the MIR borrowck error E0503.

Part of #44596
-rw-r--r--src/librustc_mir/borrow_check.rs25
1 files changed, 19 insertions, 6 deletions
diff --git a/src/librustc_mir/borrow_check.rs b/src/librustc_mir/borrow_check.rs
index 063cbc77559..5133e528b09 100644
--- a/src/librustc_mir/borrow_check.rs
+++ b/src/librustc_mir/borrow_check.rs
@@ -419,7 +419,7 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
             self.each_borrow_involving_path(
                 context, lvalue_span.0, flow_state, |this, _idx, borrow| {
                     if !borrow.compatible_with(BorrowKind::Shared) {
-                        this.report_use_while_mutably_borrowed(context, lvalue_span);
+                        this.report_use_while_mutably_borrowed(context, lvalue_span, borrow);
                         Control::Break
                     } else {
                         Control::Continue
@@ -914,11 +914,17 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
 
     fn report_use_while_mutably_borrowed(&mut self,
                                          _context: Context,
-                                         (lvalue, span): (&Lvalue, Span)) {
+                                         (lvalue, span): (&Lvalue, Span),
+                                         borrow : &BorrowData) {
+        let described_lvalue = self.describe_lvalue(lvalue);
+        let borrow_span = self.retrieve_borrow_span(borrow);
+
         let mut err = self.tcx.cannot_use_when_mutably_borrowed(
-            span, &self.describe_lvalue(lvalue), Origin::Mir);
-        // FIXME 1: add span_label for "borrow of `()` occurs here"
-        // FIXME 2: add span_label for "use of `{}` occurs here"
+            span, &described_lvalue, Origin::Mir);
+
+        err.span_label(borrow_span, format!("borrow of `{}` occurs here", described_lvalue));
+        err.span_label(span, format!("use of borrowed `{}`", described_lvalue));
+
         err.emit();
     }
 
@@ -998,7 +1004,7 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
                     ProjectionElem::Downcast(..) =>
                         ("",   format!(""), None), // (dont emit downcast info)
                     ProjectionElem::Field(field, _ty) =>
-                        ("",   format!(".{}", field.index()), None),
+                        ("",   format!(".{}", field.index()), None), // FIXME: report name of field
                     ProjectionElem::Index(index) =>
                         ("",   format!(""), Some(index)),
                     ProjectionElem::ConstantIndex { offset, min_length, from_end: true } =>
@@ -1024,6 +1030,13 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
             }
         }
     }
+
+    // Retrieve span of given borrow from the current MIR representation
+    fn retrieve_borrow_span(&self, borrow: &BorrowData) -> Span {
+        self.mir.basic_blocks()[borrow.location.block]
+            .statements[borrow.location.statement_index]
+            .source_info.span
+    }
 }
 
 impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx> {