Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upUsing #[repr(C)] on unit structs should warn #20660
Comments
kmcallister
added
A-lint
A-ffi
E-easy
labels
Jan 6, 2015
This comment has been minimized.
This comment has been minimized.
|
Mind if I try to fix this? |
This comment has been minimized.
This comment has been minimized.
|
I don't mind. |
This comment has been minimized.
This comment has been minimized.
|
In general, By the way, I think it would be dangerous to assume that |
This comment has been minimized.
This comment has been minimized.
|
Ack, sorry, what's a FFI position? |
This comment has been minimized.
This comment has been minimized.
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) |
This comment has been minimized.
This comment has been minimized.
|
Ah, that make sense. The |
cactorium
referenced this issue
Jan 7, 2015
Closed
Check for unit structs in improper_ctype lint #20694
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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! |
crumblingstatue
referenced this issue
Jul 26, 2015
Closed
Document the correct FFI equivalent to a C opaque struct #27303
This comment has been minimized.
This comment has been minimized.
|
When a zero-sized struct is used in an |
This comment has been minimized.
This comment has been minimized.
|
Yay! |
dgrunwald commentedJan 6, 2015
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.