Skip to content

Commit a57f1c9

Browse files
committed
Auto merge of #54666 - matthewjasper:mir-function-spans, r=pnkfelix
[NLL] Improve "borrow later used here" messages * In the case of two conflicting borrows, the later used message says which borrow it's referring to * If the later use is a function call (from the users point of view) say that the later use is for the call. Point just to the function. r? @pnkfelix Closes #48643
2 parents 5de5281 + bc4f9b8 commit a57f1c9

File tree

119 files changed

+665
-653
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+665
-653
lines changed

src/librustc/ich/impls_mir.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,13 @@ for mir::TerminatorKind<'gcx> {
194194
mir::TerminatorKind::Call { ref func,
195195
ref args,
196196
ref destination,
197-
cleanup } => {
197+
cleanup,
198+
from_hir_call, } => {
198199
func.hash_stable(hcx, hasher);
199200
args.hash_stable(hcx, hasher);
200201
destination.hash_stable(hcx, hasher);
201202
cleanup.hash_stable(hcx, hasher);
203+
from_hir_call.hash_stable(hcx, hasher);
202204
}
203205
mir::TerminatorKind::Assert { ref cond,
204206
expected,

src/librustc/mir/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,9 @@ pub enum TerminatorKind<'tcx> {
10441044
destination: Option<(Place<'tcx>, BasicBlock)>,
10451045
/// Cleanups to be done if the call unwinds.
10461046
cleanup: Option<BasicBlock>,
1047+
/// Whether this is from a call in HIR, rather than from an overloaded
1048+
/// operator. True for overloaded function call.
1049+
from_hir_call: bool,
10471050
},
10481051

10491052
/// Jump to the target if the condition has the expected value,
@@ -2805,6 +2808,7 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
28052808
ref args,
28062809
ref destination,
28072810
cleanup,
2811+
from_hir_call,
28082812
} => {
28092813
let dest = destination
28102814
.as_ref()
@@ -2815,6 +2819,7 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
28152819
args: args.fold_with(folder),
28162820
destination: dest,
28172821
cleanup,
2822+
from_hir_call,
28182823
}
28192824
}
28202825
Assert {

src/librustc/mir/visit.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ macro_rules! make_mir_visitor {
468468
TerminatorKind::Call { ref $($mutability)* func,
469469
ref $($mutability)* args,
470470
ref $($mutability)* destination,
471-
cleanup } => {
471+
cleanup,
472+
from_hir_call: _, } => {
472473
self.visit_operand(func, source_location);
473474
for arg in args {
474475
self.visit_operand(arg, source_location);

src/librustc_codegen_llvm/mir/block.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,13 @@ impl FunctionCx<'a, 'll, 'tcx> {
412412
bug!("undesugared DropAndReplace in codegen: {:?}", terminator);
413413
}
414414

415-
mir::TerminatorKind::Call { ref func, ref args, ref destination, cleanup } => {
415+
mir::TerminatorKind::Call {
416+
ref func,
417+
ref args,
418+
ref destination,
419+
cleanup,
420+
from_hir_call: _
421+
} => {
416422
// Create the callee. This is a fn ptr or zero-sized and hence a kind of scalar.
417423
let callee = self.codegen_operand(&bx, func);
418424

src/librustc_mir/borrow_check/error_reporting.rs

+47-38
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use rustc::hir;
1515
use rustc::hir::def_id::DefId;
1616
use rustc::middle::region::ScopeTree;
1717
use rustc::mir::{
18-
self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, FakeReadCause, Field, Local,
19-
LocalDecl, LocalKind, Location, Operand, Place, PlaceProjection, ProjectionElem, Rvalue,
20-
Statement, StatementKind, TerminatorKind, VarBindingForm,
18+
self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, Field, Local,
19+
LocalDecl, LocalKind, Location, Operand, Place, PlaceProjection, ProjectionElem,
20+
Rvalue, Statement, StatementKind, TerminatorKind, VarBindingForm,
2121
};
2222
use rustc::ty;
2323
use rustc::util::ppaux::with_highlight_region_for_bound_region;
@@ -262,7 +262,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
262262
move_spans.var_span_label(&mut err, "move occurs due to use in closure");
263263

264264
self.explain_why_borrow_contains_point(context, borrow, None)
265-
.emit(self.infcx.tcx, &mut err);
265+
.emit(self.infcx.tcx, &mut err, String::new());
266266
err.buffer(&mut self.errors_buffer);
267267
}
268268

@@ -299,7 +299,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
299299
});
300300

301301
self.explain_why_borrow_contains_point(context, borrow, None)
302-
.emit(self.infcx.tcx, &mut err);
302+
.emit(self.infcx.tcx, &mut err, String::new());
303303
err.buffer(&mut self.errors_buffer);
304304
}
305305

@@ -319,6 +319,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
319319
let desc_place = self.describe_place(place).unwrap_or("_".to_owned());
320320
let tcx = self.infcx.tcx;
321321

322+
let first_borrow_desc;
323+
322324
// FIXME: supply non-"" `opt_via` when appropriate
323325
let mut err = match (
324326
gen_borrow_kind,
@@ -328,8 +330,23 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
328330
"immutable",
329331
"mutable",
330332
) {
331-
(BorrowKind::Shared, lft, _, BorrowKind::Mut { .. }, _, rgt)
332-
| (BorrowKind::Mut { .. }, _, lft, BorrowKind::Shared, rgt, _) => {
333+
(BorrowKind::Shared, lft, _, BorrowKind::Mut { .. }, _, rgt) => {
334+
first_borrow_desc = "mutable ";
335+
tcx.cannot_reborrow_already_borrowed(
336+
span,
337+
&desc_place,
338+
"",
339+
lft,
340+
issued_span,
341+
"it",
342+
rgt,
343+
"",
344+
None,
345+
Origin::Mir,
346+
)
347+
}
348+
(BorrowKind::Mut { .. }, _, lft, BorrowKind::Shared, rgt, _) => {
349+
first_borrow_desc = "immutable ";
333350
tcx.cannot_reborrow_already_borrowed(
334351
span,
335352
&desc_place,
@@ -345,6 +362,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
345362
}
346363

347364
(BorrowKind::Mut { .. }, _, _, BorrowKind::Mut { .. }, _, _) => {
365+
first_borrow_desc = "first ";
348366
tcx.cannot_mutably_borrow_multiply(
349367
span,
350368
&desc_place,
@@ -357,6 +375,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
357375
}
358376

359377
(BorrowKind::Unique, _, _, BorrowKind::Unique, _, _) => {
378+
first_borrow_desc = "first ";
360379
tcx.cannot_uniquely_borrow_by_two_closures(
361380
span,
362381
&desc_place,
@@ -384,18 +403,22 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
384403
return;
385404
}
386405

387-
(BorrowKind::Unique, _, _, _, _, _) => tcx.cannot_uniquely_borrow_by_one_closure(
388-
span,
389-
&desc_place,
390-
"",
391-
issued_span,
392-
"it",
393-
"",
394-
None,
395-
Origin::Mir,
396-
),
406+
(BorrowKind::Unique, _, _, _, _, _) => {
407+
first_borrow_desc = "first ";
408+
tcx.cannot_uniquely_borrow_by_one_closure(
409+
span,
410+
&desc_place,
411+
"",
412+
issued_span,
413+
"it",
414+
"",
415+
None,
416+
Origin::Mir,
417+
)
418+
},
397419

398420
(BorrowKind::Shared, lft, _, BorrowKind::Unique, _, _) => {
421+
first_borrow_desc = "first ";
399422
tcx.cannot_reborrow_already_uniquely_borrowed(
400423
span,
401424
&desc_place,
@@ -409,6 +432,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
409432
}
410433

411434
(BorrowKind::Mut { .. }, _, lft, BorrowKind::Unique, _, _) => {
435+
first_borrow_desc = "first ";
412436
tcx.cannot_reborrow_already_uniquely_borrowed(
413437
span,
414438
&desc_place,
@@ -459,7 +483,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
459483
}
460484

461485
self.explain_why_borrow_contains_point(context, issued_borrow, None)
462-
.emit(self.infcx.tcx, &mut err);
486+
.emit(self.infcx.tcx, &mut err, first_borrow_desc.to_string());
463487

464488
err.buffer(&mut self.errors_buffer);
465489
}
@@ -614,7 +638,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
614638

615639
if let BorrowExplanation::MustBeValidFor(..) = explanation {
616640
} else {
617-
explanation.emit(self.infcx.tcx, &mut err);
641+
explanation.emit(self.infcx.tcx, &mut err, String::new());
618642
}
619643
} else {
620644
err.span_label(borrow_span, "borrowed value does not live long enough");
@@ -625,7 +649,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
625649

626650
borrow_spans.args_span_label(&mut err, "value captured here");
627651

628-
explanation.emit(self.infcx.tcx, &mut err);
652+
explanation.emit(self.infcx.tcx, &mut err, String::new());
629653
}
630654

631655
err
@@ -685,7 +709,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
685709
_ => {}
686710
}
687711

688-
explanation.emit(self.infcx.tcx, &mut err);
712+
explanation.emit(self.infcx.tcx, &mut err, String::new());
689713

690714
err.buffer(&mut self.errors_buffer);
691715
}
@@ -752,7 +776,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
752776
}
753777
_ => {}
754778
}
755-
explanation.emit(self.infcx.tcx, &mut err);
779+
explanation.emit(self.infcx.tcx, &mut err, String::new());
756780

757781
borrow_spans.args_span_label(&mut err, "value captured here");
758782

@@ -889,7 +913,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
889913
loan_spans.var_span_label(&mut err, "borrow occurs due to use in closure");
890914

891915
self.explain_why_borrow_contains_point(context, loan, None)
892-
.emit(self.infcx.tcx, &mut err);
916+
.emit(self.infcx.tcx, &mut err, String::new());
893917

894918
err.buffer(&mut self.errors_buffer);
895919
}
@@ -1262,21 +1286,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
12621286
}
12631287
}
12641288

1265-
/// Returns the `FakeReadCause` at this location if it is a `FakeRead` statement.
1266-
pub(super) fn retrieve_fake_read_cause_for_location(
1267-
&self,
1268-
location: &Location,
1269-
) -> Option<FakeReadCause> {
1270-
let stmt = self.mir.basic_blocks()[location.block]
1271-
.statements
1272-
.get(location.statement_index)?;
1273-
if let StatementKind::FakeRead(cause, _) = stmt.kind {
1274-
Some(cause)
1275-
} else {
1276-
None
1277-
}
1278-
}
1279-
12801289
fn classify_drop_access_kind(&self, place: &Place<'tcx>) -> StorageDeadOrDrop<'tcx> {
12811290
let tcx = self.infcx.tcx;
12821291
match place {

src/librustc_mir/borrow_check/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx
667667
ref args,
668668
ref destination,
669669
cleanup: _,
670+
from_hir_call: _,
670671
} => {
671672
self.consume_operand(ContextKind::CallOperator.new(loc), (func, span), flow_state);
672673
for arg in args {

0 commit comments

Comments
 (0)