11//! Borrow checker diagnostics.
22
3+ use rustc_const_eval:: util:: call_kind;
34use rustc_errors:: DiagnosticBuilder ;
45use rustc_hir as hir;
56use rustc_hir:: def:: Namespace ;
67use rustc_hir:: def_id:: DefId ;
7- use rustc_hir:: lang_items:: LangItemGroup ;
88use rustc_hir:: GeneratorKind ;
99use rustc_middle:: mir:: {
1010 AggregateKind , Constant , FakeReadCause , Field , Local , LocalInfo , LocalKind , Location , Operand ,
@@ -13,7 +13,7 @@ use rustc_middle::mir::{
1313use rustc_middle:: ty:: print:: Print ;
1414use rustc_middle:: ty:: { self , DefIdTree , Instance , Ty , TyCtxt } ;
1515use rustc_mir_dataflow:: move_paths:: { InitLocation , LookupResult } ;
16- use rustc_span:: { hygiene :: DesugaringKind , symbol:: sym, Span } ;
16+ use rustc_span:: { symbol:: sym, Span } ;
1717use rustc_target:: abi:: VariantIdx ;
1818
1919use super :: borrow_set:: BorrowData ;
@@ -37,7 +37,7 @@ crate use mutability_errors::AccessKind;
3737crate use outlives_suggestion:: OutlivesSuggestionBuilder ;
3838crate use region_errors:: { ErrorConstraintInfo , RegionErrorKind , RegionErrors } ;
3939crate use region_name:: { RegionName , RegionNameSource } ;
40- use rustc_span :: symbol :: Ident ;
40+ crate use rustc_const_eval :: util :: CallKind ;
4141
4242pub ( super ) struct IncludingDowncast ( pub ( super ) bool ) ;
4343
@@ -563,46 +563,23 @@ pub(super) enum UseSpans<'tcx> {
563563 fn_call_span : Span ,
564564 /// The definition span of the method being called
565565 fn_span : Span ,
566- kind : FnSelfUseKind < ' tcx > ,
566+ kind : CallKind < ' tcx > ,
567567 } ,
568568 /// This access is caused by a `match` or `if let` pattern.
569569 PatUse ( Span ) ,
570570 /// This access has a single span associated to it: common case.
571571 OtherUse ( Span ) ,
572572}
573573
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-
597574impl UseSpans < ' _ > {
598575 pub ( super ) fn args_or_use ( self ) -> Span {
599576 match self {
600577 UseSpans :: ClosureUse { args_span : span, .. }
601578 | UseSpans :: PatUse ( span)
602579 | 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+ }
606583 UseSpans :: FnSelfUse { var_span, .. } => var_span,
607584 }
608585 }
@@ -613,9 +590,9 @@ impl UseSpans<'_> {
613590 UseSpans :: ClosureUse { path_span : span, .. }
614591 | UseSpans :: PatUse ( span)
615592 | 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+ }
619596 UseSpans :: FnSelfUse { var_span, .. } => var_span,
620597 }
621598 }
@@ -626,9 +603,9 @@ impl UseSpans<'_> {
626603 UseSpans :: ClosureUse { capture_kind_span : span, .. }
627604 | UseSpans :: PatUse ( span)
628605 | 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+ }
632609 UseSpans :: FnSelfUse { var_span, .. } => var_span,
633610 }
634611 }
@@ -904,67 +881,19 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
904881 return normal_ret;
905882 } ;
906883
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 ] ) ,
920892 ) ;
921893
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-
965894 return FnSelfUse {
966895 var_span : stmt. source_info . span ,
967- fn_call_span,
896+ fn_call_span : * fn_span ,
968897 fn_span : self
969898 . infcx
970899 . tcx
0 commit comments