@@ -20,7 +20,7 @@ use rustc_middle::bug;
2020use rustc_middle:: hir:: nested_filter:: OnlyBodies ;
2121use rustc_middle:: mir:: tcx:: PlaceTy ;
2222use rustc_middle:: mir:: {
23- self , AggregateKind , BindingForm , BorrowKind , CallSource , ClearCrossCrate , ConstraintCategory ,
23+ self , AggregateKind , BindingForm , BorrowKind , ClearCrossCrate , ConstraintCategory ,
2424 FakeBorrowKind , FakeReadCause , LocalDecl , LocalInfo , LocalKind , Location , MutBorrowKind ,
2525 Operand , Place , PlaceRef , ProjectionElem , Rvalue , Statement , StatementKind , Terminator ,
2626 TerminatorKind , VarBindingForm , VarDebugInfoContents ,
@@ -30,13 +30,13 @@ use rustc_middle::ty::{
3030 self , PredicateKind , Ty , TyCtxt , TypeSuperVisitable , TypeVisitor , Upcast ,
3131 suggest_constraining_type_params,
3232} ;
33- use rustc_middle:: util:: CallKind ;
3433use rustc_mir_dataflow:: move_paths:: { InitKind , MoveOutIndex , MovePathIndex } ;
3534use rustc_span:: def_id:: { DefId , LocalDefId } ;
3635use rustc_span:: hygiene:: DesugaringKind ;
3736use rustc_span:: { BytePos , Ident , Span , Symbol , kw, sym} ;
3837use rustc_trait_selection:: error_reporting:: InferCtxtErrorExt ;
3938use rustc_trait_selection:: error_reporting:: traits:: FindExprBySpan ;
39+ use rustc_trait_selection:: error_reporting:: traits:: call_kind:: CallKind ;
4040use rustc_trait_selection:: infer:: InferCtxtExt ;
4141use rustc_trait_selection:: traits:: query:: evaluate_obligation:: InferCtxtExt as _;
4242use rustc_trait_selection:: traits:: { Obligation , ObligationCause , ObligationCtxt } ;
@@ -46,7 +46,7 @@ use super::explain_borrow::{BorrowExplanation, LaterUseKind};
4646use super :: { DescribePlaceOpt , RegionName , RegionNameSource , UseSpans } ;
4747use crate :: borrow_set:: { BorrowData , TwoPhaseActivation } ;
4848use crate :: diagnostics:: conflict_errors:: StorageDeadOrDrop :: LocalStorageDead ;
49- use crate :: diagnostics:: { CapturedMessageOpt , Instance , find_all_local_uses} ;
49+ use crate :: diagnostics:: { CapturedMessageOpt , call_kind , find_all_local_uses} ;
5050use crate :: prefixes:: IsPrefixOf ;
5151use crate :: { InitializationRequiringAction , MirBorrowckCtxt , WriteKind , borrowck_errors} ;
5252
@@ -305,7 +305,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
305305 }
306306
307307 if let UseSpans :: FnSelfUse {
308- kind : CallKind :: DerefCoercion { deref_target , deref_target_ty, .. } ,
308+ kind : CallKind :: DerefCoercion { deref_target_span , deref_target_ty, .. } ,
309309 ..
310310 } = use_spans
311311 {
@@ -315,8 +315,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
315315 ) ) ;
316316
317317 // Check first whether the source is accessible (issue #87060)
318- if self . infcx . tcx . sess . source_map ( ) . is_span_accessible ( deref_target) {
319- err. span_note ( deref_target, "deref defined here" ) ;
318+ if let Some ( deref_target_span) = deref_target_span
319+ && self . infcx . tcx . sess . source_map ( ) . is_span_accessible ( deref_target_span)
320+ {
321+ err. span_note ( deref_target_span, "deref defined here" ) ;
320322 }
321323 }
322324
@@ -3765,38 +3767,27 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
37653767
37663768 fn explain_deref_coercion ( & mut self , loan : & BorrowData < ' tcx > , err : & mut Diag < ' _ > ) {
37673769 let tcx = self . infcx . tcx ;
3768- if let (
3769- Some ( Terminator {
3770- kind : TerminatorKind :: Call { call_source : CallSource :: OverloadedOperator , .. } ,
3771- ..
3772- } ) ,
3773- Some ( ( method_did, method_args) ) ,
3774- ) = (
3775- & self . body [ loan. reserve_location . block ] . terminator ,
3776- rustc_middle:: util:: find_self_call (
3770+ if let Some ( Terminator { kind : TerminatorKind :: Call { call_source, fn_span, .. } , .. } ) =
3771+ & self . body [ loan. reserve_location . block ] . terminator
3772+ && let Some ( ( method_did, method_args) ) = rustc_middle:: util:: find_self_call (
37773773 tcx,
37783774 self . body ,
37793775 loan. assigned_place . local ,
37803776 loan. reserve_location . block ,
3781- ) ,
3782- ) {
3783- if tcx. is_diagnostic_item ( sym:: deref_method, method_did) {
3784- let deref_target =
3785- tcx. get_diagnostic_item ( sym:: deref_target) . and_then ( |deref_target| {
3786- Instance :: try_resolve (
3787- tcx,
3788- self . infcx . typing_env ( self . infcx . param_env ) ,
3789- deref_target,
3790- method_args,
3791- )
3792- . transpose ( )
3793- } ) ;
3794- if let Some ( Ok ( instance) ) = deref_target {
3795- let deref_target_ty =
3796- instance. ty ( tcx, self . infcx . typing_env ( self . infcx . param_env ) ) ;
3797- err. note ( format ! ( "borrow occurs due to deref coercion to `{deref_target_ty}`" ) ) ;
3798- err. span_note ( tcx. def_span ( instance. def_id ( ) ) , "deref defined here" ) ;
3799- }
3777+ )
3778+ && let CallKind :: DerefCoercion { deref_target_span, deref_target_ty, .. } = call_kind (
3779+ self . infcx . tcx ,
3780+ self . infcx . typing_env ( self . infcx . param_env ) ,
3781+ method_did,
3782+ method_args,
3783+ * fn_span,
3784+ call_source. from_hir_call ( ) ,
3785+ Some ( self . infcx . tcx . fn_arg_names ( method_did) [ 0 ] ) ,
3786+ )
3787+ {
3788+ err. note ( format ! ( "borrow occurs due to deref coercion to `{deref_target_ty}`" ) ) ;
3789+ if let Some ( deref_target_span) = deref_target_span {
3790+ err. span_note ( deref_target_span, "deref defined here" ) ;
38003791 }
38013792 }
38023793 }
0 commit comments