From 2f54d68e16b13dfc45830f3110566f39a8a628eb Mon Sep 17 00:00:00 2001 From: David Herzka Date: Fri, 29 Sep 2023 16:13:21 -0400 Subject: [PATCH 1/2] When casting bool to float, go through integral type --- c2rust-transpile/src/translator/mod.rs | 4 ++++ tests/casts/src/casts.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index 91144d0f29..ca504d33de 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -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`. diff --git a/tests/casts/src/casts.c b/tests/casts/src/casts.c index e6749bec0b..1222683017 100644 --- a/tests/casts/src/casts.c +++ b/tests/casts/src/casts.c @@ -1,4 +1,5 @@ #include +#include void cast_stuff(void) { int inta[10] = {0}; @@ -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; } From 03a15cad32ba3180f6b46dbc2a01971c2fc934a7 Mon Sep 17 00:00:00 2001 From: David Herzka Date: Fri, 29 Sep 2023 16:20:33 -0400 Subject: [PATCH 2/2] Fix formatting --- c2rust-transpile/src/translator/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index ca504d33de..572ee5a68c 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -4277,7 +4277,10 @@ impl<'c> Translation<'c> { 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))) + 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,