Skip to content

Commit 9d1d40a

Browse files
committed
Auto merge of #2732 - est31:master, r=Amanieu
Two improvements of the latest i128 numbers change Two improvements of #2719: * First, a typo/grammar fix * Second, a fix to avoid usage of the anti pattern of `==` inside an `assert` macro. Since a few releases, `assert` is usable inside `const` expressions, but for the purpose of `==`, superior alternatives exist that also work at compile time but print the two values at a mismatch. `assert_eq` is not yet usable in const contexts due to formatting.
2 parents f00ea00 + e1d9927 commit 9d1d40a

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

build.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ fn main() {
8787
println!("cargo:rustc-cfg=libc_ptr_addr_of");
8888
}
8989

90-
// Rust >= 1.57.0 allows assert! (and panics in general) in constants.
91-
if rustc_minor_ver >= 57 || rustc_dep_of_std {
92-
println!("cargo:rustc-cfg=libc_const_assert");
90+
// Rust >= 1.37.0 allows underscores as anonymous constant names.
91+
if rustc_minor_ver >= 37 || rustc_dep_of_std {
92+
println!("cargo:rustc-cfg=libc_underscore_const_names");
9393
}
9494

9595
// #[thread_local] is currently unstable

src/fixed_width_ints.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ cfg_if! {
4444
// until the upstream rust issue is resolved, but this at least lets us make
4545
// progress on platforms where this type is important.
4646
//
47-
// The supported architectures and OSes is intentionally very restricted,
47+
// The list of supported architectures and OSes is intentionally very restricted,
4848
// as careful work needs to be done to verify that a particular platform
4949
// has a conformant ABI.
5050
//
@@ -60,35 +60,39 @@ cfg_if! {
6060
pub type __uint128_t = u128;
6161

6262
cfg_if! {
63-
if #[cfg(libc_const_assert)] {
63+
if #[cfg(libc_underscore_const_names)] {
64+
macro_rules! static_assert_eq {
65+
($a:expr, $b:expr) => {
66+
const _: [(); $a] = [(); $b];
67+
};
68+
}
69+
6470
// NOTE: if you add more platforms to here, you may need to cfg
6571
// these consts. They should always match the platform's values
6672
// for `sizeof(__int128)` and `_Alignof(__int128)`.
6773
const _SIZE_128: usize = 16;
6874
const _ALIGN_128: usize = 16;
6975

70-
/// Since Rust doesn't officially guarantee that these types
71-
/// have compatible ABIs, we const assert that these values have the
72-
/// known size/align of the target platform's libc. If rustc ever
73-
/// tries to regress things, it will cause a compilation error.
74-
///
75-
/// This isn't a bullet-proof solution because e.g. it doesn't
76-
/// catch the fact that llvm and gcc disagree on how x64 __int128
77-
/// is actually *passed* on the stack (clang underaligns it for
78-
/// the same reason that rustc *never* properly aligns it).
79-
const _ASSERT_128_COMPAT: () = {
80-
assert!(core::mem::size_of::<__int128>() == _SIZE_128);
81-
assert!(core::mem::align_of::<__int128>() == _ALIGN_128);
76+
// Since Rust doesn't officially guarantee that these types
77+
// have compatible ABIs, we const assert that these values have the
78+
// known size/align of the target platform's libc. If rustc ever
79+
// tries to regress things, it will cause a compilation error.
80+
//
81+
// This isn't a bullet-proof solution because e.g. it doesn't
82+
// catch the fact that llvm and gcc disagree on how x64 __int128
83+
// is actually *passed* on the stack (clang underaligns it for
84+
// the same reason that rustc *never* properly aligns it).
85+
static_assert_eq!(core::mem::size_of::<__int128>(), _SIZE_128);
86+
static_assert_eq!(core::mem::align_of::<__int128>(), _ALIGN_128);
8287

83-
assert!(core::mem::size_of::<__uint128>() == _SIZE_128);
84-
assert!(core::mem::align_of::<__uint128>() == _ALIGN_128);
88+
static_assert_eq!(core::mem::size_of::<__uint128>(), _SIZE_128);
89+
static_assert_eq!(core::mem::align_of::<__uint128>(), _ALIGN_128);
8590

86-
assert!(core::mem::size_of::<__int128_t>() == _SIZE_128);
87-
assert!(core::mem::align_of::<__int128_t>() == _ALIGN_128);
91+
static_assert_eq!(core::mem::size_of::<__int128_t>(), _SIZE_128);
92+
static_assert_eq!(core::mem::align_of::<__int128_t>(), _ALIGN_128);
8893

89-
assert!(core::mem::size_of::<__uint128_t>() == _SIZE_128);
90-
assert!(core::mem::align_of::<__uint128_t>() == _ALIGN_128);
91-
};
94+
static_assert_eq!(core::mem::size_of::<__uint128_t>(), _SIZE_128);
95+
static_assert_eq!(core::mem::align_of::<__uint128_t>(), _ALIGN_128);
9296
}
9397
}
9498
}

0 commit comments

Comments
 (0)