Merge rust-bitcoin/rust-bitcoin#2972: Use index size rather than pointer size to enforce convertibility of `u32` to `usize`

c427d8b213 bitcoin: Compile time assert on index size (Tobin C. Harding)
49a6acc1a0 internals: Remove double parenthesis in const_assert (Tobin C. Harding)
2300b285ef units: Remove compile time pointer width check (Tobin C. Harding)

Pull request description:

  3 patches in preparation for other size related work, this PR does not touch the `ToU64` issue which will be handled separately.

  - Patch 1: Don't check pointer width in `units` because its not consensus code
  - Patch 2: Modify internal macro `const_assert`
  - Patch 3: Use index size to enforce not building on a 16 bit machine

ACKs for top commit:
  Kixunil:
    ACK c427d8b213 though I think the last commit was kinda a waste of time and it should have been adding the trait instead or leave it for later.
  apoelstra:
    ACK c427d8b213 successfully ran local tests; unsure if we want to merg this or wait for #3215

Tree-SHA512: 823df5b6a5af3265bce2422c00d287f45816faeb5f965685650ac974a1bd441cf548e25ac2962591732ff221bee91a55703da936382eb166c014ca5d4129edf8
This commit is contained in:
merge-script 2024-08-24 14:16:31 +00:00
commit 837fc9c9c2
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 15 additions and 15 deletions

View File

@ -37,12 +37,19 @@
#![allow(clippy::manual_range_contains)] // More readable than clippy's format. #![allow(clippy::manual_range_contains)] // More readable than clippy's format.
#![allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/12454 #![allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/12454
// Disable 16-bit support at least for now as we can't guarantee it yet. use core::mem;
#[cfg(target_pointer_width = "16")]
compile_error!( // We only support machines with index size of 4 bytes or more.
"rust-bitcoin currently only supports architectures with pointers wider than 16 bits, let us //
know if you want 16-bit support. Note that we do NOT guarantee that we will implement it!" // Bitcoin consensus code relies on being able to have containers with more than 65536 (2^16)
); // entries in them so we cannot support consensus logic on machines that only have 16-bit memory
// addresses.
//
// We specifically do not use `target_pointer_width` because of the possibility that pointer width
// does not equal index size.
//
// ref: https://github.com/rust-bitcoin/rust-bitcoin/pull/2929#discussion_r1661848565
internals::const_assert!(mem::size_of::<usize>() >= 4);
#[cfg(bench)] #[cfg(bench)]
extern crate test; extern crate test;

View File

@ -139,9 +139,9 @@ macro_rules! debug_from_display {
/// Asserts a boolean expression at compile time. /// Asserts a boolean expression at compile time.
#[macro_export] #[macro_export]
macro_rules! const_assert { macro_rules! const_assert {
($x:expr) => {{ ($x:expr) => {
const _: [(); 0 - !$x as usize] = []; const _: [(); 0 - !$x as usize] = [];
}}; };
} }
/// Derives `From<core::convert::Infallible>` for the given type. /// Derives `From<core::convert::Infallible>` for the given type.

View File

@ -14,13 +14,6 @@
#![allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/12454 #![allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/12454
#![no_std] #![no_std]
// Disable 16-bit support at least for now as we can't guarantee it yet.
#[cfg(target_pointer_width = "16")]
compile_error!(
"rust-bitcoin currently only supports architectures with pointers wider than 16 bits, let us
know if you want 16-bit support. Note that we do NOT guarantee that we will implement it!"
);
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
extern crate alloc; extern crate alloc;