@@ -756,7 +756,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
756
756
guard : Option < ExprId > ,
757
757
opt_match_place : Option < ( Option < & Place < ' tcx > > , Span ) > ,
758
758
) -> Option < SourceScope > {
759
- self . visit_primary_bindings (
759
+ self . visit_primary_bindings_full (
760
760
pattern,
761
761
UserTypeProjections :: none ( ) ,
762
762
& mut |this, name, mode, var, span, ty, user_ty| {
@@ -850,7 +850,29 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
850
850
/// Visit all of the primary bindings in a patterns, that is, visit the
851
851
/// leftmost occurrence of each variable bound in a pattern. A variable
852
852
/// will occur more than once in an or-pattern.
853
+ ///
854
+ /// This variant provides only the limited subset of callback data needed
855
+ /// by its callers.
853
856
pub ( super ) fn visit_primary_bindings (
857
+ & mut self ,
858
+ pattern : & Pat < ' tcx > ,
859
+ f : & mut impl FnMut ( & mut Self , LocalVarId , Span ) ,
860
+ ) {
861
+ pattern. walk_always ( |pat| {
862
+ if let PatKind :: Binding { var, is_primary : true , .. } = pat. kind {
863
+ f ( self , var, pat. span ) ;
864
+ }
865
+ } )
866
+ }
867
+
868
+ /// Visit all of the primary bindings in a patterns, that is, visit the
869
+ /// leftmost occurrence of each variable bound in a pattern. A variable
870
+ /// will occur more than once in an or-pattern.
871
+ ///
872
+ /// This variant provides all of the extra callback data needed by
873
+ /// [`Self::declare_bindings`], including user-type projections.
874
+ #[ instrument( level = "debug" , skip( self , f) ) ]
875
+ fn visit_primary_bindings_full (
854
876
& mut self ,
855
877
pattern : & Pat < ' tcx > ,
856
878
pattern_user_ty : UserTypeProjections ,
@@ -864,17 +886,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
864
886
UserTypeProjections ,
865
887
) ,
866
888
) {
867
- debug ! (
868
- "visit_primary_bindings: pattern={:?} pattern_user_ty={:?}" ,
869
- pattern, pattern_user_ty
870
- ) ;
871
889
match pattern. kind {
872
890
PatKind :: Binding { name, mode, var, ty, ref subpattern, is_primary, .. } => {
873
891
if is_primary {
874
892
f ( self , name, mode, var, pattern. span , ty, pattern_user_ty. clone ( ) ) ;
875
893
}
876
894
if let Some ( subpattern) = subpattern. as_ref ( ) {
877
- self . visit_primary_bindings ( subpattern, pattern_user_ty, f) ;
895
+ self . visit_primary_bindings_full ( subpattern, pattern_user_ty, f) ;
878
896
}
879
897
}
880
898
@@ -883,17 +901,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
883
901
let from = u64:: try_from ( prefix. len ( ) ) . unwrap ( ) ;
884
902
let to = u64:: try_from ( suffix. len ( ) ) . unwrap ( ) ;
885
903
for subpattern in prefix. iter ( ) {
886
- self . visit_primary_bindings ( subpattern, pattern_user_ty. clone ( ) . index ( ) , f) ;
904
+ self . visit_primary_bindings_full (
905
+ subpattern,
906
+ pattern_user_ty. clone ( ) . index ( ) ,
907
+ f,
908
+ ) ;
887
909
}
888
910
if let Some ( subpattern) = slice {
889
- self . visit_primary_bindings (
911
+ self . visit_primary_bindings_full (
890
912
subpattern,
891
913
pattern_user_ty. clone ( ) . subslice ( from, to) ,
892
914
f,
893
915
) ;
894
916
}
895
917
for subpattern in suffix. iter ( ) {
896
- self . visit_primary_bindings ( subpattern, pattern_user_ty. clone ( ) . index ( ) , f) ;
918
+ self . visit_primary_bindings_full (
919
+ subpattern,
920
+ pattern_user_ty. clone ( ) . index ( ) ,
921
+ f,
922
+ ) ;
897
923
}
898
924
}
899
925
@@ -904,11 +930,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
904
930
| PatKind :: Error ( _) => { }
905
931
906
932
PatKind :: Deref { ref subpattern } => {
907
- self . visit_primary_bindings ( subpattern, pattern_user_ty. deref ( ) , f) ;
933
+ self . visit_primary_bindings_full ( subpattern, pattern_user_ty. deref ( ) , f) ;
908
934
}
909
935
910
936
PatKind :: DerefPattern { ref subpattern, .. } => {
911
- self . visit_primary_bindings ( subpattern, UserTypeProjections :: none ( ) , f) ;
937
+ self . visit_primary_bindings_full ( subpattern, UserTypeProjections :: none ( ) , f) ;
912
938
}
913
939
914
940
PatKind :: AscribeUserType {
@@ -926,26 +952,26 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
926
952
927
953
let base_user_ty = self . canonical_user_type_annotations . push ( annotation. clone ( ) ) ;
928
954
let subpattern_user_ty = pattern_user_ty. push_user_type ( base_user_ty) ;
929
- self . visit_primary_bindings ( subpattern, subpattern_user_ty, f)
955
+ self . visit_primary_bindings_full ( subpattern, subpattern_user_ty, f)
930
956
}
931
957
932
958
PatKind :: ExpandedConstant { ref subpattern, .. } => {
933
- self . visit_primary_bindings ( subpattern, pattern_user_ty, f)
959
+ self . visit_primary_bindings_full ( subpattern, pattern_user_ty, f)
934
960
}
935
961
936
962
PatKind :: Leaf { ref subpatterns } => {
937
963
for subpattern in subpatterns {
938
964
let subpattern_user_ty = pattern_user_ty. clone ( ) . leaf ( subpattern. field ) ;
939
965
debug ! ( "visit_primary_bindings: subpattern_user_ty={:?}" , subpattern_user_ty) ;
940
- self . visit_primary_bindings ( & subpattern. pattern , subpattern_user_ty, f) ;
966
+ self . visit_primary_bindings_full ( & subpattern. pattern , subpattern_user_ty, f) ;
941
967
}
942
968
}
943
969
944
970
PatKind :: Variant { adt_def, args : _, variant_index, ref subpatterns } => {
945
971
for subpattern in subpatterns {
946
972
let subpattern_user_ty =
947
973
pattern_user_ty. clone ( ) . variant ( adt_def, variant_index, subpattern. field ) ;
948
- self . visit_primary_bindings ( & subpattern. pattern , subpattern_user_ty, f) ;
974
+ self . visit_primary_bindings_full ( & subpattern. pattern , subpattern_user_ty, f) ;
949
975
}
950
976
}
951
977
PatKind :: Or { ref pats } => {
@@ -954,7 +980,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
954
980
// `let (x | y) = ...`, the primary binding of `y` occurs in
955
981
// the right subpattern
956
982
for subpattern in pats. iter ( ) {
957
- self . visit_primary_bindings ( subpattern, pattern_user_ty. clone ( ) , f) ;
983
+ self . visit_primary_bindings_full ( subpattern, pattern_user_ty. clone ( ) , f) ;
958
984
}
959
985
}
960
986
}
0 commit comments