Zero-sized structs in C types

Currently, rustc warns if structures passed to C contain zero sized elements. I am building representations of structs (using bindgen) for running Rust on Zephyr, and have found a few structs, that depending on the configuration, end up as zero sized. It seems that both gcc and clang support this. Given that two significant toolchains support zero-sized structs in C code, what would be the possibility of having a way to suppress this warning (without suppressing other reasons for things to be c-unsafe). Or have I missed something, and there is already a way to suppress this particular warning.

Currently, I've enabled a workaround that is used for C++, which doesn't allow the zero-sized structs, but it'd be nice to avoid the wasted space of dummy elements defined by the workaround.

The problem is that different C compilers do different things with zero-sized structs. The warning is there to tell you that the layout you are getting in Rust might not be matching the layout that you are getting in C.

Also see repr(C) is unsound on MSVC targets · Issue #81996 · rust-lang/rust · GitHub, in particular the summary here.

5 Likes

Is there a way to suppress this warning, ideally just this one, and not the whole C-FFI warnings? In my specific case, I do know what my compiler is doing, and bindgen even generates code to verify that the behavior is the same.

Have you tried #[allow()]ing it on just the types and/or functions in question?

So, the problem here is that bindgen doesn't (currently) provide any way to add annotations to selected types. In my case, I just allowed it for the entire bindgen-generated output, which is likely to be ok, given that they come from C declarations.

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.