@@ -9,10 +9,7 @@ use chalk_ir::{
99} ;
1010use hir_def:: {
1111 data:: adt:: VariantData ,
12- hir:: {
13- Array , BinaryOp , BindingAnnotation , BindingId , CaptureBy , Expr , ExprId , Pat , PatId ,
14- Statement , UnaryOp ,
15- } ,
12+ hir:: { Array , BinaryOp , BindingId , CaptureBy , Expr , ExprId , Pat , PatId , Statement , UnaryOp } ,
1613 lang_item:: LangItem ,
1714 resolver:: { resolver_for_expr, ResolveValueResult , ValueNs } ,
1815 DefWithBodyId , FieldId , HasModule , VariantId ,
@@ -28,9 +25,9 @@ use crate::{
2825 mir:: { BorrowKind , MirSpan , ProjectionElem } ,
2926 static_lifetime, to_chalk_trait_id,
3027 traits:: FnTrait ,
31- utils:: { self , generics, pattern_matching_dereference_count , Generics } ,
32- Adjust , Adjustment , Binders , ChalkTraitId , ClosureId , ConstValue , DynTy , FnPointer , FnSig ,
33- Interner , Substitution , Ty , TyExt ,
28+ utils:: { self , generics, Generics } ,
29+ Adjust , Adjustment , Binders , BindingMode , ChalkTraitId , ClosureId , ConstValue , DynTy ,
30+ FnPointer , FnSig , Interner , Substitution , Ty , TyExt ,
3431} ;
3532
3633use super :: { Expectation , InferenceContext } ;
@@ -488,13 +485,7 @@ impl InferenceContext<'_> {
488485 if let Some ( initializer) = initializer {
489486 self . walk_expr ( * initializer) ;
490487 if let Some ( place) = self . place_of_expr ( * initializer) {
491- let ty = self . expr_ty ( * initializer) ;
492- self . consume_with_pat (
493- place,
494- ty,
495- BindingAnnotation :: Unannotated ,
496- * pat,
497- ) ;
488+ self . consume_with_pat ( place, * pat) ;
498489 }
499490 }
500491 }
@@ -799,41 +790,37 @@ impl InferenceContext<'_> {
799790 }
800791 }
801792
802- fn consume_with_pat (
803- & mut self ,
804- mut place : HirPlace ,
805- mut ty : Ty ,
806- mut bm : BindingAnnotation ,
807- pat : PatId ,
808- ) {
793+ fn consume_with_pat ( & mut self , mut place : HirPlace , pat : PatId ) {
794+ let cnt = self . result . pat_adjustments . get ( & pat) . map ( |x| x. len ( ) ) . unwrap_or_default ( ) ;
795+ place. projections = place
796+ . projections
797+ . iter ( )
798+ . cloned ( )
799+ . chain ( ( 0 ..cnt) . map ( |_| ProjectionElem :: Deref ) )
800+ . collect :: < Vec < _ > > ( )
801+ . into ( ) ;
809802 match & self . body [ pat] {
810803 Pat :: Missing | Pat :: Wild => ( ) ,
811804 Pat :: Tuple { args, ellipsis } => {
812- pattern_matching_dereference ( & mut ty, & mut bm, & mut place) ;
813805 let ( al, ar) = args. split_at ( ellipsis. unwrap_or ( args. len ( ) ) ) ;
814- let subst = match ty . kind ( Interner ) {
815- TyKind :: Tuple ( _, s) => s,
806+ let field_count = match self . result [ pat ] . kind ( Interner ) {
807+ TyKind :: Tuple ( _, s) => s. len ( Interner ) ,
816808 _ => return ,
817809 } ;
818- let fields = subst . iter ( Interner ) . map ( |x| x . assert_ty_ref ( Interner ) ) . enumerate ( ) ;
810+ let fields = 0 ..field_count ;
819811 let it = al. iter ( ) . zip ( fields. clone ( ) ) . chain ( ar. iter ( ) . rev ( ) . zip ( fields. rev ( ) ) ) ;
820- for ( arg, ( i , ty ) ) in it {
812+ for ( arg, i ) in it {
821813 let mut p = place. clone ( ) ;
822814 p. projections . push ( ProjectionElem :: TupleOrClosureField ( i) ) ;
823- self . consume_with_pat ( p, ty . clone ( ) , bm , * arg) ;
815+ self . consume_with_pat ( p, * arg) ;
824816 }
825817 }
826818 Pat :: Or ( pats) => {
827819 for pat in pats. iter ( ) {
828- self . consume_with_pat ( place. clone ( ) , ty . clone ( ) , bm , * pat) ;
820+ self . consume_with_pat ( place. clone ( ) , * pat) ;
829821 }
830822 }
831823 Pat :: Record { args, .. } => {
832- pattern_matching_dereference ( & mut ty, & mut bm, & mut place) ;
833- let subst = match ty. kind ( Interner ) {
834- TyKind :: Adt ( _, s) => s,
835- _ => return ,
836- } ;
837824 let Some ( variant) = self . result . variant_resolution_for_pat ( pat) else {
838825 return ;
839826 } ;
@@ -843,7 +830,6 @@ impl InferenceContext<'_> {
843830 }
844831 VariantId :: StructId ( s) => {
845832 let vd = & * self . db . struct_data ( s) . variant_data ;
846- let field_types = self . db . field_types ( variant) ;
847833 for field_pat in args. iter ( ) {
848834 let arg = field_pat. pat ;
849835 let Some ( local_id) = vd. field ( & field_pat. name ) else {
@@ -854,12 +840,7 @@ impl InferenceContext<'_> {
854840 parent : variant. into ( ) ,
855841 local_id,
856842 } ) ) ;
857- self . consume_with_pat (
858- p,
859- field_types[ local_id] . clone ( ) . substitute ( Interner , subst) ,
860- bm,
861- arg,
862- ) ;
843+ self . consume_with_pat ( p, arg) ;
863844 }
864845 }
865846 }
@@ -870,26 +851,20 @@ impl InferenceContext<'_> {
870851 | Pat :: Path ( _)
871852 | Pat :: Lit ( _) => self . consume_place ( place, pat. into ( ) ) ,
872853 Pat :: Bind { id, subpat : _ } => {
873- let mode = self . body . bindings [ * id] . mode ;
874- if matches ! ( mode, BindingAnnotation :: Ref | BindingAnnotation :: RefMut ) {
875- bm = mode;
876- }
877- let capture_kind = match bm {
878- BindingAnnotation :: Unannotated | BindingAnnotation :: Mutable => {
854+ let mode = self . result . binding_modes [ * id] ;
855+ let capture_kind = match mode {
856+ BindingMode :: Move => {
879857 self . consume_place ( place, pat. into ( ) ) ;
880858 return ;
881859 }
882- BindingAnnotation :: Ref => BorrowKind :: Shared ,
883- BindingAnnotation :: RefMut => BorrowKind :: Mut { allow_two_phase_borrow : false } ,
860+ BindingMode :: Ref ( Mutability :: Not ) => BorrowKind :: Shared ,
861+ BindingMode :: Ref ( Mutability :: Mut ) => {
862+ BorrowKind :: Mut { allow_two_phase_borrow : false }
863+ }
884864 } ;
885865 self . add_capture ( place, CaptureKind :: ByRef ( capture_kind) , pat. into ( ) ) ;
886866 }
887867 Pat :: TupleStruct { path : _, args, ellipsis } => {
888- pattern_matching_dereference ( & mut ty, & mut bm, & mut place) ;
889- let subst = match ty. kind ( Interner ) {
890- TyKind :: Adt ( _, s) => s,
891- _ => return ,
892- } ;
893868 let Some ( variant) = self . result . variant_resolution_for_pat ( pat) else {
894869 return ;
895870 } ;
@@ -903,29 +878,20 @@ impl InferenceContext<'_> {
903878 let fields = vd. fields ( ) . iter ( ) ;
904879 let it =
905880 al. iter ( ) . zip ( fields. clone ( ) ) . chain ( ar. iter ( ) . rev ( ) . zip ( fields. rev ( ) ) ) ;
906- let field_types = self . db . field_types ( variant) ;
907881 for ( arg, ( i, _) ) in it {
908882 let mut p = place. clone ( ) ;
909883 p. projections . push ( ProjectionElem :: Field ( FieldId {
910884 parent : variant. into ( ) ,
911885 local_id : i,
912886 } ) ) ;
913- self . consume_with_pat (
914- p,
915- field_types[ i] . clone ( ) . substitute ( Interner , subst) ,
916- bm,
917- * arg,
918- ) ;
887+ self . consume_with_pat ( p, * arg) ;
919888 }
920889 }
921890 }
922891 }
923892 Pat :: Ref { pat, mutability : _ } => {
924- if let Some ( ( inner, _, _) ) = ty. as_reference ( ) {
925- ty = inner. clone ( ) ;
926- place. projections . push ( ProjectionElem :: Deref ) ;
927- self . consume_with_pat ( place, ty, bm, * pat)
928- }
893+ place. projections . push ( ProjectionElem :: Deref ) ;
894+ self . consume_with_pat ( place, * pat)
929895 }
930896 Pat :: Box { .. } => ( ) , // not supported
931897 }
@@ -1054,12 +1020,3 @@ fn apply_adjusts_to_place(mut r: HirPlace, adjustments: &[Adjustment]) -> Option
10541020 }
10551021 Some ( r)
10561022}
1057-
1058- fn pattern_matching_dereference (
1059- cond_ty : & mut Ty ,
1060- binding_mode : & mut BindingAnnotation ,
1061- cond_place : & mut HirPlace ,
1062- ) {
1063- let cnt = pattern_matching_dereference_count ( cond_ty, binding_mode) ;
1064- cond_place. projections . extend ( ( 0 ..cnt) . map ( |_| ProjectionElem :: Deref ) ) ;
1065- }
0 commit comments