-
Notifications
You must be signed in to change notification settings - Fork 701
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
Compile time layout tests #2787
Conversation
This looks great, though I think it's a requirement to make it the default to be able to tell how the values differ... Can we use |
(or does the assert macro print that already?) |
There's no way to print such thing on |
I haven't been able to play with this yet but maybe we can abuse slice indexing to at least print the differences between actual and expected? E.g for |
oh that's so hacky... I love it! |
Using slice indexing works. I've put the string as the expression in the array rather than |
cbb817f
to
d508b91
Compare
The new code generated by this change triggers a false-positive |
It also increased MSRV of the generated code to 1.77.0 which is the one that stabilized |
The MSRV of the generated code is configurable with Builder::rust_target. If using Bindgen 0.70 and Rust < 1.77.0 then the relevant target will need to be set. |
Ah yes, sorry for the noise. |
Biggest change change introduced in 0.70 is the compile-time layout tests to to check and validate layouts. Previously these tests were implemented as unit tests which made it somewhat difficult to run on cross-compiled targets. More info: * rust-lang/rust-bindgen#2786 * rust-lang/rust-bindgen#2787
Biggest change change introduced in bindgen 0.70 is the compile-time layout tests to to check and validate layouts. Previously these tests were implemented as unit tests which made it somewhat difficult to run on cross-compiled targets. Also, drop `rustfmt_bindings()` call had been enabled by default for a while and was removed/deprecated in bindgen 0.65.x releases. More info: * rust-lang/rust-bindgen#2786 * rust-lang/rust-bindgen#2787
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 As @GKFX noted, configuring 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. |
Generating bindings on one platform and using them on another is inherently risky, and I personally feel that the layout tests are doing their job here by alerting you to that fact. The difference between longs may have worked out OK but that doesn't mean everything else will. If you do expect the layout tests to fail I would disable them entirely with Builder::layout_tests rather than using the older version and just not running them. |
Thank you for the tip about disabling layout tests, it's indeed a much better solution for use cases like mine! I was initially reluctant to use it because I wanted to have layout test coverage for Linux and for the bindings that don't change between platforms, but I ended up not running such tests on Windows anyway because they failed, so your comment made me realize I was not compromising on much 👍 |
They are mainly useful to catch unexpected layout discrepancies across platforms, but we were already expecting such discrepancies on Windows, due to the bindings being pregenerated on Linux. It'd be great to still have these tests on Linux, but with current versions of bindgen that's not doable without resorting to increasing tech debt. (See rust-lang/rust-bindgen#2787 (comment))
I agree wholeheartedly with this. If you want to support different platforms, I think you should consider |
I agree that's the most correct solution in general, but in my case doing so bloats the crate size and reduces its portability (realistically, I cannot be bothered to pregenerate bindings for every platform users may want to target, and generating bindings on build is not ideal) for no gain, as the only risk with the generated bindings for my project I can see are layout tests not being portable. I already took care of excluding types and functions that referred to |
We allow to disable layout tests altogether don't we? Maybe generate one for the architecture you test on with tests, one elsewhere without? |
This commit emits layout tests as compile-time assertions, rather than as unit tests. This allows them to be run in situations where running unit tests is unfeasible, i.e. cross-compiled code and nostd targets.
Fixes #2786. @ojeda - I noticed you tagged the feature request for Rust-for-Linux.