@@ -6,13 +6,12 @@ use std::fmt;
66use smallvec:: { smallvec, SmallVec } ;
77
88use 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 ;
1111
1212use self :: Constructor :: * ;
13- use self :: SliceKind :: * ;
1413
15- use crate :: constructor:: { Constructor , SliceKind } ;
14+ use crate :: constructor:: { Constructor , Slice , SliceKind } ;
1615use crate :: cx:: MatchCheckCtxt ;
1716use crate :: usefulness:: PatCtxt ;
1817
@@ -90,31 +89,25 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
9089 // We return a wildcard for each field of `other_ctor`.
9190 pcx. cx . ctor_wildcard_fields ( other_ctor, pcx. ty ) . iter ( ) . collect ( )
9291 }
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] ;
117106 }
107+ for i in 0 ..suffix {
108+ fields[ new_arity - 1 - i] = & self . fields [ self . fields . len ( ) - 1 - i] ;
109+ }
110+ fields
118111 }
119112 _ => self . fields . iter ( ) . collect ( ) ,
120113 }
0 commit comments