@@ -9,10 +9,7 @@ use chalk_ir::{
9
9
} ;
10
10
use hir_def:: {
11
11
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 } ,
16
13
lang_item:: LangItem ,
17
14
resolver:: { resolver_for_expr, ResolveValueResult , ValueNs } ,
18
15
DefWithBodyId , FieldId , HasModule , VariantId ,
@@ -28,9 +25,9 @@ use crate::{
28
25
mir:: { BorrowKind , MirSpan , ProjectionElem } ,
29
26
static_lifetime, to_chalk_trait_id,
30
27
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 ,
34
31
} ;
35
32
36
33
use super :: { Expectation , InferenceContext } ;
@@ -488,13 +485,7 @@ impl InferenceContext<'_> {
488
485
if let Some ( initializer) = initializer {
489
486
self . walk_expr ( * initializer) ;
490
487
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) ;
498
489
}
499
490
}
500
491
}
@@ -799,41 +790,37 @@ impl InferenceContext<'_> {
799
790
}
800
791
}
801
792
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 ( ) ;
809
802
match & self . body [ pat] {
810
803
Pat :: Missing | Pat :: Wild => ( ) ,
811
804
Pat :: Tuple { args, ellipsis } => {
812
- pattern_matching_dereference ( & mut ty, & mut bm, & mut place) ;
813
805
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 ) ,
816
808
_ => return ,
817
809
} ;
818
- let fields = subst . iter ( Interner ) . map ( |x| x . assert_ty_ref ( Interner ) ) . enumerate ( ) ;
810
+ let fields = 0 ..field_count ;
819
811
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 {
821
813
let mut p = place. clone ( ) ;
822
814
p. projections . push ( ProjectionElem :: TupleOrClosureField ( i) ) ;
823
- self . consume_with_pat ( p, ty . clone ( ) , bm , * arg) ;
815
+ self . consume_with_pat ( p, * arg) ;
824
816
}
825
817
}
826
818
Pat :: Or ( pats) => {
827
819
for pat in pats. iter ( ) {
828
- self . consume_with_pat ( place. clone ( ) , ty . clone ( ) , bm , * pat) ;
820
+ self . consume_with_pat ( place. clone ( ) , * pat) ;
829
821
}
830
822
}
831
823
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
- } ;
837
824
let Some ( variant) = self . result . variant_resolution_for_pat ( pat) else {
838
825
return ;
839
826
} ;
@@ -843,7 +830,6 @@ impl InferenceContext<'_> {
843
830
}
844
831
VariantId :: StructId ( s) => {
845
832
let vd = & * self . db . struct_data ( s) . variant_data ;
846
- let field_types = self . db . field_types ( variant) ;
847
833
for field_pat in args. iter ( ) {
848
834
let arg = field_pat. pat ;
849
835
let Some ( local_id) = vd. field ( & field_pat. name ) else {
@@ -854,12 +840,7 @@ impl InferenceContext<'_> {
854
840
parent : variant. into ( ) ,
855
841
local_id,
856
842
} ) ) ;
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) ;
863
844
}
864
845
}
865
846
}
@@ -870,26 +851,20 @@ impl InferenceContext<'_> {
870
851
| Pat :: Path ( _)
871
852
| Pat :: Lit ( _) => self . consume_place ( place, pat. into ( ) ) ,
872
853
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 => {
879
857
self . consume_place ( place, pat. into ( ) ) ;
880
858
return ;
881
859
}
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
+ }
884
864
} ;
885
865
self . add_capture ( place, CaptureKind :: ByRef ( capture_kind) , pat. into ( ) ) ;
886
866
}
887
867
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
- } ;
893
868
let Some ( variant) = self . result . variant_resolution_for_pat ( pat) else {
894
869
return ;
895
870
} ;
@@ -903,29 +878,20 @@ impl InferenceContext<'_> {
903
878
let fields = vd. fields ( ) . iter ( ) ;
904
879
let it =
905
880
al. iter ( ) . zip ( fields. clone ( ) ) . chain ( ar. iter ( ) . rev ( ) . zip ( fields. rev ( ) ) ) ;
906
- let field_types = self . db . field_types ( variant) ;
907
881
for ( arg, ( i, _) ) in it {
908
882
let mut p = place. clone ( ) ;
909
883
p. projections . push ( ProjectionElem :: Field ( FieldId {
910
884
parent : variant. into ( ) ,
911
885
local_id : i,
912
886
} ) ) ;
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) ;
919
888
}
920
889
}
921
890
}
922
891
}
923
892
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)
929
895
}
930
896
Pat :: Box { .. } => ( ) , // not supported
931
897
}
@@ -1054,12 +1020,3 @@ fn apply_adjusts_to_place(mut r: HirPlace, adjustments: &[Adjustment]) -> Option
1054
1020
}
1055
1021
Some ( r)
1056
1022
}
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