Skip to content
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

Merged
merged 1 commit into from Apr 1, 2024
Merged

Conversation

GKFX
Copy link
Contributor

@GKFX GKFX commented Mar 24, 2024

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.

bindgen/codegen/mod.rs Outdated Show resolved Hide resolved
@emilio
Copy link
Contributor

emilio commented Mar 26, 2024

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 format!() in the message or something along those lines?

@emilio
Copy link
Contributor

emilio commented Mar 26, 2024

(or does the assert macro print that already?)

@pvdrz
Copy link
Contributor

pvdrz commented Mar 26, 2024

There's no way to print such thing on const context AFAIK

@emilio
Copy link
Contributor

emilio commented Mar 26, 2024

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 [0u8][X - Y] or something like that which would panic if X was different to Y, and I would expect the compiler to print the bad index?

@pvdrz
Copy link
Contributor

pvdrz commented Mar 27, 2024

oh that's so hacky... I love it!

@GKFX GKFX requested a review from pvdrz March 29, 2024 16:35
@GKFX
Copy link
Contributor Author

GKFX commented Mar 29, 2024

Using slice indexing works. I've put the string as the expression in the array rather than 0u8 so it's visible.

@GKFX GKFX force-pushed the compile-time-layout-tests branch from cbb817f to d508b91 Compare March 29, 2024 19:16
@emilio emilio merged commit 4f78afa into rust-lang:main Apr 1, 2024
33 checks passed
@GKFX GKFX deleted the compile-time-layout-tests branch April 7, 2024 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: compile-time layout tests
4 participants