55//! This also includes code for pattern bindings in `let` statements and
66//! function parameters.
77
8+ use std:: assert_matches:: assert_matches;
9+ use std:: borrow:: Borrow ;
10+ use std:: mem;
11+ use std:: sync:: Arc ;
12+
813use rustc_abi:: VariantIdx ;
914use rustc_data_structures:: fx:: FxIndexMap ;
1015use rustc_data_structures:: stack:: ensure_sufficient_stack;
@@ -19,6 +24,7 @@ use tracing::{debug, instrument};
1924
2025use crate :: builder:: ForGuard :: { self , OutsideGuard , RefWithinGuard } ;
2126use crate :: builder:: expr:: as_place:: PlaceBuilder ;
27+ use crate :: builder:: matches:: user_ty:: ProjectedUserTypesNode ;
2228use crate :: builder:: scope:: DropKind ;
2329use crate :: builder:: {
2430 BlockAnd , BlockAndExtension , Builder , GuardFrame , GuardFrameLocal , LocalsForNode ,
@@ -28,13 +34,9 @@ use crate::builder::{
2834mod match_pair;
2935mod simplify;
3036mod test;
37+ mod user_ty;
3138mod util;
3239
33- use std:: assert_matches:: assert_matches;
34- use std:: borrow:: Borrow ;
35- use std:: mem;
36- use std:: sync:: Arc ;
37-
3840/// Arguments to [`Builder::then_else_break_inner`] that are usually forwarded
3941/// to recursive invocations.
4042#[ derive( Clone , Copy ) ]
@@ -758,7 +760,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
758760 ) -> Option < SourceScope > {
759761 self . visit_primary_bindings_full (
760762 pattern,
761- UserTypeProjections :: none ( ) ,
763+ & ProjectedUserTypesNode :: None ,
762764 & mut |this, name, mode, var, span, ty, user_ty| {
763765 let vis_scope = * visibility_scope
764766 . get_or_insert_with ( || this. new_source_scope ( scope_span, LintLevel :: Inherited ) ) ;
@@ -875,7 +877,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
875877 fn visit_primary_bindings_full (
876878 & mut self ,
877879 pattern : & Pat < ' tcx > ,
878- pattern_user_ty : UserTypeProjections ,
880+ user_tys : & ProjectedUserTypesNode < ' _ > ,
879881 f : & mut impl FnMut (
880882 & mut Self ,
881883 Symbol ,
@@ -889,10 +891,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
889891 match pattern. kind {
890892 PatKind :: Binding { name, mode, var, ty, ref subpattern, is_primary, .. } => {
891893 if is_primary {
892- f ( self , name, mode, var, pattern. span , ty, pattern_user_ty. clone ( ) ) ;
894+ let user_ty_projections = user_tys. build_user_type_projections ( ) ;
895+ f ( self , name, mode, var, pattern. span , ty, user_ty_projections) ;
893896 }
894897 if let Some ( subpattern) = subpattern. as_ref ( ) {
895- self . visit_primary_bindings_full ( subpattern, pattern_user_ty , f) ;
898+ self . visit_primary_bindings_full ( subpattern, user_tys , f) ;
896899 }
897900 }
898901
@@ -901,25 +904,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
901904 let from = u64:: try_from ( prefix. len ( ) ) . unwrap ( ) ;
902905 let to = u64:: try_from ( suffix. len ( ) ) . unwrap ( ) ;
903906 for subpattern in prefix. iter ( ) {
904- self . visit_primary_bindings_full (
905- subpattern,
906- pattern_user_ty. clone ( ) . index ( ) ,
907- f,
908- ) ;
907+ self . visit_primary_bindings_full ( subpattern, & user_tys. index ( ) , f) ;
909908 }
910909 if let Some ( subpattern) = slice {
911- self . visit_primary_bindings_full (
912- subpattern,
913- pattern_user_ty. clone ( ) . subslice ( from, to) ,
914- f,
915- ) ;
910+ self . visit_primary_bindings_full ( subpattern, & user_tys. subslice ( from, to) , f) ;
916911 }
917912 for subpattern in suffix. iter ( ) {
918- self . visit_primary_bindings_full (
919- subpattern,
920- pattern_user_ty. clone ( ) . index ( ) ,
921- f,
922- ) ;
913+ self . visit_primary_bindings_full ( subpattern, & user_tys. index ( ) , f) ;
923914 }
924915 }
925916
@@ -930,11 +921,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
930921 | PatKind :: Error ( _) => { }
931922
932923 PatKind :: Deref { ref subpattern } => {
933- self . visit_primary_bindings_full ( subpattern, pattern_user_ty . deref ( ) , f) ;
924+ self . visit_primary_bindings_full ( subpattern, & user_tys . deref ( ) , f) ;
934925 }
935926
936927 PatKind :: DerefPattern { ref subpattern, .. } => {
937- self . visit_primary_bindings_full ( subpattern, UserTypeProjections :: none ( ) , f) ;
928+ self . visit_primary_bindings_full ( subpattern, & ProjectedUserTypesNode :: None , f) ;
938929 }
939930
940931 PatKind :: AscribeUserType {
@@ -951,27 +942,27 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
951942 // of `user_ty` on any bindings contained with subpattern.
952943
953944 let base_user_ty = self . canonical_user_type_annotations . push ( annotation. clone ( ) ) ;
954- let subpattern_user_ty = pattern_user_ty . push_user_type ( base_user_ty) ;
955- self . visit_primary_bindings_full ( subpattern, subpattern_user_ty , f)
945+ let subpattern_user_tys = user_tys . push_user_type ( base_user_ty) ;
946+ self . visit_primary_bindings_full ( subpattern, & subpattern_user_tys , f)
956947 }
957948
958949 PatKind :: ExpandedConstant { ref subpattern, .. } => {
959- self . visit_primary_bindings_full ( subpattern, pattern_user_ty , f)
950+ self . visit_primary_bindings_full ( subpattern, user_tys , f)
960951 }
961952
962953 PatKind :: Leaf { ref subpatterns } => {
963954 for subpattern in subpatterns {
964- let subpattern_user_ty = pattern_user_ty . clone ( ) . leaf ( subpattern. field ) ;
965- debug ! ( "visit_primary_bindings: subpattern_user_ty={ :?}" , subpattern_user_ty ) ;
966- self . visit_primary_bindings_full ( & subpattern. pattern , subpattern_user_ty , f) ;
955+ let subpattern_user_tys = user_tys . leaf ( subpattern. field ) ;
956+ debug ! ( "visit_primary_bindings: subpattern_user_tys={subpattern_user_tys :?}" ) ;
957+ self . visit_primary_bindings_full ( & subpattern. pattern , & subpattern_user_tys , f) ;
967958 }
968959 }
969960
970961 PatKind :: Variant { adt_def, args : _, variant_index, ref subpatterns } => {
971962 for subpattern in subpatterns {
972- let subpattern_user_ty =
973- pattern_user_ty . clone ( ) . variant ( adt_def, variant_index, subpattern. field ) ;
974- self . visit_primary_bindings_full ( & subpattern. pattern , subpattern_user_ty , f) ;
963+ let subpattern_user_tys =
964+ user_tys . variant ( adt_def, variant_index, subpattern. field ) ;
965+ self . visit_primary_bindings_full ( & subpattern. pattern , & subpattern_user_tys , f) ;
975966 }
976967 }
977968 PatKind :: Or { ref pats } => {
@@ -980,7 +971,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
980971 // `let (x | y) = ...`, the primary binding of `y` occurs in
981972 // the right subpattern
982973 for subpattern in pats. iter ( ) {
983- self . visit_primary_bindings_full ( subpattern, pattern_user_ty . clone ( ) , f) ;
974+ self . visit_primary_bindings_full ( subpattern, user_tys , f) ;
984975 }
985976 }
986977 }
0 commit comments