1
1
use rustc_middle:: mir:: * ;
2
2
use rustc_middle:: thir:: { self , * } ;
3
3
use rustc_middle:: ty:: { self , Ty , TypeVisitableExt } ;
4
- use rustc_span:: Span ;
5
4
6
5
use crate :: builder:: Builder ;
7
6
use crate :: builder:: expr:: as_place:: { PlaceBase , PlaceBuilder } ;
@@ -56,22 +55,21 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
56
55
( ( prefix. len ( ) + suffix. len ( ) ) . try_into ( ) . unwrap ( ) , false )
57
56
} ;
58
57
59
- if self . subslice_work_optimizing ( prefix) && opt_slice . is_none ( ) && suffix . is_empty ( ) {
58
+ if self . should_optimize_subslice ( prefix) {
60
59
let elem_ty = prefix[ 0 ] . ty ;
61
60
let prefix_valtree = self . simplify_const_pattern_slice_into_valtree ( prefix) ;
62
61
63
- let project = PlaceElem :: Subslice {
62
+ let src_path_subslice = place . clone_project ( PlaceElem :: Subslice {
64
63
from : 0 as u64 ,
65
- to : prefix. len ( ) as u64 , // TODO: look at this
64
+ to : prefix. len ( ) as u64 ,
66
65
from_end : !exact_size,
67
- } ;
66
+ } ) ;
68
67
69
68
let match_pair = self . valtree_to_match_pair (
70
- src_path. ty ,
71
- src_path. span ,
69
+ src_path,
72
70
prefix. len ( ) as u64 ,
73
71
prefix_valtree,
74
- place . clone_project ( project ) ,
72
+ src_path_subslice ,
75
73
elem_ty,
76
74
) ;
77
75
@@ -91,7 +89,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
91
89
let suffix_len = suffix. len ( ) as u64 ;
92
90
let subslice = place. clone_project ( PlaceElem :: Subslice {
93
91
from : prefix. len ( ) as u64 ,
94
- to : if exact_size { min_length - suffix_len } else { suffix_len } , // TODO: look at this
92
+ to : if exact_size { min_length - suffix_len } else { suffix_len } ,
95
93
from_end : !exact_size,
96
94
} ) ;
97
95
match_pairs. push ( MatchPairTree :: for_pattern ( subslice, subslice_pat, self ) ) ;
@@ -109,7 +107,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
109
107
} ) ) ;
110
108
}
111
109
112
- fn subslice_work_optimizing ( & self , subslice : & [ Box < Pat < ' tcx > > ] ) -> bool {
110
+ fn should_optimize_subslice ( & self , subslice : & [ Box < Pat < ' tcx > > ] ) -> bool {
111
+ // Only wasted effort if we're just comparing a single elememt anyway.
113
112
subslice. len ( ) > 1 && subslice. iter ( ) . all ( |p| self . is_constant_pattern ( p) )
114
113
}
115
114
@@ -148,8 +147,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
148
147
149
148
fn valtree_to_match_pair < ' pat > (
150
149
& mut self ,
151
- src_pat_ty : Ty < ' tcx > ,
152
- span : Span ,
150
+ src_path : & ' pat Pat < ' tcx > ,
153
151
subslice_len : u64 ,
154
152
valtree : ty:: ValTree < ' tcx > ,
155
153
place : PlaceBuilder < ' tcx > ,
@@ -162,16 +160,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
162
160
Ty :: new_array ( tcx, elem_ty, subslice_len) ,
163
161
) ;
164
162
165
- let pat_ty = match src_pat_ty . kind ( ) {
163
+ let pat_ty = match src_path . ty . kind ( ) {
166
164
ty:: Slice ( _) => Ty :: new_slice ( tcx, elem_ty) ,
167
165
ty:: Array ( _, _) => Ty :: new_array ( tcx, elem_ty, subslice_len) ,
168
166
_ => unreachable ! ( ) ,
169
167
} ;
170
168
171
- let ty_cost = ty:: Const :: new ( tcx, ty:: ConstKind :: Value ( const_ty, valtree) ) ;
172
- let value = Const :: Ty ( const_ty, r#ty_cost ) ;
169
+ let ty_const = ty:: Const :: new ( tcx, ty:: ConstKind :: Value ( const_ty, valtree) ) ;
170
+ let value = Const :: Ty ( const_ty, ty_const ) ;
173
171
let test_case = TestCase :: Constant { value } ;
174
- let pattern = tcx. arena . alloc ( Pat { ty : pat_ty, span, kind : PatKind :: Constant { value } } ) ;
172
+ let pattern = tcx. arena . alloc ( Pat {
173
+ ty : pat_ty,
174
+ span : src_path. span ,
175
+ kind : PatKind :: Constant { value } ,
176
+ } ) ;
175
177
176
178
MatchPairTree {
177
179
place : Some ( place. to_place ( self ) ) ,
0 commit comments