-
Notifications
You must be signed in to change notification settings - Fork 707
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
Default to generating constified enums, rather than generating Rust enums #758
Comments
Hi! If you have any questions regarding this issue, feel free to make a comment here, or ask it in the If you intend to work on this issue, then add |
@highfive I am working on this issue. |
Hi @jprusakova -- still hacking on this? Anything I can do to help out? |
I'm slightly concerned about using const enums, because their size is not well defined. (From the spec for C11)
This means that you can't assume a particular size of an enum |
@derekdreery, We still have this complication when generating Rust As far as choosing a size goes, regardless which Rust code we're emitting, we rely on |
Doesn't |
I was under the impression that rust-lang/rust#28925 seems relevant, where at least some folks are expecting the behavior you're describing, but it doesn't seem to always be correct. |
Yeah, that discussion is illuminating. You cannot possibly encode every algorithm in every compiler. But it also means that C libraries are not necessarily ABI compatible with each other. I guess the lesson is "don't use enums in C", and we in rust have to do the best we can (by using large types and hoping for the best :/). In that case constants are definitely better than enums - apart from anything else they are simpler. EDIT: don't expose enums. |
Well, libraries use to be compatible. Some of them have hacks to force enum size: |
But yeah, I agree that the situation is quite bad, you can compile with some compiler options to force this layout optimizations and what not, breaking all sort of stuff if it's not compiled the same way. |
@highfive: assign me (I started on this, have made progress, and think I will be able to finish it, hopefully today?). |
It looks like this has already been assigned to someone. I'll leave the decision to a core contributor. |
@Cldfire sounds good! This bug has been quiet, so I don't think @jprusakova is still working on it. |
Status update: I have all tests passing locally and have pushed to my fork (https://github.com/Cldfire/rust-bindgen/commit/ca206562a2d9e891721a0d8e932f114393d596c0). I still have a few things I want to think about before I make a PR, though, and I'm done working on it for tonight. I'll get back to it tomorrow night and / or Sunday. |
Generate constants for enums by default Closes #758. The first commit does strictly what the issue description described, the second does a small amount of (what I believe is) logic simplification. Hopefully I didn't miss any tests when adding the `--rustified-enum .*` flag to the ones that needed it; all of the tests are passing for me, though, so I don't think I did.
e.g. ``` cargo build --features bindgen cp target/**/bindings.rs src/bindings.rs ``` This might not be a good idea: rust-lang/rust-bindgen#758 rust-lang/rust#36927
Right now, we translate C/C++ enums into Rust enums by default. This is problematic because it is OK for C/C++ code to return some
int
that isn't one of the enum variants, and that is well defined for C/C++; however, that is UB in Rust and can lead to miscompilations. People shouldn't be using Rust enums unless they have complete control of the C/C++ code (ie not using a shared library and have audited the C/C++ code themselves). Therefore, it definitely shouldn't be the default!The changes involved are to
replace the
Builder::constified_enum
method with aBuilder::rustified_enum
method insrc/lib.rs
replace the
--constified-enum
flag with a--rustified-enum
flag insrc/options.rs
update the way we determine if an enum is constified or a rust enum in
src/codegen/mod.rs
. Do agit grep constified src/codegen
to see the relevant code paths.For all the test header files in
tests/headers/*
that contain enums and do not have a--constified-enum
flag in the// bindgen-flags:
comment, add a--rustified-enum .*
to the// bindgen-flags:
commentRemove
--constified-enum whatever
flags from all test headers in thetests/headers/*
filesThe text was updated successfully, but these errors were encountered: