Skip to content

Commit

Permalink
Improve span when temporary receiver is dropped in edition 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jan 6, 2025
1 parent b3b368a commit 3560a2b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
34 changes: 34 additions & 0 deletions compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use rustc_middle::mir::{
};
use rustc_middle::ty::adjustment::PointerCoercion;
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
use rustc_middle::util::CallKind;
use rustc_span::{DesugaringKind, Span, Symbol, kw, sym};
use rustc_trait_selection::error_reporting::traits::FindExprBySpan;
use tracing::{debug, instrument};
Expand Down Expand Up @@ -635,6 +636,39 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
// Used in a closure.
(LaterUseKind::ClosureCapture, capture_kind_span, Some(path_span))
}
// In the case that the borrowed value (probably a temporary)
// overlaps with the method's receiver, then point at the method.
UseSpans::FnSelfUse {
var_span: span,
kind: CallKind::Normal { desugaring: None, .. },
..
} if span
.overlaps(self.body.local_decls[borrow.assigned_place.local].source_info.span) =>
{
if let TerminatorKind::Call { func, call_source: CallSource::Normal, .. } =
&self.body.basic_blocks[location.block].terminator().kind
{
// Just point to the function, to reduce the chance of overlapping spans.
let function_span = match func {
Operand::Constant(c) => c.span,
Operand::Copy(place) | Operand::Move(place) => {
if let Some(l) = place.as_local() {
let local_decl = &self.body.local_decls[l];
if self.local_names[l].is_none() {
local_decl.source_info.span
} else {
span
}
} else {
span
}
}
};
(LaterUseKind::Call, function_span, None)
} else {
(LaterUseKind::Other, span, None)
}
}
UseSpans::PatUse(span)
| UseSpans::OtherUse(span)
| UseSpans::FnSelfUse { var_span: span, .. } => {
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/lifetimes/tail-expr-in-nested-expr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ error[E0716]: temporary value dropped while borrowed
--> $DIR/tail-expr-in-nested-expr.rs:4:15
|
LL | let _ = { String::new().as_str() }.len();
| ^^^^^^^^^^^^^---------
| ^^^^^^^^^^^^^ - --- borrow later used by call
| | |
| | temporary value is freed at the end of this statement
| creates a temporary value which is freed while still in use
| borrow later used here
|
= note: consider using a `let` binding to create a longer lived value

Expand Down

0 comments on commit 3560a2b

Please sign in to comment.