Skip to content
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

Enums: Document that out-of-range values for fieldless repr(C) enums are UB #137

Closed
sdroege opened this issue May 30, 2019 · 4 comments
Closed

Comments

@sdroege
Copy link

sdroege commented May 30, 2019

See rust-lang/rust-memory-model#41

@RalfJung
Copy link
Member

This is basically subsumed by #69: any enum value must have a valid discriminant value.

@sdroege
Copy link
Author

sdroege commented May 31, 2019

It is, but I believe it would be useful to document this explicitly for repr(C) enums in addition to documenting it in general for all types of enums. I.e. that the attribute only affects the representation (the in memory representation is the same as in C), and that it does not cause the enum to behave like the same enum in C (being a fancy integer that can take any possible value, not just the ones defined as part of the enum).

This is something that I noticed being misunderstood by quite a few people in the past (including myself), who believed that repr(C) makes the enum be equivalent in all regards to the same enum in C. When passing via FFI this misunderstanding can easily lead to UB, and the correct solution would be to pass plain integers via FFI instead, checking on the Rust side if it's a valid value and then converting to an enum.

@RalfJung
Copy link
Member

I agree. Please leave that note in the other discussion. :)

@sdroege
Copy link
Author

sdroege commented May 31, 2019

Done, thanks :)

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

No branches or pull requests

2 participants