Repository: immunant/c2rust (language: rust)

Issue:
c2rust Transpilation failure with Struct Initialization Using Expressions
## Description

`c2rust` transpiler demonstrates an inability to handle struct initializations where the fields are initialized with expressions that involve operations, including arithmetic, increments, and decrements. This limitation affects the successful transpilation of typical C code patterns where struct fields are dynamically initialized using various expressions.


## Source C Code

```c
typedef struct {
    int a;
} Data;

int main() {
    int p = 10;
    Data myDataInc = {p++}; // error here at Post-increment 
    Data myDataDec = {p--}; // error here at Post-decrement
    Data myDataAdd = {p + 4}; // error here at Arithmetic addition
    Data myDataMinus = {p - 4}; // error here at Arithmetic subtraction
    return 0;
}
```

## Transpilation Error

Attempting to transpile the above code using `c2rust` results in an error message that suggests a fundamental handling issue with expressions in struct field initialization: 

`c2rust v0.18.0` shows the following error:
```bash
$ c2rust-transpile compile_commands.json -e -o transpiled_code --binary runner
Transpiling runner.c
error: Failed to translate main: Expected no statements in field expression
```

This error reveals that c2rust expects field initializations in `struct` to be straightforward without involving any operations or statements.

## Expected Behavior

`c2rust` should ideally handle all forms of valid expressions used in struct field initializations, reflecting the diverse and dynamic initialization practices in C programming.



Discussion / hints:
I tried `v0.19.0` as well but got a different type of error for the above C code:

```
$ c2rust-transpile compile_commands.json -e -o transpiled_code --binary runner
Transpiling runner.c
thread 'main' panicked at /home/yeaseen/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.92/src/lib.rs:849:13:
unsupported proc macro punctuation character '('
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```
> I tried `v0.19.0` as well but got a different type of error for the above C code:
> 
> ```
> $ c2rust-transpile compile_commands.json -e -o transpiled_code --binary runner
> Transpiling runner.c
> thread 'main' panicked at /home/yeaseen/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.92/src/lib.rs:849:13:
> unsupported proc macro punctuation character '('
> note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
> ```

This error can be fixed by `cargo install`ing with `--locked` I believe.

The main issue you opened here, we need to fix, though.
@kkysen thanks for `--locked` option. The latest release, `v0.19.0`, works fine and gives the same error as the main issue error.

Fix patch:
diff --git a/c2rust-transpile/src/translator/structs.rs b/c2rust-transpile/src/translator/structs.rs
index fae47c4d15..4eb75e18a5 100644
--- a/c2rust-transpile/src/translator/structs.rs
+++ b/c2rust-transpile/src/translator/structs.rs
@@ -544,12 +544,6 @@ impl<'a> Translation<'a> {
                 Both(field_id, (field_name, _, bitfield_width, use_inner_type)) => {
                     let mut expr = self.convert_expr(ctx.used(), *field_id)?;
 
-                    if !expr.is_pure() {
-                        return Err(TranslationError::generic(
-                            "Expected no statements in field expression",
-                        ));
-                    }
-
                     if use_inner_type {
                         // See comment above
                         expr = expr.map(|fi| mk().anon_field_expr(fi, 0));


Test patch:
diff --git a/tests/structs/src/struct_with_exp.c b/tests/structs/src/struct_with_exp.c
new file mode 100644
index 0000000000..8d99be6b89
--- /dev/null
+++ b/tests/structs/src/struct_with_exp.c
@@ -0,0 +1,14 @@
+
+struct s { 
+    int i; 
+};
+
+void struct_with_exp(const unsigned int buffer_size, int buffer[const]){
+    if (buffer_size < 1) return;
+    
+    struct s *p;
+    int j = 42;
+    p = &((struct s){j++}); // Compound literal with address-of operator and post-increment operator
+
+    buffer[0] = p->i;
+}
diff --git a/tests/structs/src/test_struct_with_exp.rs b/tests/structs/src/test_struct_with_exp.rs
new file mode 100644
index 0000000000..db063882c6
--- /dev/null
+++ b/tests/structs/src/test_struct_with_exp.rs
@@ -0,0 +1,23 @@
+use crate::struct_with_exp::rust_struct_with_exp;
+use libc::{c_int, c_uint, size_t};
+
+#[link(name = "test")]
+extern "C" {
+    fn struct_with_exp(_: c_uint, _: *mut c_int);
+}
+
+const BUFFER_SIZE: usize = 1;
+
+pub fn test_struct_with_exp() {
+  let mut buffer = [0; BUFFER_SIZE];
+  let mut rust_buffer = [0; BUFFER_SIZE];
+  let expected_buffer = [42];
+
+  unsafe {
+      struct_with_exp(BUFFER_SIZE as u32, buffer.as_mut_ptr());
+      rust_struct_with_exp(BUFFER_SIZE as u32, rust_buffer.as_mut_ptr());
+  }
+
+  assert_eq!(buffer, rust_buffer);
+  assert_eq!(buffer, expected_buffer);
+}