@@ -20,7 +20,7 @@ use rustc_middle::bug;
20
20
use rustc_middle:: hir:: nested_filter:: OnlyBodies ;
21
21
use rustc_middle:: mir:: tcx:: PlaceTy ;
22
22
use rustc_middle:: mir:: {
23
- self , AggregateKind , BindingForm , BorrowKind , CallSource , ClearCrossCrate , ConstraintCategory ,
23
+ self , AggregateKind , BindingForm , BorrowKind , ClearCrossCrate , ConstraintCategory ,
24
24
FakeBorrowKind , FakeReadCause , LocalDecl , LocalInfo , LocalKind , Location , MutBorrowKind ,
25
25
Operand , Place , PlaceRef , ProjectionElem , Rvalue , Statement , StatementKind , Terminator ,
26
26
TerminatorKind , VarBindingForm , VarDebugInfoContents ,
@@ -30,13 +30,13 @@ use rustc_middle::ty::{
30
30
self , PredicateKind , Ty , TyCtxt , TypeSuperVisitable , TypeVisitor , Upcast ,
31
31
suggest_constraining_type_params,
32
32
} ;
33
- use rustc_middle:: util:: CallKind ;
34
33
use rustc_mir_dataflow:: move_paths:: { InitKind , MoveOutIndex , MovePathIndex } ;
35
34
use rustc_span:: def_id:: { DefId , LocalDefId } ;
36
35
use rustc_span:: hygiene:: DesugaringKind ;
37
36
use rustc_span:: { BytePos , Ident , Span , Symbol , kw, sym} ;
38
37
use rustc_trait_selection:: error_reporting:: InferCtxtErrorExt ;
39
38
use rustc_trait_selection:: error_reporting:: traits:: FindExprBySpan ;
39
+ use rustc_trait_selection:: error_reporting:: traits:: call_kind:: CallKind ;
40
40
use rustc_trait_selection:: infer:: InferCtxtExt ;
41
41
use rustc_trait_selection:: traits:: query:: evaluate_obligation:: InferCtxtExt as _;
42
42
use rustc_trait_selection:: traits:: { Obligation , ObligationCause , ObligationCtxt } ;
@@ -46,7 +46,7 @@ use super::explain_borrow::{BorrowExplanation, LaterUseKind};
46
46
use super :: { DescribePlaceOpt , RegionName , RegionNameSource , UseSpans } ;
47
47
use crate :: borrow_set:: { BorrowData , TwoPhaseActivation } ;
48
48
use 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} ;
50
50
use crate :: prefixes:: IsPrefixOf ;
51
51
use crate :: { InitializationRequiringAction , MirBorrowckCtxt , WriteKind , borrowck_errors} ;
52
52
@@ -305,7 +305,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
305
305
}
306
306
307
307
if let UseSpans :: FnSelfUse {
308
- kind : CallKind :: DerefCoercion { deref_target , deref_target_ty, .. } ,
308
+ kind : CallKind :: DerefCoercion { deref_target_span , deref_target_ty, .. } ,
309
309
..
310
310
} = use_spans
311
311
{
@@ -315,8 +315,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
315
315
) ) ;
316
316
317
317
// 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" ) ;
320
322
}
321
323
}
322
324
@@ -3765,38 +3767,27 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
3765
3767
3766
3768
fn explain_deref_coercion ( & mut self , loan : & BorrowData < ' tcx > , err : & mut Diag < ' _ > ) {
3767
3769
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 (
3777
3773
tcx,
3778
3774
self . body ,
3779
3775
loan. assigned_place . local ,
3780
3776
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" ) ;
3800
3791
}
3801
3792
}
3802
3793
}
0 commit comments