@@ -408,6 +408,7 @@ pub enum ErrKind {
408
408
TypeMismatch ( String , ConstInt ) ,
409
409
BadType ( ConstVal ) ,
410
410
ErroneousReferencedConstant ( Box < ConstEvalErr > ) ,
411
+ BadCharValue ,
411
412
}
412
413
413
414
impl From < ConstMathErr > for ErrKind {
@@ -468,6 +469,7 @@ impl ConstEvalErr {
468
469
} ,
469
470
BadType ( ref i) => format ! ( "value of wrong type: {:?}" , i) . into_cow ( ) ,
470
471
ErroneousReferencedConstant ( _) => "could not evaluate referenced constant" . into_cow ( ) ,
472
+ BadCharValue => "invalid numeric value for char" . into_cow ( ) ,
471
473
}
472
474
}
473
475
}
@@ -1075,23 +1077,19 @@ fn cast_const_int<'tcx>(tcx: &TyCtxt<'tcx>, val: ConstInt, ty: ty::Ty) -> CastRe
1075
1077
Err ( _) => Ok ( Integral ( Usize ( ConstUsize :: Us32 ( v as u32 ) ) ) ) ,
1076
1078
}
1077
1079
} ,
1078
- ty:: TyFloat ( ast:: FloatTy :: F64 ) if val. is_negative ( ) => {
1079
- // FIXME: this could probably be prettier
1080
- // there's no easy way to turn an `Infer` into a f64
1081
- let val = ( -val) . map_err ( Math ) ?;
1082
- let val = val. to_u64 ( ) . unwrap ( ) as f64 ;
1083
- let val = -val;
1084
- Ok ( Float ( val) )
1080
+ ty:: TyFloat ( ast:: FloatTy :: F64 ) => match val. erase_type ( ) {
1081
+ Infer ( u) => Ok ( Float ( u as f64 ) ) ,
1082
+ InferSigned ( i) => Ok ( Float ( i as f64 ) ) ,
1083
+ _ => unreachable ! ( ) ,
1085
1084
} ,
1086
- ty:: TyFloat ( ast:: FloatTy :: F64 ) => Ok ( Float ( val. to_u64 ( ) . unwrap ( ) as f64 ) ) ,
1087
- ty:: TyFloat ( ast:: FloatTy :: F32 ) if val. is_negative ( ) => {
1088
- let val = ( -val) . map_err ( Math ) ?;
1089
- let val = val. to_u64 ( ) . unwrap ( ) as f32 ;
1090
- let val = -val;
1091
- Ok ( Float ( val as f64 ) )
1085
+ ty:: TyFloat ( ast:: FloatTy :: F32 ) => match val. erase_type ( ) {
1086
+ Infer ( u) => Ok ( Float ( u as f32 as f64 ) ) ,
1087
+ InferSigned ( i) => Ok ( Float ( i as f32 as f64 ) ) ,
1088
+ _ => unreachable ! ( ) ,
1092
1089
} ,
1093
- ty:: TyFloat ( ast:: FloatTy :: F32 ) => Ok ( Float ( val. to_u64 ( ) . unwrap ( ) as f32 as f64 ) ) ,
1094
1090
ty:: TyRawPtr ( _) => Err ( ErrKind :: UnimplementedConstVal ( "casting an address to a raw ptr" ) ) ,
1091
+ ty:: TyChar if v as u32 as u64 == v => :: std:: char:: from_u32 ( v as u32 ) . map ( Char )
1092
+ . ok_or ( BadCharValue ) ,
1095
1093
_ => Err ( CannotCast ) ,
1096
1094
}
1097
1095
}
0 commit comments