@@ -408,7 +408,7 @@ pub enum ErrKind {
408
408
TypeMismatch ( String , ConstInt ) ,
409
409
BadType ( ConstVal ) ,
410
410
ErroneousReferencedConstant ( Box < ConstEvalErr > ) ,
411
- BadCharValue ,
411
+ CharCast ( ConstInt ) ,
412
412
}
413
413
414
414
impl From < ConstMathErr > for ErrKind {
@@ -469,7 +469,9 @@ impl ConstEvalErr {
469
469
} ,
470
470
BadType ( ref i) => format ! ( "value of wrong type: {:?}" , i) . into_cow ( ) ,
471
471
ErroneousReferencedConstant ( _) => "could not evaluate referenced constant" . into_cow ( ) ,
472
- BadCharValue => "invalid numeric value for char" . into_cow ( ) ,
472
+ CharCast ( ref got) => {
473
+ format ! ( "only `u8` can be cast as `char`, not `{}`" , got. description( ) ) . into_cow ( )
474
+ } ,
473
475
}
474
476
}
475
477
}
@@ -1080,16 +1082,19 @@ fn cast_const_int<'tcx>(tcx: &TyCtxt<'tcx>, val: ConstInt, ty: ty::Ty) -> CastRe
1080
1082
ty:: TyFloat ( ast:: FloatTy :: F64 ) => match val. erase_type ( ) {
1081
1083
Infer ( u) => Ok ( Float ( u as f64 ) ) ,
1082
1084
InferSigned ( i) => Ok ( Float ( i as f64 ) ) ,
1083
- _ => unreachable ! ( ) ,
1085
+ _ => bug ! ( "ConstInt::erase_type returned something other than Infer/InferSigned" ) ,
1084
1086
} ,
1085
1087
ty:: TyFloat ( ast:: FloatTy :: F32 ) => match val. erase_type ( ) {
1086
1088
Infer ( u) => Ok ( Float ( u as f32 as f64 ) ) ,
1087
1089
InferSigned ( i) => Ok ( Float ( i as f32 as f64 ) ) ,
1088
- _ => unreachable ! ( ) ,
1090
+ _ => bug ! ( "ConstInt::erase_type returned something other than Infer/InferSigned" ) ,
1089
1091
} ,
1090
1092
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 ) ,
1093
+ ty:: TyChar => match infer ( val, tcx, & ty:: TyUint ( ast:: UintTy :: U8 ) ) {
1094
+ Ok ( U8 ( u) ) => Ok ( Char ( u as char ) ) ,
1095
+ // can only occur before typeck, typeck blocks `T as char` for `T` != `u8`
1096
+ _ => Err ( CharCast ( val) ) ,
1097
+ } ,
1093
1098
_ => Err ( CannotCast ) ,
1094
1099
}
1095
1100
}
0 commit comments