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

implement union #22

Open
Cortlandd opened this Issue Jul 8, 2016 · 8 comments

Comments

Projects
None yet
5 participants
@Cortlandd
Copy link

Cortlandd commented Jul 8, 2016

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

This comment has been minimized.

Copy link

anp commented Jul 8, 2016

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

@jameysharp

This comment has been minimized.

Copy link
Owner

jameysharp commented Jul 8, 2016

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 jameysharp changed the title Corrode support error implement union Jul 8, 2016

@Cortlandd

This comment has been minimized.

Copy link
Author

Cortlandd commented Jul 8, 2016

Thanks! This is helpful

@Cortlandd Cortlandd closed this Jul 8, 2016

@jameysharp

This comment has been minimized.

Copy link
Owner

jameysharp commented Jul 8, 2016

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

@meagon

This comment has been minimized.

Copy link

meagon commented Jul 11, 2016

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

This comment has been minimized.

Copy link

flip111 commented Jul 12, 2016

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

jameysharp added a commit that referenced this issue Jul 24, 2016

Treat unions as incomplete types.
This allows many instances of issue #22 without having to fully
translate unions. Anything which refers to a union type without trying
to construct it, copy it, or access its members, will work fine now.
@jameysharp

This comment has been minimized.

Copy link
Owner

jameysharp commented Jul 24, 2016

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

This comment has been minimized.

Copy link
Owner

jameysharp commented Sep 10, 2016

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment