c2rust icon indicating copy to clipboard operation
c2rust copied to clipboard

`bool` `return` is silently lost in translation

Open dmcardle opened this issue 4 years ago • 1 comments

I played with the demo site: https://c2rust.com/

It looks like any time I return a bool without including stdbool.h, the return statement is missing from the Rust translation.

When I include stdbool.h, the output looks reasonable. Shouldn't this be an error rather than a silent failure?

C:

bool foo() {
    return true;
}

Rust:

#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case,
         non_upper_case_globals, unused_assignments, unused_mut)]
#![register_tool(c2rust)]
#![feature(register_tool)]
#[no_mangle]
pub unsafe extern "C" fn foo() -> libc::c_int {
    panic!("Reached end of non-void function without returning");
}

C:

#include <stdbool.h>

bool foo() {
    return true;
}

Rust:

#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case,
         non_upper_case_globals, unused_assignments, unused_mut)]
#![register_tool(c2rust)]
#![feature(register_tool)]
#[no_mangle]
pub unsafe extern "C" fn foo() -> bool { return 1 as libc::c_int != 0; }

dmcardle avatar Feb 01 '22 23:02 dmcardle

I've confirmed this behavior. A few notes:

  • This C code (without stdbool.h) does not compile unmodified with GCC or Clang--the header really is needed for correctness.
  • Replacing true with asdf results in the same output from c2rust.

We should indeed emit a translation-time error here.

fw-immunant avatar Apr 12 '22 20:04 fw-immunant