Skip to content

Commit d78e8a7

Browse files
committed
Fix error message confuses locals and temporaries
1 parent 4651d1e commit d78e8a7

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -347,22 +347,35 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
347347
unreachable!("root_place is an unreachable???")
348348
};
349349

350+
let borrow_span = self.mir.source_info(borrow.location).span;
350351
let proper_span = match *root_place {
351352
Place::Local(local) => self.mir.local_decls[local].source_info.span,
352353
_ => drop_span,
353354
};
354355

355-
let mut err = self.tcx
356-
.path_does_not_live_long_enough(drop_span, "borrowed value", Origin::Mir);
357-
err.span_label(proper_span, "temporary value created here");
358-
err.span_label(drop_span, "temporary value dropped here while still borrowed");
359-
err.note("consider using a `let` binding to increase its lifetime");
356+
match &self.describe_place(&borrow.place) {
357+
Some(description) => {
358+
let mut err = self.tcx.path_does_not_live_long_enough(
359+
borrow_span, &format!("`{}`", description), Origin::Mir);
360+
err.span_label(borrow_span, "does not live long enough");
361+
err.span_label(drop_span, "borrowed value only lives until here");
362+
err.note("borrowed value must be valid for the static lifetime...");
363+
err.emit();
364+
},
365+
None => {
366+
let mut err = self.tcx
367+
.path_does_not_live_long_enough(drop_span, "borrowed value", Origin::Mir);
368+
err.span_label(proper_span, "temporary value created here");
369+
err.span_label(drop_span, "temporary value dropped here while still borrowed");
370+
err.note("consider using a `let` binding to increase its lifetime");
371+
372+
if let Some(end) = end_span {
373+
err.span_label(end, "temporary value needs to live until here");
374+
}
360375

361-
if let Some(end) = end_span {
362-
err.span_label(end, "temporary value needs to live until here");
376+
err.emit();
377+
},
363378
}
364-
365-
err.emit();
366379
}
367380

368381
pub(super) fn report_illegal_mutation_of_borrowed(

src/librustc_mir/borrow_check/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18001800
}
18011801
}
18021802

1803-
18041803
impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18051804
// FIXME (#16118): function intended to allow the borrow checker
18061805
// to be less precise in its handling of Box while still allowing

0 commit comments

Comments
 (0)