Compile time layout tests by GKFX · Pull Request #2787 · rust-lang/rust-bindgen (original) (raw)
A seemingly-overlooked side effect of moving layout tests to constant expressions is that C code which gets translated to different bindings in different platforms may no longer compile on platforms different than the one the bindings were generated in. This is especially inconvenient when the platform-specific differences in size and alignment do not affect correctness.
For instance, this problem has caused Windows builds to fail for a project I maintain when using bindings generated under Linux. In this case, the discrepancies stem from the long
type having different sizes on Windows and Linux, which is inconsequential for the purposes of my project, since such numeric types are referred to by their C aliases. Generating the bindings during the build for the current target plaform is not that practically feasible either, because that slows down compilation significantly and introduces additional build-time requirements.
As @GKFX noted, configuring bindgen
to target Rust 1.73 restores the previous codegen behavior for size and alignment tests. However, discovering this workaround was difficult, as I had to dig through the repository to locate this PR, and I think it's a bad long-term solution. This workaround means the generated bindings won't ever be able to take advantage of features introduced in newer Rust versions, and it's reasonable to expect that bindgen
maintainers may eventually drop support for targeting older Rust versions once doing so becomes enough of a burden.
Would it be feasible to make this behavior toggleable via an independent switch or, at the very least, document it more thoroughly? This could save others from encountering similar problems.