-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Zero-sized structs should be FFI-safe #17679
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
Comments
Strictly speaking, the grammar for the C language does not allow you to define structs with no members, and the specification confusingly claims separately that they have undefined behavior. The C++ language allows them, but requires that they have nonzero size. |
@zwarich: This is about struct definitions in Rust which don't have to follow C word by word. A tool for converting Rust struct definitions into C or C++ ought to be able to detect and remove zero-size members. |
GNU C supports zero-size structs, and they are zero-size (unlike C++ / GNU C++). It's similar to the situation with packed structs which aren't defined by standard C either. |
There may be a special case for zero-size structs by themselves being FFI-unsafe when used by value. |
Related #20660 I'm using 0-sized structures to represent unknown C types (C's |
@pornel: Rust has truly opaque types ( |
Raw pointers to anything are FFI safe, since they have a uniform representation. |
(edit: nevermind the compile problem) I've been assuming |
I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized. This issue has been moved to the RFCs repo: rust-lang/rfcs#760 |
If I get a struct by pointer from a foreign library, and I know the structure cannot safely be copied because the actual object is larger than the struct, I want to declare it as something like this:
Now the compiler complains at use site that the struct is not foreign-function-safe.
As the markers are zero-sized, they have no bearing on the layout and no consequences for the structure data, so they, as long as any other zero-sized structures, should not waive FFS.
@thestinger suggests the same should apply to structures with one field as well, but I'm not so certain about that: there could be optimizations regarding alignment which would not be compatible with the (default) C ABI.
The text was updated successfully, but these errors were encountered: