@@ -502,42 +502,44 @@ pub fn eval_const_expr_partial<T: ty::ExprTyProvider>(tcx: &T, e: &Expr)
502
502
const cast")
503
503
} ) ;
504
504
505
- let base = eval_const_expr_partial ( tcx, & * * base) ;
506
- match base {
507
- Err ( _) => base,
508
- Ok ( val) => {
509
- match ty:: get ( ety) . sty {
510
- ty:: ty_float( _) => {
511
- match val {
512
- const_bool( b) => Ok ( const_float ( b as f64 ) ) ,
513
- const_uint( u) => Ok ( const_float ( u as f64 ) ) ,
514
- const_int( i) => Ok ( const_float ( i as f64 ) ) ,
515
- const_float( f) => Ok ( const_float ( f) ) ,
516
- _ => Err ( "can't cast this type to float" . to_string ( ) ) ,
517
- }
505
+ macro_rules! define_casts(
506
+ ( $val: ident, {
507
+ $( $ty_pat: pat => (
508
+ $intermediate_ty: ty,
509
+ $const_type: ident,
510
+ $target_ty: ty
511
+ ) ) ,*
512
+ } ) => ( match ty:: get( ety) . sty {
513
+ $( $ty_pat => {
514
+ match $val {
515
+ const_bool( b) => Ok ( $const_type( b as $intermediate_ty as $target_ty) ) ,
516
+ const_uint( u) => Ok ( $const_type( u as $intermediate_ty as $target_ty) ) ,
517
+ const_int( i) => Ok ( $const_type( i as $intermediate_ty as $target_ty) ) ,
518
+ const_float( f) => Ok ( $const_type( f as $intermediate_ty as $target_ty) ) ,
519
+ _ => Err ( concat!(
520
+ "can't cast this type to " , stringify!( $const_type)
521
+ ) . to_string( ) )
518
522
}
519
- ty:: ty_uint( _) => {
520
- match val {
521
- const_bool( b) => Ok ( const_uint ( b as u64 ) ) ,
522
- const_uint( u) => Ok ( const_uint ( u) ) ,
523
- const_int( i) => Ok ( const_uint ( i as u64 ) ) ,
524
- const_float( f) => Ok ( const_uint ( f as u64 ) ) ,
525
- _ => Err ( "can't cast this type to uint" . to_string ( ) ) ,
526
- }
527
- }
528
- ty:: ty_int( _) => {
529
- match val {
530
- const_bool( b) => Ok ( const_int ( b as i64 ) ) ,
531
- const_uint( u) => Ok ( const_int ( u as i64 ) ) ,
532
- const_int( i) => Ok ( const_int ( i) ) ,
533
- const_float( f) => Ok ( const_int ( f as i64 ) ) ,
534
- _ => Err ( "can't cast this type to int" . to_string ( ) ) ,
535
- }
536
- }
537
- _ => Err ( "can't cast this type" . to_string ( ) )
538
- }
539
- }
540
- }
523
+ } , ) *
524
+ _ => Err ( "can't cast this type" . to_string( ) )
525
+ } )
526
+ )
527
+
528
+ eval_const_expr_partial ( tcx, & * * base)
529
+ . and_then ( |val| define_casts ! ( val, {
530
+ ty:: ty_int( ast:: TyI ) => ( int, const_int, i64 ) ,
531
+ ty:: ty_int( ast:: TyI8 ) => ( i8 , const_int, i64 ) ,
532
+ ty:: ty_int( ast:: TyI16 ) => ( i16 , const_int, i64 ) ,
533
+ ty:: ty_int( ast:: TyI32 ) => ( i32 , const_int, i64 ) ,
534
+ ty:: ty_int( ast:: TyI64 ) => ( i64 , const_int, i64 ) ,
535
+ ty:: ty_uint( ast:: TyU ) => ( uint, const_uint, u64 ) ,
536
+ ty:: ty_uint( ast:: TyU8 ) => ( u8 , const_uint, u64 ) ,
537
+ ty:: ty_uint( ast:: TyU16 ) => ( u16 , const_uint, u64 ) ,
538
+ ty:: ty_uint( ast:: TyU32 ) => ( u32 , const_uint, u64 ) ,
539
+ ty:: ty_uint( ast:: TyU64 ) => ( u64 , const_uint, u64 ) ,
540
+ ty:: ty_float( ast:: TyF32 ) => ( f32 , const_float, f64 ) ,
541
+ ty:: ty_float( ast:: TyF64 ) => ( f64 , const_float, f64 )
542
+ } ) )
541
543
}
542
544
ExprPath ( _) => {
543
545
match lookup_const ( tcx. ty_ctxt ( ) , e) {
0 commit comments