corrode icon indicating copy to clipboard operation
corrode copied to clipboard

implement union

Open Cortlandd opened this issue 8 years ago • 8 comments

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?

Cortlandd avatar Jul 08 '16 16:07 Cortlandd

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

anp avatar Jul 08 '16 17:07 anp

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.

jameysharp avatar Jul 08 '16 18:07 jameysharp

Thanks! This is helpful

Cortlandd avatar Jul 08 '16 21:07 Cortlandd

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

jameysharp avatar Jul 08 '16 21:07 jameysharp

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

meagon avatar Jul 11 '16 02:07 meagon

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

flip111 avatar Jul 12 '16 14:07 flip111

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 avatar Jul 24 '16 20:07 jameysharp

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?

jameysharp avatar Sep 10 '16 17:09 jameysharp