@@ -18,24 +18,20 @@ use self::EvalHint::*;
18
18
use rustc:: hir:: map as ast_map;
19
19
use rustc:: hir:: map:: blocks:: FnLikeNode ;
20
20
use rustc:: traits;
21
- use rustc:: hir:: def:: { Def , CtorKind } ;
21
+ use rustc:: hir:: def:: Def ;
22
22
use rustc:: hir:: def_id:: DefId ;
23
23
use rustc:: ty:: { self , Ty , TyCtxt } ;
24
24
use rustc:: ty:: util:: IntTypeExt ;
25
25
use rustc:: ty:: subst:: Substs ;
26
26
use rustc:: traits:: Reveal ;
27
27
use rustc:: util:: common:: ErrorReported ;
28
28
use rustc:: util:: nodemap:: DefIdMap ;
29
- use rustc:: lint;
30
29
31
30
use graphviz:: IntoCow ;
32
31
use syntax:: ast;
33
- use rustc:: hir:: { Expr , PatKind } ;
34
- use rustc:: hir;
35
- use syntax:: ptr:: P ;
36
- use syntax:: codemap;
32
+ use rustc:: hir:: { self , Expr } ;
37
33
use syntax:: attr:: IntType ;
38
- use syntax_pos:: { self , Span } ;
34
+ use syntax_pos:: Span ;
39
35
40
36
use std:: borrow:: Cow ;
41
37
use std:: cmp:: Ordering ;
@@ -186,126 +182,6 @@ fn lookup_const_fn_by_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
186
182
}
187
183
}
188
184
189
- pub fn const_expr_to_pat < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
190
- expr : & Expr ,
191
- pat_id : ast:: NodeId ,
192
- span : Span )
193
- -> Result < P < hir:: Pat > , DefId > {
194
- let pat_ty = tcx. tables ( ) . expr_ty ( expr) ;
195
- debug ! ( "expr={:?} pat_ty={:?} pat_id={}" , expr, pat_ty, pat_id) ;
196
- match pat_ty. sty {
197
- ty:: TyFloat ( _) => {
198
- tcx. sess . add_lint (
199
- lint:: builtin:: ILLEGAL_FLOATING_POINT_CONSTANT_PATTERN ,
200
- pat_id,
201
- span,
202
- format ! ( "floating point constants cannot be used in patterns" ) ) ;
203
- }
204
- ty:: TyAdt ( adt_def, _) if adt_def. is_union ( ) => {
205
- // Matching on union fields is unsafe, we can't hide it in constants
206
- tcx. sess . span_err ( span, "cannot use unions in constant patterns" ) ;
207
- }
208
- ty:: TyAdt ( adt_def, _) => {
209
- if !tcx. has_attr ( adt_def. did , "structural_match" ) {
210
- tcx. sess . add_lint (
211
- lint:: builtin:: ILLEGAL_STRUCT_OR_ENUM_CONSTANT_PATTERN ,
212
- pat_id,
213
- span,
214
- format ! ( "to use a constant of type `{}` \
215
- in a pattern, \
216
- `{}` must be annotated with `#[derive(PartialEq, Eq)]`",
217
- tcx. item_path_str( adt_def. did) ,
218
- tcx. item_path_str( adt_def. did) ) ) ;
219
- }
220
- }
221
- _ => { }
222
- }
223
- let pat = match expr. node {
224
- hir:: ExprTup ( ref exprs) =>
225
- PatKind :: Tuple ( exprs. iter ( )
226
- . map ( |expr| const_expr_to_pat ( tcx, & expr, pat_id, span) )
227
- . collect :: < Result < _ , _ > > ( ) ?, None ) ,
228
-
229
- hir:: ExprCall ( ref callee, ref args) => {
230
- let qpath = match callee. node {
231
- hir:: ExprPath ( ref qpath) => qpath,
232
- _ => bug ! ( )
233
- } ;
234
- let def = tcx. tables ( ) . qpath_def ( qpath, callee. id ) ;
235
- let ctor_path = if let hir:: QPath :: Resolved ( _, ref path) = * qpath {
236
- match def {
237
- Def :: StructCtor ( _, CtorKind :: Fn ) |
238
- Def :: VariantCtor ( _, CtorKind :: Fn ) => {
239
- Some ( path. clone ( ) )
240
- }
241
- _ => None
242
- }
243
- } else {
244
- None
245
- } ;
246
- match ( def, ctor_path) {
247
- ( Def :: Fn ( ..) , None ) | ( Def :: Method ( ..) , None ) => {
248
- PatKind :: Lit ( P ( expr. clone ( ) ) )
249
- }
250
- ( _, Some ( ctor_path) ) => {
251
- let pats = args. iter ( )
252
- . map ( |expr| const_expr_to_pat ( tcx, expr, pat_id, span) )
253
- . collect :: < Result < _ , _ > > ( ) ?;
254
- PatKind :: TupleStruct ( hir:: QPath :: Resolved ( None , ctor_path) , pats, None )
255
- }
256
- _ => bug ! ( )
257
- }
258
- }
259
-
260
- hir:: ExprStruct ( ref qpath, ref fields, None ) => {
261
- let field_pats =
262
- fields. iter ( )
263
- . map ( |field| Ok ( codemap:: Spanned {
264
- span : syntax_pos:: DUMMY_SP ,
265
- node : hir:: FieldPat {
266
- name : field. name . node ,
267
- pat : const_expr_to_pat ( tcx, & field. expr , pat_id, span) ?,
268
- is_shorthand : false ,
269
- } ,
270
- } ) )
271
- . collect :: < Result < _ , _ > > ( ) ?;
272
- PatKind :: Struct ( qpath. clone ( ) , field_pats, false )
273
- }
274
-
275
- hir:: ExprArray ( ref exprs) => {
276
- let pats = exprs. iter ( )
277
- . map ( |expr| const_expr_to_pat ( tcx, & expr, pat_id, span) )
278
- . collect :: < Result < _ , _ > > ( ) ?;
279
- PatKind :: Slice ( pats, None , hir:: HirVec :: new ( ) )
280
- }
281
-
282
- hir:: ExprPath ( ref qpath) => {
283
- let def = tcx. tables ( ) . qpath_def ( qpath, expr. id ) ;
284
- match def {
285
- Def :: StructCtor ( _, CtorKind :: Const ) |
286
- Def :: VariantCtor ( _, CtorKind :: Const ) => {
287
- match expr. node {
288
- hir:: ExprPath ( hir:: QPath :: Resolved ( _, ref path) ) => {
289
- PatKind :: Path ( hir:: QPath :: Resolved ( None , path. clone ( ) ) )
290
- }
291
- _ => bug ! ( )
292
- }
293
- }
294
- Def :: Const ( def_id) | Def :: AssociatedConst ( def_id) => {
295
- let substs = Some ( tcx. tables ( ) . node_id_item_substs ( expr. id )
296
- . unwrap_or_else ( || tcx. intern_substs ( & [ ] ) ) ) ;
297
- let ( expr, _ty) = lookup_const_by_id ( tcx, def_id, substs) . unwrap ( ) ;
298
- return const_expr_to_pat ( tcx, expr, pat_id, span) ;
299
- } ,
300
- _ => bug ! ( ) ,
301
- }
302
- }
303
-
304
- _ => PatKind :: Lit ( P ( expr. clone ( ) ) )
305
- } ;
306
- Ok ( P ( hir:: Pat { id : expr. id , node : pat, span : span } ) )
307
- }
308
-
309
185
pub fn report_const_eval_err < ' a , ' tcx > (
310
186
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
311
187
err : & ConstEvalErr ,
0 commit comments