@@ -283,106 +283,40 @@ impl ConstEvalErr {
283
283
}
284
284
}
285
285
286
- fn invalid_op_for_bools ( e : & Expr , op : ast:: BinOp_ ) -> ConstEvalErr {
287
- ConstEvalErr { span : e. span , kind : ErrKind :: InvalidOpForBools ( op) }
288
- }
289
- fn invalid_op_for_floats ( e : & Expr , op : ast:: BinOp_ ) -> ConstEvalErr {
290
- ConstEvalErr { span : e. span , kind : ErrKind :: InvalidOpForFloats ( op) }
291
- }
292
- fn invalid_op_for_int_uint ( e : & Expr , op : ast:: BinOp_ ) -> ConstEvalErr {
293
- ConstEvalErr { span : e. span , kind : ErrKind :: InvalidOpForIntUint ( op) }
294
- }
295
- fn invalid_op_for_uint_int ( e : & Expr , op : ast:: BinOp_ ) -> ConstEvalErr {
296
- ConstEvalErr { span : e. span , kind : ErrKind :: InvalidOpForUintInt ( op) }
297
- }
298
- fn negate_on_string ( e : & Expr ) -> ConstEvalErr {
299
- ConstEvalErr { span : e. span , kind : ErrKind :: NegateOnString }
300
- }
301
- fn negate_on_boolean ( e : & Expr ) -> ConstEvalErr {
302
- ConstEvalErr { span : e. span , kind : ErrKind :: NegateOnBoolean }
303
- }
304
- fn not_on_float ( e : & Expr ) -> ConstEvalErr {
305
- ConstEvalErr { span : e. span , kind : ErrKind :: NotOnFloat }
306
- }
307
- fn not_on_string ( e : & Expr ) -> ConstEvalErr {
308
- ConstEvalErr { span : e. span , kind : ErrKind :: NotOnString }
309
- }
310
-
311
- fn addi_with_overflow ( e : & Expr , a : i64 , b : i64 ) -> ConstEvalErr {
312
- ConstEvalErr { span : e. span , kind : ErrKind :: AddiWithOverflow ( a, b) }
313
- }
314
- fn subi_with_overflow ( e : & Expr , a : i64 , b : i64 ) -> ConstEvalErr {
315
- ConstEvalErr { span : e. span , kind : ErrKind :: SubiWithOverflow ( a, b) }
316
- }
317
- fn muli_with_overflow ( e : & Expr , a : i64 , b : i64 ) -> ConstEvalErr {
318
- ConstEvalErr { span : e. span , kind : ErrKind :: MuliWithOverflow ( a, b) }
319
- }
320
- fn addu_with_overflow ( e : & Expr , a : u64 , b : u64 ) -> ConstEvalErr {
321
- ConstEvalErr { span : e. span , kind : ErrKind :: AdduWithOverflow ( a, b) }
322
- }
323
- fn subu_with_overflow ( e : & Expr , a : u64 , b : u64 ) -> ConstEvalErr {
324
- ConstEvalErr { span : e. span , kind : ErrKind :: SubuWithOverflow ( a, b) }
325
- }
326
- fn mulu_with_overflow ( e : & Expr , a : u64 , b : u64 ) -> ConstEvalErr {
327
- ConstEvalErr { span : e. span , kind : ErrKind :: MuluWithOverflow ( a, b) }
328
- }
329
- fn divide_by_zero ( e : & Expr ) -> ConstEvalErr {
330
- ConstEvalErr { span : e. span , kind : ErrKind :: DivideByZero }
331
- }
332
- fn divide_with_overflow ( e : & Expr ) -> ConstEvalErr {
333
- ConstEvalErr { span : e. span , kind : ErrKind :: DivideWithOverflow }
334
- }
335
- fn modulo_by_zero ( e : & Expr ) -> ConstEvalErr {
336
- ConstEvalErr { span : e. span , kind : ErrKind :: ModuloByZero }
337
- }
338
- fn modulo_with_overflow ( e : & Expr ) -> ConstEvalErr {
339
- ConstEvalErr { span : e. span , kind : ErrKind :: ModuloWithOverflow }
340
- }
341
- fn missing_struct_field ( e : & Expr ) -> ConstEvalErr {
342
- ConstEvalErr { span : e. span , kind : ErrKind :: MissingStructField }
343
- }
344
- fn non_const_path ( e : & Expr ) -> ConstEvalErr {
345
- ConstEvalErr { span : e. span , kind : ErrKind :: NonConstPath }
346
- }
347
- fn non_const_struct ( e : & Expr ) -> ConstEvalErr {
348
- ConstEvalErr { span : e. span , kind : ErrKind :: NonConstStruct }
349
- }
350
- fn tuple_index_out_of_bounds ( e : & Expr ) -> ConstEvalErr {
351
- ConstEvalErr { span : e. span , kind : ErrKind :: TupleIndexOutOfBounds }
352
- }
286
+ macro_rules! signal {
287
+ ( $e: expr, $ctor: ident) => {
288
+ return Err ( ConstEvalErr { span: $e. span, kind: ErrKind :: $ctor } )
289
+ } ;
353
290
354
- fn misc_binary_op ( e : & Expr ) -> ConstEvalErr {
355
- ConstEvalErr { span : e. span , kind : ErrKind :: MiscBinaryOp }
356
- }
357
- fn misc_catch_all ( e : & Expr ) -> ConstEvalErr {
358
- ConstEvalErr { span : e. span , kind : ErrKind :: MiscCatchAll }
291
+ ( $e: expr, $ctor: ident( $( $arg: expr) ,* ) ) => {
292
+ return Err ( ConstEvalErr { span: $e. span, kind: ErrKind :: $ctor( $( $arg) ,* ) } )
293
+ }
359
294
}
360
295
361
-
362
296
fn checked_add_int ( e : & Expr , a : i64 , b : i64 ) -> Result < const_val , ConstEvalErr > {
363
297
let ( ret, oflo) = a. overflowing_add ( b) ;
364
- if !oflo { Ok ( const_int ( ret) ) } else { Err ( addi_with_overflow ( e, a, b) ) }
298
+ if !oflo { Ok ( const_int ( ret) ) } else { signal ! ( e, AddiWithOverflow ( a, b) ) }
365
299
}
366
300
fn checked_sub_int ( e : & Expr , a : i64 , b : i64 ) -> Result < const_val , ConstEvalErr > {
367
301
let ( ret, oflo) = a. overflowing_sub ( b) ;
368
- if !oflo { Ok ( const_int ( ret) ) } else { Err ( subi_with_overflow ( e, a, b) ) }
302
+ if !oflo { Ok ( const_int ( ret) ) } else { signal ! ( e, SubiWithOverflow ( a, b) ) }
369
303
}
370
304
fn checked_mul_int ( e : & Expr , a : i64 , b : i64 ) -> Result < const_val , ConstEvalErr > {
371
305
let ( ret, oflo) = a. overflowing_mul ( b) ;
372
- if !oflo { Ok ( const_int ( ret) ) } else { Err ( muli_with_overflow ( e, a, b) ) }
306
+ if !oflo { Ok ( const_int ( ret) ) } else { signal ! ( e, MuliWithOverflow ( a, b) ) }
373
307
}
374
308
375
309
fn checked_add_uint ( e : & Expr , a : u64 , b : u64 ) -> Result < const_val , ConstEvalErr > {
376
310
let ( ret, oflo) = a. overflowing_add ( b) ;
377
- if !oflo { Ok ( const_uint ( ret) ) } else { Err ( addu_with_overflow ( e, a, b) ) }
311
+ if !oflo { Ok ( const_uint ( ret) ) } else { signal ! ( e, AdduWithOverflow ( a, b) ) }
378
312
}
379
313
fn checked_sub_uint ( e : & Expr , a : u64 , b : u64 ) -> Result < const_val , ConstEvalErr > {
380
314
let ( ret, oflo) = a. overflowing_sub ( b) ;
381
- if !oflo { Ok ( const_uint ( ret) ) } else { Err ( subu_with_overflow ( e, a, b) ) }
315
+ if !oflo { Ok ( const_uint ( ret) ) } else { signal ! ( e, SubuWithOverflow ( a, b) ) }
382
316
}
383
317
fn checked_mul_uint ( e : & Expr , a : u64 , b : u64 ) -> Result < const_val , ConstEvalErr > {
384
318
let ( ret, oflo) = a. overflowing_mul ( b) ;
385
- if !oflo { Ok ( const_uint ( ret) ) } else { Err ( mulu_with_overflow ( e, a, b) ) }
319
+ if !oflo { Ok ( const_uint ( ret) ) } else { signal ! ( e, MuluWithOverflow ( a, b) ) }
386
320
}
387
321
388
322
@@ -400,8 +334,8 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
400
334
Ok ( const_float( f) ) => Ok ( const_float ( -f) ) ,
401
335
Ok ( const_int( i) ) => Ok ( const_int ( -i) ) ,
402
336
Ok ( const_uint( i) ) => Ok ( const_uint ( -i) ) ,
403
- Ok ( const_str( _) ) => Err ( negate_on_string ( e ) ) ,
404
- Ok ( const_bool( _) ) => Err ( negate_on_boolean ( e ) ) ,
337
+ Ok ( const_str( _) ) => signal ! ( e , NegateOnString ) ,
338
+ Ok ( const_bool( _) ) => signal ! ( e , NegateOnBoolean ) ,
405
339
err => err
406
340
}
407
341
}
@@ -410,8 +344,8 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
410
344
Ok ( const_int( i) ) => Ok ( const_int ( !i) ) ,
411
345
Ok ( const_uint( i) ) => Ok ( const_uint ( !i) ) ,
412
346
Ok ( const_bool( b) ) => Ok ( const_bool ( !b) ) ,
413
- Ok ( const_str( _) ) => Err ( not_on_string ( e ) ) ,
414
- Ok ( const_float( _) ) => Err ( not_on_float ( e ) ) ,
347
+ Ok ( const_str( _) ) => signal ! ( e , NotOnString ) ,
348
+ Ok ( const_float( _) ) => signal ! ( e , NotOnFloat ) ,
415
349
err => err
416
350
}
417
351
}
@@ -435,7 +369,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
435
369
ast:: BiNe => fromb ( a != b) ,
436
370
ast:: BiGe => fromb ( a >= b) ,
437
371
ast:: BiGt => fromb ( a > b) ,
438
- _ => Err ( invalid_op_for_floats ( e, op. node ) ) ,
372
+ _ => signal ! ( e, InvalidOpForFloats ( op. node) )
439
373
}
440
374
}
441
375
( Ok ( const_int( a) ) , Ok ( const_int( b) ) ) => {
@@ -463,18 +397,18 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
463
397
ast:: BiMul => checked_mul_int ( e, a, b) ,
464
398
ast:: BiDiv => {
465
399
if b == 0 {
466
- Err ( divide_by_zero ( e ) )
400
+ signal ! ( e , DivideByZero ) ;
467
401
} else if b == -1 && is_a_min_value ( ) {
468
- Err ( divide_with_overflow ( e ) )
402
+ signal ! ( e , DivideWithOverflow ) ;
469
403
} else {
470
404
Ok ( const_int ( a / b) )
471
405
}
472
406
}
473
407
ast:: BiRem => {
474
408
if b == 0 {
475
- Err ( modulo_by_zero ( e ) )
409
+ signal ! ( e , ModuloByZero )
476
410
} else if b == -1 && is_a_min_value ( ) {
477
- Err ( modulo_with_overflow ( e ) )
411
+ signal ! ( e , ModuloWithOverflow )
478
412
} else {
479
413
Ok ( const_int ( a % b) )
480
414
}
@@ -497,9 +431,9 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
497
431
ast:: BiAdd => checked_add_uint ( e, a, b) ,
498
432
ast:: BiSub => checked_sub_uint ( e, a, b) ,
499
433
ast:: BiMul => checked_mul_uint ( e, a, b) ,
500
- ast:: BiDiv if b == 0 => Err ( divide_by_zero ( e ) ) ,
434
+ ast:: BiDiv if b == 0 => signal ! ( e , DivideByZero ) ,
501
435
ast:: BiDiv => Ok ( const_uint ( a / b) ) ,
502
- ast:: BiRem if b == 0 => Err ( modulo_by_zero ( e ) ) ,
436
+ ast:: BiRem if b == 0 => signal ! ( e , ModuloByZero ) ,
503
437
ast:: BiRem => Ok ( const_uint ( a % b) ) ,
504
438
ast:: BiAnd | ast:: BiBitAnd => Ok ( const_uint ( a & b) ) ,
505
439
ast:: BiOr | ast:: BiBitOr => Ok ( const_uint ( a | b) ) ,
@@ -519,14 +453,14 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
519
453
match op. node {
520
454
ast:: BiShl => Ok ( const_int ( a << b as uint ) ) ,
521
455
ast:: BiShr => Ok ( const_int ( a >> b as uint ) ) ,
522
- _ => Err ( invalid_op_for_int_uint ( e, op. node ) ) ,
456
+ _ => signal ! ( e, InvalidOpForIntUint ( op. node) ) ,
523
457
}
524
458
}
525
459
( Ok ( const_uint( a) ) , Ok ( const_int( b) ) ) => {
526
460
match op. node {
527
461
ast:: BiShl => Ok ( const_uint ( a << b as uint ) ) ,
528
462
ast:: BiShr => Ok ( const_uint ( a >> b as uint ) ) ,
529
- _ => Err ( invalid_op_for_uint_int ( e, op. node ) ) ,
463
+ _ => signal ! ( e, InvalidOpForUintInt ( op. node) ) ,
530
464
}
531
465
}
532
466
( Ok ( const_bool( a) ) , Ok ( const_bool( b) ) ) => {
@@ -538,13 +472,13 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
538
472
ast:: BiBitOr => a | b,
539
473
ast:: BiEq => a == b,
540
474
ast:: BiNe => a != b,
541
- _ => return Err ( invalid_op_for_bools ( e, op. node ) ) ,
475
+ _ => signal ! ( e, InvalidOpForBools ( op. node) ) ,
542
476
} ) )
543
477
}
544
478
( err @ Err ( ..) , _) |
545
479
( _, err @ Err ( ..) ) => err,
546
480
547
- _ => Err ( misc_binary_op ( e ) ) ,
481
+ _ => signal ! ( e , MiscBinaryOp ) ,
548
482
}
549
483
}
550
484
ast:: ExprCast ( ref base, ref target_ty) => {
@@ -589,7 +523,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
589
523
} ;
590
524
let const_expr = match const_expr {
591
525
Some ( actual_e) => actual_e,
592
- None => return Err ( non_const_path ( e ) ) ,
526
+ None => signal ! ( e , NonConstPath )
593
527
} ;
594
528
let ety = ety. or_else ( || const_ty. and_then ( |ty| ast_ty_to_prim_ty ( tcx, ty) ) ) ;
595
529
eval_const_expr_partial ( tcx, const_expr, ety)
@@ -611,11 +545,11 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
611
545
if fields. len ( ) > index. node {
612
546
return eval_const_expr_partial ( tcx, & * fields[ index. node ] , None )
613
547
} else {
614
- return Err ( tuple_index_out_of_bounds ( e ) )
548
+ signal ! ( e , TupleIndexOutOfBounds ) ;
615
549
}
616
550
}
617
551
618
- Err ( non_const_struct ( e ) )
552
+ signal ! ( e , NonConstStruct ) ;
619
553
}
620
554
ast:: ExprField ( ref base, field_name) => {
621
555
// Get the base expression if it is a struct and it is constant
@@ -626,13 +560,13 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
626
560
f. ident . node . as_str ( ) == field_name. node . as_str ( ) ) {
627
561
return eval_const_expr_partial ( tcx, & * f. expr , None )
628
562
} else {
629
- return Err ( missing_struct_field ( e ) ) ;
563
+ signal ! ( e , MissingStructField ) ;
630
564
}
631
565
}
632
566
633
- Err ( non_const_struct ( e ) )
567
+ signal ! ( e , NonConstStruct ) ;
634
568
}
635
- _ => Err ( misc_catch_all ( e ) )
569
+ _ => signal ! ( e , MiscCatchAll )
636
570
}
637
571
}
638
572
0 commit comments