Skip to content

Commit 3560a2b

Browse files
Improve span when temporary receiver is dropped in edition 2024
1 parent b3b368a commit 3560a2b

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_middle::mir::{
1717
};
1818
use rustc_middle::ty::adjustment::PointerCoercion;
1919
use rustc_middle::ty::{self,RegionVid,Ty,TyCtxt};
20+
use rustc_middle::util::CallKind;
2021
use rustc_span::{DesugaringKind,Span,Symbol, kw, sym};
2122
use rustc_trait_selection::error_reporting::traits::FindExprBySpan;
2223
use tracing::{debug, instrument};
@@ -635,6 +636,39 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
635636
// Used in a closure.
636637
(LaterUseKind::ClosureCapture, capture_kind_span,Some(path_span))
637638
}
639+
// In the case that the borrowed value (probably a temporary)
640+
// overlaps with the method's receiver, then point at the method.
641+
UseSpans::FnSelfUse{
642+
var_span: span,
643+
kind:CallKind::Normal{desugaring:None, .. },
644+
..
645+
}if span
646+
.overlaps(self.body.local_decls[borrow.assigned_place.local].source_info.span) =>
647+
{
648+
ifletTerminatorKind::Call{ func,call_source:CallSource::Normal, .. } =
649+
&self.body.basic_blocks[location.block].terminator().kind
650+
{
651+
// Just point to the function, to reduce the chance of overlapping spans.
652+
let function_span = match func {
653+
Operand::Constant(c) => c.span,
654+
Operand::Copy(place) | Operand::Move(place) => {
655+
ifletSome(l) = place.as_local(){
656+
let local_decl = &self.body.local_decls[l];
657+
ifself.local_names[l].is_none(){
658+
local_decl.source_info.span
659+
}else{
660+
span
661+
}
662+
}else{
663+
span
664+
}
665+
}
666+
};
667+
(LaterUseKind::Call, function_span,None)
668+
}else{
669+
(LaterUseKind::Other, span,None)
670+
}
671+
}
638672
UseSpans::PatUse(span)
639673
| UseSpans::OtherUse(span)
640674
| UseSpans::FnSelfUse{var_span: span, .. } => {

‎tests/ui/lifetimes/tail-expr-in-nested-expr.stderr‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ error[E0716]: temporary value dropped while borrowed
22
--> $DIR/tail-expr-in-nested-expr.rs:4:15
33
|
44
LL | let _ = { String::new().as_str() }.len();
5-
| ^^^^^^^^^^^^^---------
5+
| ^^^^^^^^^^^^^ - --- borrow later used by call
66
| | |
77
| | temporary value is freed at the end of this statement
88
| creates a temporary value which is freed while still in use
9-
| borrow later used here
109
|
1110
= note: consider using a `let` binding to create a longer lived value
1211

0 commit comments

Comments
 (0)