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 Translation Failure with Enums in Compound Literals #1168

Closed
Yeaseen opened this issue Nov 24, 2024 · 4 comments · Fixed by #1185
Closed

c2rust Translation Failure with Enums in Compound Literals #1168

Yeaseen opened this issue Nov 24, 2024 · 4 comments · Fixed by #1185
Labels
bug Something isn't working

Comments

@Yeaseen
Copy link
Contributor

Yeaseen commented Nov 24, 2024

Description

When translating C code that initializes a struct's integer field from an enum using memcpy with a compound literal, c2rust fails, indicating potential unimplemented handling or a bug in translating such memory operations.

Source C code

#include <stdio.h>
#include <string.h> 
typedef enum {
    b=2
} a;

typedef struct {
    int data; 
} d_struct;

d_struct d;
int main() {
    // Initialize d with the value of `b` using compound literal and memcpy
    memcpy(&d.data, &((a){b}), sizeof(a));
    printf("Stored enum value: %d\n", d.data);
    return 0; }

Output by GCC and Clang

Stored enum value: 2

godbolt view

Observed Behavior: Transpilation Failure with the following error message

$ c2rust-transpile compile_commands.json -e -o transpiled_code --binary runner
Transpiling runner.c
error: Failed to translate main: Init list not implemented for Enum(CDeclId(1131))

I think this is related to memcpy as the following alternative for memcpy is correctly handled by c2rust

#include <stdio.h>
typedef enum {
    b=2
} a;

typedef struct {
    int data; 
} d_struct;

d_struct d;

int main() {
    a tmp = b;  
    *(int*)&d.data = tmp;
    printf("Stored enum value: %d\n", d.data);
    return 0; }
@fw-immunant
Copy link
Contributor

This is not an issue with memcpy but with translation of enums in compound literals; the below code is a minimal reproducer:

typedef enum {
    b=2
} a;

int main() {
    (a){b};
    return 0;
}

@fw-immunant fw-immunant added the bug Something isn't working label Dec 4, 2024
@fw-immunant
Copy link
Contributor

This seems to resolve the error:

diff --git a/c2rust-transpile/src/translator/literals.rs b/c2rust-transpile/src/translator/literals.rs
index d8eb64db6..861bb1467 100644
--- a/c2rust-transpile/src/translator/literals.rs
+++ b/c2rust-transpile/src/translator/literals.rs
@@ -246,6 +246,10 @@ impl<'c> Translation<'c> {
                 let id = ids.first().unwrap();
                 self.convert_expr(ctx.used(), *id)
             }
+            CTypeKind::Enum(_) => {
+                let id = ids.first().unwrap();
+                self.convert_expr(ctx.used(), *id)
+            }
             CTypeKind::Vector(CQualTypeId { ctype, .. }, len) => {
                 self.vector_list_initializer(ctx, ids, ctype, len)
             }

@Yeaseen Yeaseen changed the title c2rust Translation Failure with memcpy Used on Enum Compound Literals c2rust Translation Failure with Enums in Compound Literals Dec 4, 2024
@Yeaseen
Copy link
Contributor Author

Yeaseen commented Dec 4, 2024

Thank you for your response, @fw-immunant
I updated the title.

Should I give it a try and create a PR for this?

@kkysen
Copy link
Contributor

kkysen commented Dec 6, 2024

Should I give it a try and create a PR for this?

Yeah, that would be great! Thanks!

kkysen pushed a commit that referenced this issue Dec 6, 2024
…#1185)

* Fixes #1168.
   
### Modification:
- Suggested changes mentioned in #1168 were added in the
`c2rust-transpile/src/translator/literals.rs` file
- Added a relevant C program in `tests/enums/src/enum_compound.c` file
- Modified `tests/enums/src/test_enums.rs` file to check the correct
outputs

### Testing

The added test case specifically checks for the correct assignment of
enum values in buffer initialization using compound literals. This test
has been integrated into the existing test suite and passed
successfully, confirming the fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants