@@ -756,7 +756,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
756756 guard : Option < ExprId > ,
757757 opt_match_place : Option < ( Option < & Place < ' tcx > > , Span ) > ,
758758 ) -> Option < SourceScope > {
759- self . visit_primary_bindings (
759+ self . visit_primary_bindings_full (
760760 pattern,
761761 UserTypeProjections :: none ( ) ,
762762 & mut |this, name, mode, var, span, ty, user_ty| {
@@ -850,7 +850,29 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
850850 /// Visit all of the primary bindings in a patterns, that is, visit the
851851 /// leftmost occurrence of each variable bound in a pattern. A variable
852852 /// 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.
853856 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 (
854876 & mut self ,
855877 pattern : & Pat < ' tcx > ,
856878 pattern_user_ty : UserTypeProjections ,
@@ -864,17 +886,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
864886 UserTypeProjections ,
865887 ) ,
866888 ) {
867- debug ! (
868- "visit_primary_bindings: pattern={:?} pattern_user_ty={:?}" ,
869- pattern, pattern_user_ty
870- ) ;
871889 match pattern. kind {
872890 PatKind :: Binding { name, mode, var, ty, ref subpattern, is_primary, .. } => {
873891 if is_primary {
874892 f ( self , name, mode, var, pattern. span , ty, pattern_user_ty. clone ( ) ) ;
875893 }
876894 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) ;
878896 }
879897 }
880898
@@ -883,17 +901,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
883901 let from = u64:: try_from ( prefix. len ( ) ) . unwrap ( ) ;
884902 let to = u64:: try_from ( suffix. len ( ) ) . unwrap ( ) ;
885903 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+ ) ;
887909 }
888910 if let Some ( subpattern) = slice {
889- self . visit_primary_bindings (
911+ self . visit_primary_bindings_full (
890912 subpattern,
891913 pattern_user_ty. clone ( ) . subslice ( from, to) ,
892914 f,
893915 ) ;
894916 }
895917 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+ ) ;
897923 }
898924 }
899925
@@ -904,11 +930,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
904930 | PatKind :: Error ( _) => { }
905931
906932 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) ;
908934 }
909935
910936 PatKind :: DerefPattern { ref subpattern, .. } => {
911- self . visit_primary_bindings ( subpattern, UserTypeProjections :: none ( ) , f) ;
937+ self . visit_primary_bindings_full ( subpattern, UserTypeProjections :: none ( ) , f) ;
912938 }
913939
914940 PatKind :: AscribeUserType {
@@ -926,26 +952,26 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
926952
927953 let base_user_ty = self . canonical_user_type_annotations . push ( annotation. clone ( ) ) ;
928954 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)
930956 }
931957
932958 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)
934960 }
935961
936962 PatKind :: Leaf { ref subpatterns } => {
937963 for subpattern in subpatterns {
938964 let subpattern_user_ty = pattern_user_ty. clone ( ) . leaf ( subpattern. field ) ;
939965 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) ;
941967 }
942968 }
943969
944970 PatKind :: Variant { adt_def, args : _, variant_index, ref subpatterns } => {
945971 for subpattern in subpatterns {
946972 let subpattern_user_ty =
947973 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) ;
949975 }
950976 }
951977 PatKind :: Or { ref pats } => {
@@ -954,7 +980,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
954980 // `let (x | y) = ...`, the primary binding of `y` occurs in
955981 // the right subpattern
956982 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) ;
958984 }
959985 }
960986 }
0 commit comments