@@ -14,11 +14,11 @@ use rustc_data_structures::fnv::FnvHashMap;
14
14
use rustc:: middle:: const_eval;
15
15
use rustc:: middle:: def;
16
16
use rustc:: middle:: pat_util:: { pat_is_resolved_const, pat_is_binding} ;
17
- use rustc:: middle:: subst:: Substs ;
18
17
use rustc:: middle:: ty:: { self , Ty } ;
19
18
use rustc:: mir:: repr:: * ;
20
19
use rustc_front:: hir;
21
20
use syntax:: ast;
21
+ use syntax:: codemap:: Span ;
22
22
use syntax:: ptr:: P ;
23
23
24
24
/// When there are multiple patterns in a single arm, each one has its
@@ -40,15 +40,15 @@ struct PatCx<'patcx, 'cx: 'patcx, 'tcx: 'cx> {
40
40
}
41
41
42
42
impl < ' cx , ' tcx > Cx < ' cx , ' tcx > {
43
- pub fn irrefutable_pat ( & mut self , pat : & ' tcx hir:: Pat ) -> Pattern < ' tcx > {
44
- PatCx :: new ( self , None ) . to_pat ( pat)
43
+ pub fn irrefutable_pat ( & mut self , pat : & hir:: Pat ) -> Pattern < ' tcx > {
44
+ PatCx :: new ( self , None ) . to_pattern ( pat)
45
45
}
46
46
47
47
pub fn refutable_pat ( & mut self ,
48
48
binding_map : Option < & FnvHashMap < ast:: Name , ast:: NodeId > > ,
49
- pat : & ' tcx hir:: Pat )
49
+ pat : & hir:: Pat )
50
50
-> Pattern < ' tcx > {
51
- PatCx :: new ( self , binding_map) . to_pat ( pat)
51
+ PatCx :: new ( self , binding_map) . to_pattern ( pat)
52
52
}
53
53
}
54
54
@@ -62,13 +62,12 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
62
62
}
63
63
}
64
64
65
- fn to_pat ( & mut self , pat : & ' tcx hir:: Pat ) -> Pattern < ' tcx > {
65
+ fn to_pattern ( & mut self , pat : & hir:: Pat ) -> Pattern < ' tcx > {
66
66
let kind = match pat. node {
67
67
hir:: PatWild => PatternKind :: Wild ,
68
68
69
69
hir:: PatLit ( ref value) => {
70
70
let value = const_eval:: eval_const_expr ( self . cx . tcx , value) ;
71
- let value = Literal :: Value { value : value } ;
72
71
PatternKind :: Constant { value : value }
73
72
}
74
73
@@ -88,22 +87,9 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
88
87
def:: DefConst ( def_id) | def:: DefAssociatedConst ( def_id) =>
89
88
match const_eval:: lookup_const_by_id ( self . cx . tcx , def_id, Some ( pat. id ) ) {
90
89
Some ( const_expr) => {
91
- let opt_value =
92
- const_eval:: eval_const_expr_partial (
93
- self . cx . tcx , const_expr,
94
- const_eval:: EvalHint :: ExprTypeChecked ,
95
- None ) ;
96
- let literal = if let Ok ( value) = opt_value {
97
- Literal :: Value { value : value }
98
- } else {
99
- let substs = self . cx . tcx . mk_substs ( Substs :: empty ( ) ) ;
100
- Literal :: Item {
101
- def_id : def_id,
102
- kind : ItemKind :: Constant ,
103
- substs : substs
104
- }
105
- } ;
106
- PatternKind :: Constant { value : literal }
90
+ let pat = const_eval:: const_expr_to_pat ( self . cx . tcx , const_expr,
91
+ pat. span ) ;
92
+ return self . to_pattern ( & * pat) ;
107
93
}
108
94
None => {
109
95
self . cx . tcx . sess . span_bug (
@@ -120,7 +106,7 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
120
106
121
107
hir:: PatRegion ( ref subpattern, _) |
122
108
hir:: PatBox ( ref subpattern) => {
123
- PatternKind :: Deref { subpattern : self . to_pat ( subpattern) }
109
+ PatternKind :: Deref { subpattern : self . to_pattern ( subpattern) }
124
110
}
125
111
126
112
hir:: PatVec ( ref prefix, ref slice, ref suffix) => {
@@ -131,14 +117,14 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
131
117
subpattern : Pattern {
132
118
ty : mt. ty ,
133
119
span : pat. span ,
134
- kind : Box :: new ( self . slice_or_array_pattern ( pat, mt. ty , prefix,
120
+ kind : Box :: new ( self . slice_or_array_pattern ( pat. span , mt. ty , prefix,
135
121
slice, suffix) ) ,
136
122
} ,
137
123
} ,
138
124
139
125
ty:: TySlice ( ..) |
140
126
ty:: TyArray ( ..) =>
141
- self . slice_or_array_pattern ( pat, ty, prefix, slice, suffix) ,
127
+ self . slice_or_array_pattern ( pat. span , ty, prefix, slice, suffix) ,
142
128
143
129
ref sty =>
144
130
self . cx . tcx . sess . span_bug (
@@ -153,7 +139,7 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
153
139
. enumerate ( )
154
140
. map ( |( i, subpattern) | FieldPattern {
155
141
field : Field :: new ( i) ,
156
- pattern : self . to_pat ( subpattern) ,
142
+ pattern : self . to_pattern ( subpattern) ,
157
143
} )
158
144
. collect ( ) ;
159
145
@@ -188,7 +174,7 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
188
174
name : ident. node . name ,
189
175
var : id,
190
176
ty : var_ty,
191
- subpattern : self . to_opt_pat ( sub) ,
177
+ subpattern : self . to_opt_pattern ( sub) ,
192
178
}
193
179
}
194
180
@@ -203,7 +189,7 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
203
189
. enumerate ( )
204
190
. map ( |( i, field) | FieldPattern {
205
191
field : Field :: new ( i) ,
206
- pattern : self . to_pat ( field) ,
192
+ pattern : self . to_pattern ( field) ,
207
193
} )
208
194
. collect ( ) ;
209
195
self . variant_or_leaf ( pat, subpatterns)
@@ -234,7 +220,7 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
234
220
} ) ;
235
221
FieldPattern {
236
222
field : Field :: new ( index) ,
237
- pattern : self . to_pat ( & field. node . pat ) ,
223
+ pattern : self . to_pattern ( & field. node . pat ) ,
238
224
}
239
225
} )
240
226
. collect ( ) ;
@@ -256,49 +242,49 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
256
242
}
257
243
}
258
244
259
- fn to_pats ( & mut self , pats : & ' tcx [ P < hir:: Pat > ] ) -> Vec < Pattern < ' tcx > > {
260
- pats. iter ( ) . map ( |p| self . to_pat ( p) ) . collect ( )
245
+ fn to_patterns ( & mut self , pats : & [ P < hir:: Pat > ] ) -> Vec < Pattern < ' tcx > > {
246
+ pats. iter ( ) . map ( |p| self . to_pattern ( p) ) . collect ( )
261
247
}
262
248
263
- fn to_opt_pat ( & mut self , pat : & ' tcx Option < P < hir:: Pat > > ) -> Option < Pattern < ' tcx > > {
264
- pat. as_ref ( ) . map ( |p| self . to_pat ( p) )
249
+ fn to_opt_pattern ( & mut self , pat : & Option < P < hir:: Pat > > ) -> Option < Pattern < ' tcx > > {
250
+ pat. as_ref ( ) . map ( |p| self . to_pattern ( p) )
265
251
}
266
252
267
253
fn slice_or_array_pattern ( & mut self ,
268
- pat : & ' tcx hir :: Pat ,
254
+ span : Span ,
269
255
ty : Ty < ' tcx > ,
270
- prefix : & ' tcx [ P < hir:: Pat > ] ,
271
- slice : & ' tcx Option < P < hir:: Pat > > ,
272
- suffix : & ' tcx [ P < hir:: Pat > ] )
256
+ prefix : & [ P < hir:: Pat > ] ,
257
+ slice : & Option < P < hir:: Pat > > ,
258
+ suffix : & [ P < hir:: Pat > ] )
273
259
-> PatternKind < ' tcx > {
274
260
match ty. sty {
275
261
ty:: TySlice ( ..) => {
276
262
// matching a slice or fixed-length array
277
263
PatternKind :: Slice {
278
- prefix : self . to_pats ( prefix) ,
279
- slice : self . to_opt_pat ( slice) ,
280
- suffix : self . to_pats ( suffix) ,
264
+ prefix : self . to_patterns ( prefix) ,
265
+ slice : self . to_opt_pattern ( slice) ,
266
+ suffix : self . to_patterns ( suffix) ,
281
267
}
282
268
}
283
269
284
270
ty:: TyArray ( _, len) => {
285
271
// fixed-length array
286
272
assert ! ( len >= prefix. len( ) + suffix. len( ) ) ;
287
273
PatternKind :: Array {
288
- prefix : self . to_pats ( prefix) ,
289
- slice : self . to_opt_pat ( slice) ,
290
- suffix : self . to_pats ( suffix) ,
274
+ prefix : self . to_patterns ( prefix) ,
275
+ slice : self . to_opt_pattern ( slice) ,
276
+ suffix : self . to_patterns ( suffix) ,
291
277
}
292
278
}
293
279
294
280
_ => {
295
- self . cx . tcx . sess . span_bug ( pat . span , "unexpanded macro or bad constant etc" ) ;
281
+ self . cx . tcx . sess . span_bug ( span, "unexpanded macro or bad constant etc" ) ;
296
282
}
297
283
}
298
284
}
299
285
300
286
fn variant_or_leaf ( & mut self ,
301
- pat : & ' tcx hir:: Pat ,
287
+ pat : & hir:: Pat ,
302
288
subpatterns : Vec < FieldPattern < ' tcx > > )
303
289
-> PatternKind < ' tcx > {
304
290
let def = self . cx . tcx . def_map . borrow ( ) . get ( & pat. id ) . unwrap ( ) . full_def ( ) ;
0 commit comments