@@ -30,16 +30,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
30
30
31
31
// We don't consider an empty subslice as a constant pattern subslice.
32
32
fn is_constant_pattern_subslice ( & self , subslice : & [ Box < Pat < ' tcx > > ] ) -> bool {
33
- ! subslice. is_empty ( ) && subslice. iter ( ) . all ( |p| self . is_constant_pattern ( p) )
33
+ subslice. len ( ) > 1 && subslice. iter ( ) . all ( |p| self . is_constant_pattern ( p) )
34
34
}
35
35
36
36
// TODO: expand beyond u8
37
37
fn is_constant_pattern ( & self , pat : & Pat < ' tcx > ) -> bool {
38
38
if let PatKind :: Constant { value } = pat. kind
39
39
&& let Const :: Ty ( _, const_) = value
40
40
&& let ty:: ConstKind :: Value ( ty, valtree) = const_. kind ( )
41
+ && let ty:: ValTree :: Leaf ( scalar) = valtree
41
42
&& self . tcx . types . u8 == ty
42
- && matches ! ( valtree , ty :: ValTree :: Leaf ( _ ) )
43
+ && scalar . to_u8 ( ) != b'_'
43
44
{
44
45
true
45
46
} else {
@@ -78,7 +79,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
78
79
valtree : ty:: ValTree < ' tcx > ,
79
80
place : PlaceBuilder < ' tcx > ,
80
81
elem_ty : Ty < ' tcx > ,
81
- ) -> MatchPairTree < ' pat , ' tcx > {
82
+ ) -> Option < MatchPairTree < ' pat , ' tcx > > {
82
83
let tcx = self . tcx ;
83
84
let ( const_ty, pat_ty) = if base_pat_ty. is_slice ( ) {
84
85
(
@@ -91,9 +92,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
91
92
)
92
93
} else {
93
94
//let arr_ty = Ty::new_array(tcx, elem_ty, subslice_len);
94
- let arr_ty = Ty :: new_slice ( tcx, elem_ty) ;
95
- let pat_ty = Ty :: new_imm_ref ( tcx, tcx. lifetimes . re_erased , arr_ty) ;
96
- ( arr_ty, pat_ty)
95
+ //let arr_ty = Ty::new_imm_ref(
96
+ // tcx,
97
+ // tcx.lifetimes.re_erased,
98
+ // Ty::new_array(tcx, elem_ty, subslice_len),
99
+ //);
100
+ //let pat_ty = Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, Ty::new_slice(tcx, elem_ty));
101
+ //(arr_ty, pat_ty)
102
+ return None ;
97
103
} ;
98
104
99
105
let r#const = ty:: Const :: new ( tcx, ty:: ConstKind :: Value ( const_ty, valtree) ) ;
@@ -107,12 +113,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
107
113
108
114
let test_case = TestCase :: Constant { value : r#const2 } ;
109
115
110
- MatchPairTree {
116
+ Some ( MatchPairTree {
111
117
place : Some ( place. to_place ( self ) ) ,
112
118
test_case,
113
119
subpairs : Vec :: new ( ) ,
114
120
pattern,
115
- }
121
+ } )
116
122
}
117
123
118
124
/// Builds [`MatchPairTree`] subtrees for the prefix/middle/suffix parts of an
@@ -143,6 +149,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
143
149
( ( prefix. len ( ) + suffix. len ( ) ) . try_into ( ) . unwrap ( ) , false )
144
150
} ;
145
151
152
+ let mut not = false ;
153
+
146
154
// Here we try to special-case constant pattern subslices into valtrees to generate
147
155
// nicer MIR.
148
156
// Try to perform simplification of a constant pattern slice `prefix` sequence into
@@ -151,16 +159,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
151
159
let elem_ty = prefix[ 0 ] . ty ;
152
160
let prefix_valtree = self . simplify_const_pattern_slice_into_valtree ( prefix) ;
153
161
// FIXME(jieyouxu): triple check these place calculations!
154
- let match_pair = self . valtree_to_match_pair (
162
+ if let Some ( match_pair) = self . valtree_to_match_pair (
155
163
base_pat. ty ,
156
164
base_pat. span ,
157
165
prefix. len ( ) as u64 ,
158
166
prefix_valtree,
159
167
place. base ( ) . into ( ) ,
160
168
elem_ty,
161
- ) ;
162
- match_pairs. push ( match_pair) ;
163
- } else {
169
+ ) {
170
+ match_pairs. push ( match_pair) ;
171
+ not = true ;
172
+ }
173
+ }
174
+
175
+ if !not {
164
176
match_pairs. extend ( prefix. iter ( ) . enumerate ( ) . map ( |( idx, subpattern) | {
165
177
let elem =
166
178
ProjectionElem :: ConstantIndex { offset : idx as u64 , min_length, from_end : false } ;
0 commit comments