-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Using #[repr(C)] on unit structs should warn #20660
Comments
Mind if I try to fix this? |
I don't mind. |
In general, By the way, I think it would be dangerous to assume that |
Ack, sorry, what's a FFI position? |
tomjakubowski means that a warning should be issued in the case that a unit struct is used in FFI code (for example, in a call to an external C function) |
Ah, that make sense. The |
I forgot that there is another possible use of unit structs in FFI code: representing incomplete types. Problems occur only when a unit struct is passed by value, or if we pass a pointer to a struct that contains a unit struct. |
Sorry for taking so long to reply, it's taking a while to figure out how to do this. We can't not recurse, since we need to check for structs inside structs that have unit structs as fields, so I've been reading a lot of the AST and type tree source to figure out how to tell a pointer to a struct from a struct and how they're all represented in the trees, which has been oddly difficult. Getting there though! |
When a zero-sized struct is used in an |
Yay! |
The C language does not allow empty structs.
gcc and clang allow them as a language extension, and they have size 0 there. With --pedantic, both will emit a warning on empty C structs.
msvc refuses to compile C code using empty structs.
In C++, empty structs are allowed -- but there, they have size 1!
In rust, a #[repr(C)] unit struct is given size 0. This makes it compatible with gcc and clang in C mode; but has the potential to cause trouble when interfacing with a C++ library.
I think the improper_ctypes lint should issue a warning when #[repr(C)] is applied to a size 0 struct.
Users can suppress the warning when interfacing with C code using the gcc extensions.
The text was updated successfully, but these errors were encountered: