c2rust icon indicating copy to clipboard operation
c2rust copied to clipboard

transpile: `return` is stripped from end of statement expressions

Open Rua opened this issue 2 weeks ago • 0 comments

Transpiling the following:

#include <stdio.h>

void foo() {
    int var = 9001;

    ({
        var = 42;
        return;
    });

    printf("I should be unreachable!");
}

results in

#![allow(
    dead_code,
    non_camel_case_types,
    non_snake_case,
    non_upper_case_globals,
    unused_assignments,
    unused_mut
)]
#![feature(label_break_value)]
extern "C" {
    fn printf(__format: *const ::core::ffi::c_char, ...) -> ::core::ffi::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn foo() {
    let mut var: ::core::ffi::c_int = 9001 as ::core::ffi::c_int;
    var = 42 as ::core::ffi::c_int;
    printf(b"I should be unreachable!\0" as *const u8 as *const ::core::ffi::c_char);
}

The Rust code is missing the return, causing the code below to be executed when it shouldn't be. I've tracked down the cause of this to the code here: https://github.com/immunant/c2rust/blob/ca313fd66e947b87c72d45dbe941a9d737d51474/c2rust-transpile/src/translator/mod.rs#L4260-L4273

convert_function_body calls convert_cfg with true passed for the cut_out_trailing_ret argument. But it should be passing false for statement expressions, only true for actual functions.

Rua avatar Dec 10 '25 15:12 Rua