1
1
//! Borrow checker diagnostics.
2
2
3
+ use rustc_const_eval:: util:: call_kind;
3
4
use rustc_errors:: DiagnosticBuilder ;
4
5
use rustc_hir as hir;
5
6
use rustc_hir:: def:: Namespace ;
6
7
use rustc_hir:: def_id:: DefId ;
7
- use rustc_hir:: lang_items:: LangItemGroup ;
8
8
use rustc_hir:: GeneratorKind ;
9
9
use rustc_middle:: mir:: {
10
10
AggregateKind , Constant , FakeReadCause , Field , Local , LocalInfo , LocalKind , Location , Operand ,
@@ -13,7 +13,7 @@ use rustc_middle::mir::{
13
13
use rustc_middle:: ty:: print:: Print ;
14
14
use rustc_middle:: ty:: { self , DefIdTree , Instance , Ty , TyCtxt } ;
15
15
use rustc_mir_dataflow:: move_paths:: { InitLocation , LookupResult } ;
16
- use rustc_span:: { hygiene :: DesugaringKind , symbol:: sym, Span } ;
16
+ use rustc_span:: { symbol:: sym, Span } ;
17
17
use rustc_target:: abi:: VariantIdx ;
18
18
19
19
use super :: borrow_set:: BorrowData ;
@@ -37,7 +37,7 @@ crate use mutability_errors::AccessKind;
37
37
crate use outlives_suggestion:: OutlivesSuggestionBuilder ;
38
38
crate use region_errors:: { ErrorConstraintInfo , RegionErrorKind , RegionErrors } ;
39
39
crate use region_name:: { RegionName , RegionNameSource } ;
40
- use rustc_span :: symbol :: Ident ;
40
+ crate use rustc_const_eval :: util :: CallKind ;
41
41
42
42
pub ( super ) struct IncludingDowncast ( pub ( super ) bool ) ;
43
43
@@ -563,46 +563,23 @@ pub(super) enum UseSpans<'tcx> {
563
563
fn_call_span : Span ,
564
564
/// The definition span of the method being called
565
565
fn_span : Span ,
566
- kind : FnSelfUseKind < ' tcx > ,
566
+ kind : CallKind < ' tcx > ,
567
567
} ,
568
568
/// This access is caused by a `match` or `if let` pattern.
569
569
PatUse ( Span ) ,
570
570
/// This access has a single span associated to it: common case.
571
571
OtherUse ( Span ) ,
572
572
}
573
573
574
- #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
575
- pub ( super ) enum FnSelfUseKind < ' tcx > {
576
- /// A normal method call of the form `receiver.foo(a, b, c)`
577
- Normal {
578
- self_arg : Ident ,
579
- implicit_into_iter : bool ,
580
- /// Whether the self type of the method call has an `.as_ref()` method.
581
- /// Used for better diagnostics.
582
- is_option_or_result : bool ,
583
- } ,
584
- /// A call to `FnOnce::call_once`, desugared from `my_closure(a, b, c)`
585
- FnOnceCall ,
586
- /// A call to an operator trait, desuraged from operator syntax (e.g. `a << b`)
587
- Operator { self_arg : Ident } ,
588
- DerefCoercion {
589
- /// The `Span` of the `Target` associated type
590
- /// in the `Deref` impl we are using.
591
- deref_target : Span ,
592
- /// The type `T::Deref` we are dereferencing to
593
- deref_target_ty : Ty < ' tcx > ,
594
- } ,
595
- }
596
-
597
574
impl UseSpans < ' _ > {
598
575
pub ( super ) fn args_or_use ( self ) -> Span {
599
576
match self {
600
577
UseSpans :: ClosureUse { args_span : span, .. }
601
578
| UseSpans :: PatUse ( span)
602
579
| UseSpans :: OtherUse ( span) => span,
603
- UseSpans :: FnSelfUse {
604
- fn_call_span, kind : FnSelfUseKind :: DerefCoercion { .. } , ..
605
- } => fn_call_span ,
580
+ UseSpans :: FnSelfUse { fn_call_span , kind : CallKind :: DerefCoercion { .. } , .. } => {
581
+ fn_call_span
582
+ }
606
583
UseSpans :: FnSelfUse { var_span, .. } => var_span,
607
584
}
608
585
}
@@ -613,9 +590,9 @@ impl UseSpans<'_> {
613
590
UseSpans :: ClosureUse { path_span : span, .. }
614
591
| UseSpans :: PatUse ( span)
615
592
| UseSpans :: OtherUse ( span) => span,
616
- UseSpans :: FnSelfUse {
617
- fn_call_span, kind : FnSelfUseKind :: DerefCoercion { .. } , ..
618
- } => fn_call_span ,
593
+ UseSpans :: FnSelfUse { fn_call_span , kind : CallKind :: DerefCoercion { .. } , .. } => {
594
+ fn_call_span
595
+ }
619
596
UseSpans :: FnSelfUse { var_span, .. } => var_span,
620
597
}
621
598
}
@@ -626,9 +603,9 @@ impl UseSpans<'_> {
626
603
UseSpans :: ClosureUse { capture_kind_span : span, .. }
627
604
| UseSpans :: PatUse ( span)
628
605
| UseSpans :: OtherUse ( span) => span,
629
- UseSpans :: FnSelfUse {
630
- fn_call_span, kind : FnSelfUseKind :: DerefCoercion { .. } , ..
631
- } => fn_call_span ,
606
+ UseSpans :: FnSelfUse { fn_call_span , kind : CallKind :: DerefCoercion { .. } , .. } => {
607
+ fn_call_span
608
+ }
632
609
UseSpans :: FnSelfUse { var_span, .. } => var_span,
633
610
}
634
611
}
@@ -904,67 +881,19 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
904
881
return normal_ret;
905
882
} ;
906
883
907
- let tcx = self . infcx . tcx ;
908
- let parent = tcx. parent ( method_did) ;
909
- let is_fn_once = parent == tcx. lang_items ( ) . fn_once_trait ( ) ;
910
- let is_operator = !from_hir_call
911
- && parent. map_or ( false , |p| tcx. lang_items ( ) . group ( LangItemGroup :: Op ) . contains ( & p) ) ;
912
- let is_deref = !from_hir_call && tcx. is_diagnostic_item ( sym:: deref_method, method_did) ;
913
- let fn_call_span = * fn_span;
914
-
915
- let self_arg = tcx. fn_arg_names ( method_did) [ 0 ] ;
916
-
917
- debug ! (
918
- "terminator = {:?} from_hir_call={:?}" ,
919
- self . body[ location. block] . terminator, from_hir_call
884
+ let kind = call_kind (
885
+ self . infcx . tcx ,
886
+ self . param_env ,
887
+ method_did,
888
+ method_substs,
889
+ * fn_span,
890
+ * from_hir_call,
891
+ Some ( self . infcx . tcx . fn_arg_names ( method_did) [ 0 ] ) ,
920
892
) ;
921
893
922
- // Check for a 'special' use of 'self' -
923
- // an FnOnce call, an operator (e.g. `<<`), or a
924
- // deref coercion.
925
- let kind = if is_fn_once {
926
- Some ( FnSelfUseKind :: FnOnceCall )
927
- } else if is_operator {
928
- Some ( FnSelfUseKind :: Operator { self_arg } )
929
- } else if is_deref {
930
- let deref_target =
931
- tcx. get_diagnostic_item ( sym:: deref_target) . and_then ( |deref_target| {
932
- Instance :: resolve ( tcx, self . param_env , deref_target, method_substs)
933
- . transpose ( )
934
- } ) ;
935
- if let Some ( Ok ( instance) ) = deref_target {
936
- let deref_target_ty = instance. ty ( tcx, self . param_env ) ;
937
- Some ( FnSelfUseKind :: DerefCoercion {
938
- deref_target : tcx. def_span ( instance. def_id ( ) ) ,
939
- deref_target_ty,
940
- } )
941
- } else {
942
- None
943
- }
944
- } else {
945
- None
946
- } ;
947
-
948
- let kind = kind. unwrap_or_else ( || {
949
- // This isn't a 'special' use of `self`
950
- debug ! ( "move_spans: method_did={:?}, fn_call_span={:?}" , method_did, fn_call_span) ;
951
- let implicit_into_iter = Some ( method_did) == tcx. lang_items ( ) . into_iter_fn ( )
952
- && fn_call_span. desugaring_kind ( ) == Some ( DesugaringKind :: ForLoop ) ;
953
- let parent_self_ty = parent
954
- . filter ( |did| tcx. def_kind ( * did) == rustc_hir:: def:: DefKind :: Impl )
955
- . and_then ( |did| match tcx. type_of ( did) . kind ( ) {
956
- ty:: Adt ( def, ..) => Some ( def. did ) ,
957
- _ => None ,
958
- } ) ;
959
- let is_option_or_result = parent_self_ty. map_or ( false , |def_id| {
960
- matches ! ( tcx. get_diagnostic_name( def_id) , Some ( sym:: Option | sym:: Result ) )
961
- } ) ;
962
- FnSelfUseKind :: Normal { self_arg, implicit_into_iter, is_option_or_result }
963
- } ) ;
964
-
965
894
return FnSelfUse {
966
895
var_span : stmt. source_info . span ,
967
- fn_call_span,
896
+ fn_call_span : * fn_span ,
968
897
fn_span : self
969
898
. infcx
970
899
. tcx
0 commit comments