]> git.lizzy.rs Git - rust.git/commitdiff
Check casts from float
authorSeo Sanghyeon <sanxiyn@gmail.com>
Tue, 7 Apr 2015 13:45:10 +0000 (22:45 +0900)
committerSeo Sanghyeon <sanxiyn@gmail.com>
Tue, 7 Apr 2015 13:49:43 +0000 (22:49 +0900)
src/librustc_typeck/check/cast.rs
src/test/compile-fail/unsupported-cast.rs

index b35e3c800fcb01a11695664729f5446d0eac6c8e..b7aebff9ac0960bab180cf34412717ae02f8a863 100644 (file)
@@ -94,9 +94,11 @@ fn cast_through_integer_err<'a, 'tcx>(fcx: &FnCtxt<'a, '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;
@@ -113,6 +115,10 @@ fn cast_through_integer_err<'a, 'tcx>(fcx: &FnCtxt<'a, '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
index ca17c898ec37cdf2e582b028cb79b4a18d6aa50b..b4246f2ed87f3009e181b2a501f31cd8e42e4d80 100644 (file)
@@ -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;