Skip to content

Commit ec5342b

Browse files
authored
Rollup merge of rust-lang#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 rust-lang#44596
2 parents e103ecd + 8c1b958 commit ec5342b

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/librustc_mir/borrow_check.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
419419
self.each_borrow_involving_path(
420420
context, lvalue_span.0, flow_state, |this, _idx, borrow| {
421421
if !borrow.compatible_with(BorrowKind::Shared) {
422-
this.report_use_while_mutably_borrowed(context, lvalue_span);
422+
this.report_use_while_mutably_borrowed(context, lvalue_span, borrow);
423423
Control::Break
424424
} else {
425425
Control::Continue
@@ -914,11 +914,17 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
914914

915915
fn report_use_while_mutably_borrowed(&mut self,
916916
_context: Context,
917-
(lvalue, span): (&Lvalue, Span)) {
917+
(lvalue, span): (&Lvalue, Span),
918+
borrow : &BorrowData) {
919+
let described_lvalue = self.describe_lvalue(lvalue);
920+
let borrow_span = self.retrieve_borrow_span(borrow);
921+
918922
let mut err = self.tcx.cannot_use_when_mutably_borrowed(
919-
span, &self.describe_lvalue(lvalue), Origin::Mir);
920-
// FIXME 1: add span_label for "borrow of `()` occurs here"
921-
// FIXME 2: add span_label for "use of `{}` occurs here"
923+
span, &described_lvalue, Origin::Mir);
924+
925+
err.span_label(borrow_span, format!("borrow of `{}` occurs here", described_lvalue));
926+
err.span_label(span, format!("use of borrowed `{}`", described_lvalue));
927+
922928
err.emit();
923929
}
924930

@@ -998,7 +1004,7 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
9981004
ProjectionElem::Downcast(..) =>
9991005
("", format!(""), None), // (dont emit downcast info)
10001006
ProjectionElem::Field(field, _ty) =>
1001-
("", format!(".{}", field.index()), None),
1007+
("", format!(".{}", field.index()), None), // FIXME: report name of field
10021008
ProjectionElem::Index(index) =>
10031009
("", format!(""), Some(index)),
10041010
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>
10241030
}
10251031
}
10261032
}
1033+
1034+
// Retrieve span of given borrow from the current MIR representation
1035+
fn retrieve_borrow_span(&self, borrow: &BorrowData) -> Span {
1036+
self.mir.basic_blocks()[borrow.location.block]
1037+
.statements[borrow.location.statement_index]
1038+
.source_info.span
1039+
}
10271040
}
10281041

10291042
impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx> {

0 commit comments

Comments
 (0)