Skip to content

Commit

Permalink
Check casts from float
Browse files Browse the repository at this point in the history
  • Loading branch information
sanxiyn committed Apr 7, 2015
1 parent da7529a commit f4c2228
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/librustc_typeck/check/cast.rs
Expand Up @@ -94,9 +94,11 @@ pub fn check_cast<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, cast: &CastCheck<'tcx>) {
let t_e_is_c_enum = ty::type_is_c_like_enum(fcx.tcx(), t_e);

let t_1_is_scalar = ty::type_is_scalar(t_1);
let t_1_is_integral = ty::type_is_integral(t_1);
let t_1_is_char = ty::type_is_char(t_1);
let t_1_is_bare_fn = ty::type_is_bare_fn(t_1);
let t_1_is_float = ty::type_is_floating_point(t_1);
let t_1_is_c_enum = ty::type_is_c_like_enum(fcx.tcx(), t_1);

// casts to scalars other than `char` and `bare fn` are trivial
let t_1_is_trivial = t_1_is_scalar && !t_1_is_char && !t_1_is_bare_fn;
Expand All @@ -113,6 +115,10 @@ pub fn check_cast<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, cast: &CastCheck<'tcx>) {
} else if t_1.sty == ty::ty_bool {
span_err!(fcx.tcx().sess, span, E0054,
"cannot cast as `bool`, compare with zero instead");
} else if t_e_is_float && (t_1_is_scalar || t_1_is_c_enum) && !(
t_1_is_integral || t_1_is_float) {
// Casts from float must go through an integer
cast_through_integer_err(fcx, span, t_1, t_e)
} else if t_1_is_float && (t_e_is_scalar || t_e_is_c_enum) && !(
t_e_is_integral || t_e_is_float || t_e.sty == ty::ty_bool) {
// Casts to float must go through an integer or boolean
Expand Down
5 changes: 3 additions & 2 deletions src/test/compile-fail/unsupported-cast.rs
Expand Up @@ -8,8 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// ignore-test FIXME: #13993
// error-pattern:unsupported cast
// error-pattern:illegal cast

#![feature(libc)]

extern crate libc;

Expand Down

0 comments on commit f4c2228

Please sign in to comment.