jameysharp / corrode

C to Rust translator
GNU General Public License v2.0
2.16k stars 113 forks source link

implement union #22

Open Cortlandd opened 8 years ago

Cortlandd commented 8 years ago

C file:

// test.c
#include <stdio.h>
struct Fish {
  char type[];
  int age;
} fish;

I run ~$corrode test.c and get

corrode: ("/usr/include/wchar.h": line 85): Corrode doesn't handle this yet:
    union {
        unsigned int __wch; char __wchb[4];
    }

Any tips on why this has happened?

anp commented 8 years ago

For one thing, Rust is still in the process of implementing support for untagged unions in rust-lang/rust#32836.

jameysharp commented 8 years ago

Yeah, since Rust doesn't support C-style unions quite yet, Corrode doesn't either. Thanks @dikaiosune for the pointer to the work in progress there!

I'm going to repurpose this issue as a tracking bug for Corrode's current lack of support for unions, since I hadn't filed one about that defect yet.

Other advice on your specific test case, @Cortlandd:

Unfortunately, a lot of standard system headers declare unions, or enums, or other things Corrode doesn't support yet. For testing purposes, try writing source files that don't #include anything, and just copy the declarations you need. For example, if you want to call printf, use this instead of #include <stdio.h>:

extern int printf (const char *format, ...);

You should also verify that your C source compiles with a normal C compiler like gcc or clang. I think your char type[]; field is not legal C. If I'm not mistaken, an array field with no size specified can only appear at the end of a struct, right? At any rate, Corrode currently translates arrays incorrectly, treating them as if they were just pointers.

Cortlandd commented 8 years ago

Thanks! This is helpful

jameysharp commented 8 years ago

I don't actually want to close this issue until Corrode has support for unions. :smile:

meagon commented 8 years ago

shall we use union  implementation Just as rust-bindgen did; rust-bindgen support union and enum

and we can pass union and enum error as a replacement

flip111 commented 8 years ago

Is it possible to have a workaround until rust supports union? Maybe use enumerations somehow?

jameysharp commented 8 years ago

As a first step, Corrode now translates union types to non-constructable and non-copyable enums. This supports any code that only passes around pointers to union types, but doesn't try to access the contents of any union.

In particular, stdio.h translates without Corrode complaining now! However the generated Rust doesn't compile for other reasons.

I won't close this issue until Corrode translates unions to something complete on the Rust side, but for now this should let people translate a lot more code successfully.

jameysharp commented 8 years ago

I'm at RustConf today and @joshtriplett tells me that support for C-style unions has landed on Rust nightly. Hooray! Anyone want to try making Corrode use the new union syntax?