Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
c2rust transpile
: When casting bool
s to floats, go through the in…
…tegral type `u8` (#1030) Previously, C code that cast `bool`s to floating types, like this ```c #include <stdbool.h> void cast_stuff(void) { bool b = true; float x15 = b; } ``` would try to do so directly in Rust, like this ```rust #[no_mangle] pub unsafe extern "C" fn cast_stuff() { let mut b: bool = 1 as libc::c_int != 0; let mut x15: libc::c_float = b as libc::c_float; } ``` which isn't allowed, resulting in errors like this ```shell error[E0606]: casting `bool` as `f32` is invalid --> src/casts.rs:31:34 | 31 | let mut x15: libc::c_float = b as libc::c_float; | ^^^^^^^^^^^^^^^^^^ | = help: cast through an integer first ``` This fixes things by emitting this Rust instead by casting through the integral type `u8`: ```rust #[no_mangle] pub unsafe extern "C" fn cast_stuff() { let mut b: bool = 1 as libc::c_int != 0; let mut x15: libc::c_float = b as u8 as libc::c_float; } ```
- Loading branch information