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
assert platform-minimum requirements at build time #3797
Conversation
tokio/build.rs
Outdated
if mem::size_of::<usize>() < 4 { | ||
panic!("Tokio only works correctly if usize is at least 4 bytes."); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about cross-compilation? Then the build script is not built in the target architecture. Another option is this:
#[cfg_attr(target_pointer_width = "16", compile_error("Pointer width must be at least 32"))]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. While strictly speaking not exhaustive, that's probably good enough until an MSRV bump. If someone designs a 31-bit hobby platform today they only have themselves to blame. :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this until we have sufficient const eval?
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64", target_pointer_width = "128")))]
compile_error! {
"Tokio requires the platform pointer width to be 32, 64, or 128 bits"
}
Adds assertions in
build.rs
which ensures that the platform being built for supports the minimum required by Tokio.Motivation
Tokio currently makes a number of soft assumptions about the size of a
usize
(andisize
), usually through the form of casting au32
like:Arguably the preferable way to do this would be to perform a checked conversion using
TryFrom
, but it would be a lot of work to trace all of them down and it might not always be immediately feasible when it comes to things like bit flags.It is unlikely that someone will ever attempt to build Tokio for a platform where
usize
is not at least 4 bytes, but at least when they do, they should be firmly shown that it's a bad idea until assumptions aroundusize
et. al. have been fixed.Solution
Add assertions to
build.rs
. I started tinkering with constant assertions, but Rust1.45
is a bit too limiting to make them ergonomical. While it's possible thatbuild.rs
is run with a differentrustc
than the one used to compile the project, one would have to do something strange to have it do so.