Skip to content

Commit

Permalink
When casting bool to float, go through integral type
Browse files Browse the repository at this point in the history
  • Loading branch information
dgherzka committed Sep 29, 2023
1 parent 0ba4903 commit 29fa67d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions c2rust-transpile/src/translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4275,6 +4275,10 @@ impl<'c> Translation<'c> {
let expr =
expr.ok_or_else(|| format_err!("Casts to enums require a C ExprId"))?;
Ok(self.enum_cast(ty.ctype, enum_decl_id, expr, val, source_ty, target_ty))
} else if target_ty_ctype.is_floating_type() && source_ty_kind.is_bool() {
val.and_then(|x| {
Ok(WithStmts::new_val(mk().cast_expr(mk().cast_expr(x, mk().path_ty(vec!["u8"])), target_ty)))
})
} else {
// Other numeric casts translate to Rust `as` casts,
// unless the cast is to a function pointer then use `transmute`.
Expand Down
4 changes: 4 additions & 0 deletions tests/casts/src/casts.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdlib.h>
#include <stdbool.h>

void cast_stuff(void) {
int inta[10] = {0};
Expand Down Expand Up @@ -26,4 +27,7 @@ void cast_stuff(void) {
// need to make sure we handle this correctly.
const int const_i = -1;
int *x14 = (int*) &const_i;

bool b = true;
float x15 = b;
}

0 comments on commit 29fa67d

Please sign in to comment.