@@ -94,9 +94,11 @@ pub fn check_cast<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, cast: &CastCheck<'tcx>) {
94
94
let t_e_is_c_enum = ty:: type_is_c_like_enum ( fcx. tcx ( ) , t_e) ;
95
95
96
96
let t_1_is_scalar = ty:: type_is_scalar ( t_1) ;
97
+ let t_1_is_integral = ty:: type_is_integral ( t_1) ;
97
98
let t_1_is_char = ty:: type_is_char ( t_1) ;
98
99
let t_1_is_bare_fn = ty:: type_is_bare_fn ( t_1) ;
99
100
let t_1_is_float = ty:: type_is_floating_point ( t_1) ;
101
+ let t_1_is_c_enum = ty:: type_is_c_like_enum ( fcx. tcx ( ) , t_1) ;
100
102
101
103
// casts to scalars other than `char` and `bare fn` are trivial
102
104
let t_1_is_trivial = t_1_is_scalar && !t_1_is_char && !t_1_is_bare_fn;
@@ -113,6 +115,10 @@ pub fn check_cast<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, cast: &CastCheck<'tcx>) {
113
115
} else if t_1. sty == ty:: ty_bool {
114
116
span_err ! ( fcx. tcx( ) . sess, span, E0054 ,
115
117
"cannot cast as `bool`, compare with zero instead" ) ;
118
+ } else if t_e_is_float && ( t_1_is_scalar || t_1_is_c_enum) && !(
119
+ t_1_is_integral || t_1_is_float) {
120
+ // Casts from float must go through an integer
121
+ cast_through_integer_err ( fcx, span, t_1, t_e)
116
122
} else if t_1_is_float && ( t_e_is_scalar || t_e_is_c_enum) && !(
117
123
t_e_is_integral || t_e_is_float || t_e. sty == ty:: ty_bool) {
118
124
// Casts to float must go through an integer or boolean
0 commit comments