@@ -62,7 +62,6 @@ use rustc_span::{Span, DUMMY_SP};
6262use rustc_target:: abi:: { Integer , Size , VariantIdx } ;
6363
6464use smallvec:: { smallvec, SmallVec } ;
65- use std:: borrow:: Cow ;
6665use std:: cmp:: { self , max, min, Ordering } ;
6766use std:: fmt;
6867use std:: iter:: { once, IntoIterator } ;
@@ -1107,33 +1106,27 @@ impl<'tcx> SplitWildcard<'tcx> {
11071106/// because the code mustn't observe that it is uninhabited. In that case that field is not
11081107/// included in `fields`. For that reason, when you have a `mir::Field` you must use
11091108/// `index_with_declared_idx`.
1110- #[ derive( Debug , Clone ) ]
1109+ #[ derive( Debug , Clone , Copy ) ]
11111110pub ( super ) struct Fields < ' p , ' tcx > {
1112- fields : SmallVec < [ & ' p DeconstructedPat < ' p , ' tcx > ; 2 ] > ,
1111+ fields : & ' p [ DeconstructedPat < ' p , ' tcx > ] ,
11131112}
11141113
11151114impl < ' p , ' tcx > Fields < ' p , ' tcx > {
11161115 fn empty ( ) -> Self {
1117- Fields { fields : SmallVec :: new ( ) }
1116+ Fields { fields : & [ ] }
11181117 }
11191118
11201119 fn singleton ( cx : & MatchCheckCtxt < ' p , ' tcx > , field : DeconstructedPat < ' p , ' tcx > ) -> Self {
11211120 let field: & _ = cx. pattern_arena . alloc ( field) ;
1122- Fields { fields : smallvec ! [ field] }
1121+ Fields { fields : std :: slice :: from_ref ( field) }
11231122 }
11241123
11251124 pub ( super ) fn from_iter (
11261125 cx : & MatchCheckCtxt < ' p , ' tcx > ,
11271126 fields : impl IntoIterator < Item = DeconstructedPat < ' p , ' tcx > > ,
11281127 ) -> Self {
1129- let fields: & _ = cx. pattern_arena . alloc_from_iter ( fields) ;
1130- Fields { fields : fields. into_iter ( ) . collect ( ) }
1131- }
1132-
1133- pub ( super ) fn from_ref_iter (
1134- fields : impl IntoIterator < Item = & ' p DeconstructedPat < ' p , ' tcx > > ,
1135- ) -> Self {
1136- Fields { fields : fields. into_iter ( ) . collect ( ) }
1128+ let fields: & [ _ ] = cx. pattern_arena . alloc_from_iter ( fields) ;
1129+ Fields { fields }
11371130 }
11381131
11391132 fn wildcards_from_tys (
@@ -1222,7 +1215,7 @@ impl<'p, 'tcx> Fields<'p, 'tcx> {
12221215 pub ( super ) fn iter_patterns < ' a > (
12231216 & ' a self ,
12241217 ) -> impl Iterator < Item = & ' p DeconstructedPat < ' p , ' tcx > > + Captures < ' a > {
1225- self . fields . iter ( ) . copied ( )
1218+ self . fields . iter ( )
12261219 }
12271220}
12281221
@@ -1245,9 +1238,8 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
12451238
12461239 pub ( crate ) fn from_pat ( cx : & MatchCheckCtxt < ' p , ' tcx > , pat : & Pat < ' tcx > ) -> Self {
12471240 let mkpat = |pat| DeconstructedPat :: from_pat ( cx, pat) ;
1248- let allocpat = |pat| & * cx. pattern_arena . alloc ( mkpat ( pat) ) ;
12491241 let ctor;
1250- let mut fields;
1242+ let fields;
12511243 match pat. kind . as_ref ( ) {
12521244 PatKind :: AscribeUserType { subpattern, .. } => return mkpat ( subpattern) ,
12531245 PatKind :: Binding { subpattern : Some ( subpat) , .. } => return mkpat ( subpat) ,
@@ -1263,10 +1255,15 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
12631255 match pat. ty . kind ( ) {
12641256 ty:: Tuple ( fs) => {
12651257 ctor = Single ;
1266- fields = Fields :: wildcards_from_tys ( cx, fs. iter ( ) . map ( |ty| ty. expect_ty ( ) ) ) ;
1258+ let mut wilds: SmallVec < [ _ ; 2 ] > = fs
1259+ . iter ( )
1260+ . map ( |ty| ty. expect_ty ( ) )
1261+ . map ( DeconstructedPat :: wildcard)
1262+ . collect ( ) ;
12671263 for pat in subpatterns {
1268- fields . fields [ pat. field . index ( ) ] = allocpat ( & pat. pattern ) ;
1264+ wilds [ pat. field . index ( ) ] = mkpat ( & pat. pattern ) ;
12691265 }
1266+ fields = Fields :: from_iter ( cx, wilds) ;
12701267 }
12711268 ty:: Adt ( adt, substs) if adt. is_box ( ) => {
12721269 // The only legal patterns of type `Box` (outside `std`) are `_` and box
@@ -1306,12 +1303,14 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
13061303 field_id_to_id[ field. index ( ) ] = Some ( i) ;
13071304 ty
13081305 } ) ;
1309- fields = Fields :: wildcards_from_tys ( cx, tys) ;
1306+ let mut wilds: SmallVec < [ _ ; 2 ] > =
1307+ tys. map ( DeconstructedPat :: wildcard) . collect ( ) ;
13101308 for pat in subpatterns {
13111309 if let Some ( i) = field_id_to_id[ pat. field . index ( ) ] {
1312- fields . fields [ i] = allocpat ( & pat. pattern ) ;
1310+ wilds [ i] = mkpat ( & pat. pattern ) ;
13131311 }
13141312 }
1313+ fields = Fields :: from_iter ( cx, wilds) ;
13151314 }
13161315 _ => bug ! ( "pattern has unexpected type: pat: {:?}, ty: {:?}" , pat, pat. ty) ,
13171316 }
@@ -1510,40 +1509,38 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
15101509 & ' a self ,
15111510 cx : & MatchCheckCtxt < ' p , ' tcx > ,
15121511 other_ctor : & Constructor < ' tcx > ,
1513- ) -> Cow < ' a , Fields < ' p , ' tcx > > {
1512+ ) -> SmallVec < [ & ' p DeconstructedPat < ' p , ' tcx > ; 2 ] > {
15141513 match ( & self . ctor , other_ctor) {
15151514 ( Wildcard , _) => {
15161515 // We return a wildcard for each field of `other_ctor`.
1517- Cow :: Owned ( Fields :: wildcards ( cx, self . ty , other_ctor) )
1516+ Fields :: wildcards ( cx, self . ty , other_ctor) . iter_patterns ( ) . collect ( )
15181517 }
15191518 ( Slice ( self_slice) , Slice ( other_slice) )
15201519 if self_slice. arity ( ) != other_slice. arity ( ) =>
15211520 {
15221521 // The only tricky case: two slices of different arity. Since `self_slice` covers
15231522 // `other_slice`, `self_slice` must be `VarLen`, i.e. of the form
15241523 // `[prefix, .., suffix]`. Moreover `other_slice` is guaranteed to have a larger
1525- // arity. We fill the middle part with enough wildcards to reach the length of the
1526- // new, larger slice.
1524+ // arity. So we fill the middle part with enough wildcards to reach the length of
1525+ // the new, larger slice.
15271526 match self_slice. kind {
15281527 FixedLen ( _) => bug ! ( "{:?} doesn't cover {:?}" , self_slice, other_slice) ,
15291528 VarLen ( prefix, suffix) => {
15301529 let inner_ty = match * self . ty . kind ( ) {
15311530 ty:: Slice ( ty) | ty:: Array ( ty, _) => ty,
15321531 _ => bug ! ( "bad slice pattern {:?} {:?}" , self . ctor, self . ty) ,
15331532 } ;
1534- let prefix = self . fields . fields [ ..prefix] . iter ( ) . copied ( ) ;
1535- let suffix =
1536- self . fields . fields [ self_slice. arity ( ) - suffix..] . iter ( ) . copied ( ) ;
1533+ let prefix = & self . fields . fields [ ..prefix] ;
1534+ let suffix = & self . fields . fields [ self_slice. arity ( ) - suffix..] ;
1535+ let wildcard: & _ =
1536+ cx. pattern_arena . alloc ( DeconstructedPat :: wildcard ( inner_ty) ) ;
15371537 let extra_wildcards = other_slice. arity ( ) - self_slice. arity ( ) ;
1538- let extra_wildcards: & [ _ ] = cx. pattern_arena . alloc_from_iter (
1539- ( 0 ..extra_wildcards) . map ( |_| DeconstructedPat :: wildcard ( inner_ty) ) ,
1540- ) ;
1541- let fields = prefix. chain ( extra_wildcards) . chain ( suffix) ;
1542- Cow :: Owned ( Fields :: from_ref_iter ( fields) )
1538+ let extra_wildcards = ( 0 ..extra_wildcards) . map ( |_| wildcard) ;
1539+ prefix. iter ( ) . chain ( extra_wildcards) . chain ( suffix) . collect ( )
15431540 }
15441541 }
15451542 }
1546- _ => Cow :: Borrowed ( & self . fields ) ,
1543+ _ => self . fields . iter_patterns ( ) . collect ( ) ,
15471544 }
15481545 }
15491546}
0 commit comments