@@ -323,10 +323,13 @@ impl ConstVal {
323
323
}
324
324
}
325
325
326
- pub fn const_expr_to_pat ( tcx : & TyCtxt , expr : & Expr , span : Span ) -> P < hir:: Pat > {
326
+ pub fn const_expr_to_pat ( tcx : & ty:: TyCtxt , expr : & Expr , span : Span )
327
+ -> Result < P < hir:: Pat > , DefId > {
327
328
let pat = match expr. node {
328
329
hir:: ExprTup ( ref exprs) =>
329
- PatKind :: Tup ( exprs. iter ( ) . map ( |expr| const_expr_to_pat ( tcx, & expr, span) ) . collect ( ) ) ,
330
+ PatKind :: Tup ( try!( exprs. iter ( )
331
+ . map ( |expr| const_expr_to_pat ( tcx, & expr, span) )
332
+ . collect ( ) ) ) ,
330
333
331
334
hir:: ExprCall ( ref callee, ref args) => {
332
335
let def = * tcx. def_map . borrow ( ) . get ( & callee. id ) . unwrap ( ) ;
@@ -336,31 +339,38 @@ pub fn const_expr_to_pat(tcx: &TyCtxt, expr: &Expr, span: Span) -> P<hir::Pat> {
336
339
let path = match def. full_def ( ) {
337
340
Def :: Struct ( def_id) => def_to_path ( tcx, def_id) ,
338
341
Def :: Variant ( _, variant_did) => def_to_path ( tcx, variant_did) ,
339
- Def :: Fn ( ..) => return P ( hir:: Pat {
342
+ Def :: Fn ( ..) => return Ok ( P ( hir:: Pat {
340
343
id : expr. id ,
341
344
node : PatKind :: Lit ( P ( expr. clone ( ) ) ) ,
342
345
span : span,
343
- } ) ,
346
+ } ) ) ,
344
347
_ => unreachable ! ( )
345
348
} ;
346
- let pats = args. iter ( ) . map ( |expr| const_expr_to_pat ( tcx, & expr, span) ) . collect ( ) ;
349
+ let pats = try!( args. iter ( )
350
+ . map ( |expr| const_expr_to_pat ( tcx, & * * expr, span) )
351
+ . collect ( ) ) ;
347
352
PatKind :: TupleStruct ( path, Some ( pats) )
348
353
}
349
354
350
355
hir:: ExprStruct ( ref path, ref fields, None ) => {
351
- let field_pats = fields. iter ( ) . map ( |field| codemap:: Spanned {
352
- span : codemap:: DUMMY_SP ,
353
- node : hir:: FieldPat {
354
- name : field. name . node ,
355
- pat : const_expr_to_pat ( tcx, & field. expr , span) ,
356
- is_shorthand : false ,
357
- } ,
358
- } ) . collect ( ) ;
356
+ let field_pats =
357
+ try!( fields. iter ( )
358
+ . map ( |field| Ok ( codemap:: Spanned {
359
+ span : codemap:: DUMMY_SP ,
360
+ node : hir:: FieldPat {
361
+ name : field. name . node ,
362
+ pat : try!( const_expr_to_pat ( tcx, & field. expr , span) ) ,
363
+ is_shorthand : false ,
364
+ } ,
365
+ } ) )
366
+ . collect ( ) ) ;
359
367
PatKind :: Struct ( path. clone ( ) , field_pats, false )
360
368
}
361
369
362
370
hir:: ExprVec ( ref exprs) => {
363
- let pats = exprs. iter ( ) . map ( |expr| const_expr_to_pat ( tcx, & expr, span) ) . collect ( ) ;
371
+ let pats = try!( exprs. iter ( )
372
+ . map ( |expr| const_expr_to_pat ( tcx, & expr, span) )
373
+ . collect ( ) ) ;
364
374
PatKind :: Vec ( pats, None , hir:: HirVec :: new ( ) )
365
375
}
366
376
@@ -381,7 +391,7 @@ pub fn const_expr_to_pat(tcx: &TyCtxt, expr: &Expr, span: Span) -> P<hir::Pat> {
381
391
382
392
_ => PatKind :: Lit ( P ( expr. clone ( ) ) )
383
393
} ;
384
- P ( hir:: Pat { id : expr. id , node : pat, span : span } )
394
+ Ok ( P ( hir:: Pat { id : expr. id , node : pat, span : span } ) )
385
395
}
386
396
387
397
pub fn eval_const_expr ( tcx : & TyCtxt , e : & Expr ) -> ConstVal {
0 commit comments