@@ -3042,12 +3042,24 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
30423042 let t_1_is_scalar = type_is_scalar ( fcx, expr. span , t_1) ;
30433043 let t_1_is_char = type_is_char ( fcx, expr. span , t_1) ;
30443044 let t_1_is_bare_fn = type_is_bare_fn ( fcx, expr. span , t_1) ;
3045+ let t_1_is_float = type_is_floating_point ( fcx,
3046+ expr. span ,
3047+ t_1) ;
30453048
30463049 // casts to scalars other than `char` and `bare fn` are trivial
30473050 let t_1_is_trivial = t_1_is_scalar &&
30483051 !t_1_is_char && !t_1_is_bare_fn;
30493052
3050- if type_is_c_like_enum ( fcx, expr. span , t_e) && t_1_is_trivial {
3053+ if type_is_c_like_enum ( fcx, expr. span , t_e) &&
3054+ t_1_is_trivial {
3055+ if t_1_is_float {
3056+ fcx. type_error_message ( expr. span , |actual| {
3057+ format ! ( "illegal cast; cast through an \
3058+ integer first: `{}` as `{}`",
3059+ actual,
3060+ fcx. infcx( ) . ty_to_str( t_1) )
3061+ } , t_e, None ) ;
3062+ }
30513063 // casts from C-like enums are allowed
30523064 } else if t_1_is_char {
30533065 let te = fcx. infcx ( ) . resolve_type_vars_if_possible ( te) ;
@@ -4206,6 +4218,11 @@ pub fn type_is_bare_fn(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
42064218 return ty:: type_is_bare_fn ( typ_s) ;
42074219}
42084220
4221+ pub fn type_is_floating_point ( fcx : & FnCtxt , sp : Span , typ : ty:: t ) -> bool {
4222+ let typ_s = structurally_resolved_type ( fcx, sp, typ) ;
4223+ return ty:: type_is_floating_point ( typ_s) ;
4224+ }
4225+
42094226pub fn type_is_unsafe_ptr ( fcx : & FnCtxt , sp : Span , typ : ty:: t ) -> bool {
42104227 let typ_s = structurally_resolved_type ( fcx, sp, typ) ;
42114228 return ty:: type_is_unsafe_ptr ( typ_s) ;
0 commit comments