Skip to content

Commit f4c2228

Browse files
committed
Check casts from float
1 parent da7529a commit f4c2228

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/librustc_typeck/check/cast.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,11 @@ pub fn check_cast<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, cast: &CastCheck<'tcx>) {
9494
let t_e_is_c_enum = ty::type_is_c_like_enum(fcx.tcx(), t_e);
9595

9696
let t_1_is_scalar = ty::type_is_scalar(t_1);
97+
let t_1_is_integral = ty::type_is_integral(t_1);
9798
let t_1_is_char = ty::type_is_char(t_1);
9899
let t_1_is_bare_fn = ty::type_is_bare_fn(t_1);
99100
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);
100102

101103
// casts to scalars other than `char` and `bare fn` are trivial
102104
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>) {
113115
} else if t_1.sty == ty::ty_bool {
114116
span_err!(fcx.tcx().sess, span, E0054,
115117
"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)
116122
} else if t_1_is_float && (t_e_is_scalar || t_e_is_c_enum) && !(
117123
t_e_is_integral || t_e_is_float || t_e.sty == ty::ty_bool) {
118124
// Casts to float must go through an integer or boolean

src/test/compile-fail/unsupported-cast.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// ignore-test FIXME: #13993
12-
// error-pattern:unsupported cast
11+
// error-pattern:illegal cast
12+
13+
#![feature(libc)]
1314

1415
extern crate libc;
1516

0 commit comments

Comments
 (0)