Fix error message confuses locals and temporaries

This commit is contained in:
David Wood 2017-12-09 11:45:15 +00:00
parent 4651d1e3cf
commit d78e8a730a
No known key found for this signature in database
GPG Key ID: 01760B4F9F53F154
2 changed files with 22 additions and 10 deletions

View File

@ -347,22 +347,35 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
unreachable!("root_place is an unreachable???")
};
let borrow_span = self.mir.source_info(borrow.location).span;
let proper_span = match *root_place {
Place::Local(local) => self.mir.local_decls[local].source_info.span,
_ => drop_span,
};
let mut err = self.tcx
.path_does_not_live_long_enough(drop_span, "borrowed value", Origin::Mir);
err.span_label(proper_span, "temporary value created here");
err.span_label(drop_span, "temporary value dropped here while still borrowed");
err.note("consider using a `let` binding to increase its lifetime");
match &self.describe_place(&borrow.place) {
Some(description) => {
let mut err = self.tcx.path_does_not_live_long_enough(
borrow_span, &format!("`{}`", description), Origin::Mir);
err.span_label(borrow_span, "does not live long enough");
err.span_label(drop_span, "borrowed value only lives until here");
err.note("borrowed value must be valid for the static lifetime...");
err.emit();
},
None => {
let mut err = self.tcx
.path_does_not_live_long_enough(drop_span, "borrowed value", Origin::Mir);
err.span_label(proper_span, "temporary value created here");
err.span_label(drop_span, "temporary value dropped here while still borrowed");
err.note("consider using a `let` binding to increase its lifetime");
if let Some(end) = end_span {
err.span_label(end, "temporary value needs to live until here");
if let Some(end) = end_span {
err.span_label(end, "temporary value needs to live until here");
}
err.emit();
},
}
err.emit();
}
pub(super) fn report_illegal_mutation_of_borrowed(

View File

@ -1800,7 +1800,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
}
}
impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
// FIXME (#16118): function intended to allow the borrow checker
// to be less precise in its handling of Box while still allowing