@@ -17,6 +17,7 @@ use rustc_middle::mir::{
1717} ;
1818use rustc_middle:: ty:: adjustment:: PointerCoercion ;
1919use rustc_middle:: ty:: { self , RegionVid , Ty , TyCtxt } ;
20+ use rustc_middle:: util:: CallKind ;
2021use rustc_span:: { DesugaringKind , Span , Symbol , kw, sym} ;
2122use rustc_trait_selection:: error_reporting:: traits:: FindExprBySpan ;
2223use 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+ if let TerminatorKind :: 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+ if let Some ( l) = place. as_local ( ) {
656+ let local_decl = & self . body . local_decls [ l] ;
657+ if self . 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, .. } => {
0 commit comments