11use rustc_middle:: mir:: * ;
22use rustc_middle:: thir:: { self , * } ;
33use rustc_middle:: ty:: { self , Ty , TypeVisitableExt } ;
4- use rustc_span:: Span ;
54
65use crate :: builder:: Builder ;
76use crate :: builder:: expr:: as_place:: { PlaceBase , PlaceBuilder } ;
@@ -56,22 +55,21 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5655 ( ( prefix. len ( ) + suffix. len ( ) ) . try_into ( ) . unwrap ( ) , false )
5756 } ;
5857
59- if self . subslice_work_optimizing ( prefix) && opt_slice . is_none ( ) && suffix . is_empty ( ) {
58+ if self . should_optimize_subslice ( prefix) {
6059 let elem_ty = prefix[ 0 ] . ty ;
6160 let prefix_valtree = self . simplify_const_pattern_slice_into_valtree ( prefix) ;
6261
63- let project = PlaceElem :: Subslice {
62+ let src_path_subslice = place . clone_project ( PlaceElem :: Subslice {
6463 from : 0 as u64 ,
65- to : prefix. len ( ) as u64 , // TODO: look at this
64+ to : prefix. len ( ) as u64 ,
6665 from_end : !exact_size,
67- } ;
66+ } ) ;
6867
6968 let match_pair = self . valtree_to_match_pair (
70- src_path. ty ,
71- src_path. span ,
69+ src_path,
7270 prefix. len ( ) as u64 ,
7371 prefix_valtree,
74- place . clone_project ( project ) ,
72+ src_path_subslice ,
7573 elem_ty,
7674 ) ;
7775
@@ -91,7 +89,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9189 let suffix_len = suffix. len ( ) as u64 ;
9290 let subslice = place. clone_project ( PlaceElem :: Subslice {
9391 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 } ,
9593 from_end : !exact_size,
9694 } ) ;
9795 match_pairs. push ( MatchPairTree :: for_pattern ( subslice, subslice_pat, self ) ) ;
@@ -109,7 +107,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
109107 } ) ) ;
110108 }
111109
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.
113112 subslice. len ( ) > 1 && subslice. iter ( ) . all ( |p| self . is_constant_pattern ( p) )
114113 }
115114
@@ -148,8 +147,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
148147
149148 fn valtree_to_match_pair < ' pat > (
150149 & mut self ,
151- src_pat_ty : Ty < ' tcx > ,
152- span : Span ,
150+ src_path : & ' pat Pat < ' tcx > ,
153151 subslice_len : u64 ,
154152 valtree : ty:: ValTree < ' tcx > ,
155153 place : PlaceBuilder < ' tcx > ,
@@ -162,16 +160,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
162160 Ty :: new_array ( tcx, elem_ty, subslice_len) ,
163161 ) ;
164162
165- let pat_ty = match src_pat_ty . kind ( ) {
163+ let pat_ty = match src_path . ty . kind ( ) {
166164 ty:: Slice ( _) => Ty :: new_slice ( tcx, elem_ty) ,
167165 ty:: Array ( _, _) => Ty :: new_array ( tcx, elem_ty, subslice_len) ,
168166 _ => unreachable ! ( ) ,
169167 } ;
170168
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 ) ;
173171 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+ } ) ;
175177
176178 MatchPairTree {
177179 place : Some ( place. to_place ( self ) ) ,
0 commit comments