Skip to content

c2rust Translation Failure with Enums in Compound Literals #1168

@Yeaseen

Description

@Yeaseen

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; }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions