Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c2rust transpile: When casting bools to floats, go through the integral type u8 #1030

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions c2rust-transpile/src/translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4275,6 +4275,13 @@ 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;
}
Loading