@@ -6,13 +6,12 @@ use std::fmt;
6
6
use smallvec:: { smallvec, SmallVec } ;
7
7
8
8
use rustc_data_structures:: captures:: Captures ;
9
- use rustc_middle:: ty:: { self , Ty } ;
10
- use rustc_span:: { Span , DUMMY_SP } ;
9
+ use rustc_middle:: ty:: Ty ;
10
+ use rustc_span:: Span ;
11
11
12
12
use self :: Constructor :: * ;
13
- use self :: SliceKind :: * ;
14
13
15
- use crate :: constructor:: { Constructor , SliceKind } ;
14
+ use crate :: constructor:: { Constructor , Slice , SliceKind } ;
16
15
use crate :: cx:: MatchCheckCtxt ;
17
16
use crate :: usefulness:: PatCtxt ;
18
17
@@ -90,31 +89,25 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
90
89
// We return a wildcard for each field of `other_ctor`.
91
90
pcx. cx . ctor_wildcard_fields ( other_ctor, pcx. ty ) . iter ( ) . collect ( )
92
91
}
93
- ( Slice ( self_slice) , Slice ( other_slice) )
94
- if self_slice. arity ( ) != other_slice. arity ( ) =>
95
- {
96
- // The only tricky case: two slices of different arity. Since `self_slice` covers
97
- // `other_slice`, `self_slice` must be `VarLen`, i.e. of the form
98
- // `[prefix, .., suffix]`. Moreover `other_slice` is guaranteed to have a larger
99
- // arity. So we fill the middle part with enough wildcards to reach the length of
100
- // the new, larger slice.
101
- match self_slice. kind {
102
- FixedLen ( _) => bug ! ( "{:?} doesn't cover {:?}" , self_slice, other_slice) ,
103
- VarLen ( prefix, suffix) => {
104
- let ( ty:: Slice ( inner_ty) | ty:: Array ( inner_ty, _) ) = * self . ty . kind ( ) else {
105
- bug ! ( "bad slice pattern {:?} {:?}" , self . ctor, self . ty) ;
106
- } ;
107
- let prefix = & self . fields [ ..prefix] ;
108
- let suffix = & self . fields [ self_slice. arity ( ) - suffix..] ;
109
- let wildcard: & _ = pcx
110
- . cx
111
- . pattern_arena
112
- . alloc ( DeconstructedPat :: wildcard ( inner_ty, DUMMY_SP ) ) ;
113
- let extra_wildcards = other_slice. arity ( ) - self_slice. arity ( ) ;
114
- let extra_wildcards = ( 0 ..extra_wildcards) . map ( |_| wildcard) ;
115
- prefix. iter ( ) . chain ( extra_wildcards) . chain ( suffix) . collect ( )
116
- }
92
+ (
93
+ & Slice ( self_slice @ Slice { kind : SliceKind :: VarLen ( prefix, suffix) , .. } ) ,
94
+ & Slice ( other_slice) ,
95
+ ) if self_slice. arity ( ) != other_slice. arity ( ) => {
96
+ // The only non-trivial case: two slices of different arity. `other_slice` is
97
+ // guaranteed to have a larger arity, so we fill the middle part with enough
98
+ // wildcards to reach the length of the new, larger slice.
99
+ // Start with a slice of wildcards of the appropriate length.
100
+ let mut fields: SmallVec < [ _ ; 2 ] > =
101
+ pcx. cx . ctor_wildcard_fields ( other_ctor, pcx. ty ) . iter ( ) . collect ( ) ;
102
+ // Fill in the fields from both ends.
103
+ let new_arity = fields. len ( ) ;
104
+ for i in 0 ..prefix {
105
+ fields[ i] = & self . fields [ i] ;
117
106
}
107
+ for i in 0 ..suffix {
108
+ fields[ new_arity - 1 - i] = & self . fields [ self . fields . len ( ) - 1 - i] ;
109
+ }
110
+ fields
118
111
}
119
112
_ => self . fields . iter ( ) . collect ( ) ,
120
113
}
0 commit comments