Repository: immunant/c2rust

Issue #1165: c2rust fails to handle increment or decrement operations within compound literals
Created: 2024-11-23T08:06:04Z
Description
The c2rust tool fails to translate valid C code that uses the post-increment operator (j++) inside a compound literal. This occurs even though the code is valid and compiles successfully with standard C compilers like GCC or Clang.
If a simple integer (e.g., 42) is used instead of j++, c2rust handles the compound literal without any issues.
Source C code
#include<stdio.h>
struct s { 
    int i; 
};

int f(void) {
    struct s *p;
    int j = 42;
    p = &((struct s){j++}); // Compound literal with address-of operator and post-increment operator
    return p->i;
}

int main() {
    int result = f();
    printf("Result of f: %d\n", result);
    return 0;
}
Actual Behavior
c2rust fails to translate the function f and gives the following error:
$ c2rust-transpile compile_commands.json -e -o transpiled_code --binary runner
Transpiling runner.c
error: Failed to translate f: Expected no statements in field expression
Expected Behavior
c2rust should translate the compound literal with j++ into equivalent Rust code.
Observation
Interestingly, c2rust can translate the following C code perfectly, and the translated rust code prints 42
#include<stdio.h>
struct s { 
    int i; 
};

int f(void) {
    struct s *p;
    p = &((struct s){42}); // Compound literal with address-of operator
    return p->i;
}

int main() {
    int result = f();
    print("%d\n", result);
    return 0;
}

Duplicate issue #1171: c2rust Transpilation failure with Struct Initialization Using Expressions
Created: 2024-12-02T07:23:39Z
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
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:
$ 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.

Closing comment (2025-01-28T21:56:40Z): This is actually a duplicate of #1165; I just tested and this works as of #1188 merging.