Skip to content

Support repr(C) for rustified enums #3265

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

thedataking
Copy link
Contributor

@thedataking thedataking commented Aug 14, 2025

TODOs:

  • check enum width as suggested by @emilio in this comment
  • should there be a --rustified-non-exhaustive-repr-c-enum flag?
  • Update CHANGELOG.md

Closes #3263.

@thedataking thedataking force-pushed the rustified-enum-repr-c branch from 33d5339 to 1885dc0 Compare August 14, 2025 07:39
thedataking pushed a commit to immunant/rust-bindgen that referenced this pull request Aug 15, 2025
Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! One nit

Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if we implement the suggested check for enum class Foo : uint8_t and so, we might not even need to add this switch? We could just repr(C) by default...

I think with this patch if you have something like:

enum class Foo : uint8_t {
};

Using this option will miscompile it.

At the very least we should add a big warning on this option since it'd cause wrong struct layout.

@thedataking
Copy link
Contributor Author

At the very least we should add a big warning on this option since it'd cause wrong struct layout.

I agree this would be good. However, it appears that LLVM does not expose the necessary APIs. Even if we exposed the necessary functionality, it would presumably only work for recent clang versions. Thoughts on how to proceed?

Per Larsen added 4 commits August 21, 2025 03:48
It is not possible to control the repr via custom attributes so add a
new rustified enum variant which does not use repr(u*) or repr(i*).
Using repr(C) is sometimes necessary to bindgen enums used in functions
subject to cross-language CFI checks.

Closes 3263.

Link: https://rcvalle.com/docs/rust-cfi-design-doc/
Signed-off-by: Per Larsen <[email protected]>
@thedataking thedataking force-pushed the rustified-enum-repr-c branch from ec7b724 to d020ef1 Compare August 21, 2025 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support repr(C) for enums
2 participants