Skip to content

Commit b2930f2

Browse files
committed
Replace const_hint! helper with full const evaluation helper
1 parent f2aa961 commit b2930f2

3 files changed

Lines changed: 49 additions & 34 deletions

File tree

build.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn main() {
3939
// Custom cfgs set by build script. Not public API.
4040
// grep -F 'cargo:rustc-cfg=' build.rs | grep -Ev '^ *//' | sed -E 's/^.*cargo:rustc-cfg=//; s/(=\\)?".*$//' | LC_ALL=C sort -u | tr '\n' ',' | sed -E 's/,$/\n/'
4141
println!(
42-
"cargo:rustc-check-cfg=cfg(atomic_maybe_uninit_no_asm,atomic_maybe_uninit_no_cmpxchg,atomic_maybe_uninit_no_cmpxchg8b,atomic_maybe_uninit_no_const_mut_refs,atomic_maybe_uninit_no_diagnostic_namespace,atomic_maybe_uninit_no_inline_const,atomic_maybe_uninit_no_strict_provenance,atomic_maybe_uninit_no_sync,atomic_maybe_uninit_pre_llvm_20,atomic_maybe_uninit_target_feature,atomic_maybe_uninit_unstable_asm_experimental_arch)"
42+
"cargo:rustc-check-cfg=cfg(atomic_maybe_uninit_no_asm,atomic_maybe_uninit_no_cmpxchg,atomic_maybe_uninit_no_cmpxchg8b,atomic_maybe_uninit_no_const_mut_refs,atomic_maybe_uninit_no_diagnostic_namespace,atomic_maybe_uninit_no_strict_provenance,atomic_maybe_uninit_no_sync,atomic_maybe_uninit_pre_llvm_20,atomic_maybe_uninit_target_feature,atomic_maybe_uninit_unstable_asm_experimental_arch)"
4343
);
4444
// TODO: handle multi-line target_feature_fallback
4545
// grep -F 'target_feature_fallback("' build.rs | grep -Ev '^ *//' | sed -E 's/^.*target_feature_fallback\(//; s/",.*$/"/' | LC_ALL=C sort -u | tr '\n' ',' | sed -E 's/,$/\n/'
@@ -72,10 +72,6 @@ fn main() {
7272
if !version.probe(78, 2024, 3, 8) {
7373
println!("cargo:rustc-cfg=atomic_maybe_uninit_no_diagnostic_namespace");
7474
}
75-
// inline_const stabilized in Rust 1.79 (nightly-2024-04-25): https://github.com/rust-lang/rust/pull/104087
76-
if !version.probe(79, 2024, 4, 24) {
77-
println!("cargo:rustc-cfg=atomic_maybe_uninit_no_inline_const");
78-
}
7975
// const_mut_refs/const_refs_to_cell stabilized in Rust 1.83 (nightly-2024-09-16): https://github.com/rust-lang/rust/pull/129195
8076
if !version.probe(83, 2024, 9, 15) {
8177
println!("cargo:rustc-cfg=atomic_maybe_uninit_no_const_mut_refs");

src/arch/s390x.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ macro_rules! serialization {
4949
// Extracts and checks condition code.
5050
#[inline(always)]
5151
const fn extract_cc(r: i64) -> bool {
52-
r.wrapping_add(-268435456) & const_hint!({ 1 << 31 }) != 0
52+
const BIT: i64 = 1 << 31;
53+
r.wrapping_add(-268435456) & BIT != 0
5354
}
5455

5556
macro_rules! atomic_load_store {

src/utils.rs

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,48 @@ use core::{
1313

1414
/// Static assertion without depending on const block which requires Rust 1.79.
1515
macro_rules! static_assert {
16-
($($ty:ident),+ => $($tt:tt)*) => {{
17-
// Inspired by https://github.com/nvzqz/static-assertions/issues/40#issuecomment-1458897730.
18-
struct _Assert<$($ty),+>($($ty),+);
19-
impl<$($ty),+> _Assert<$($ty,)*> {
20-
const _CHECK: () = assert!($($tt)*);
21-
}
22-
_Assert::<$($ty,)*>::_CHECK
16+
($(const $consts:ident: $ty:ty),+ => $($tt:tt)*) => {{
17+
const_eval!($(const $consts: $ty),+ => () { assert!($($tt)*) })
18+
}};
19+
($($ty_params:ident $(: ?Sized $(+ $bounds:path)?)?),+ => $($tt:tt)*) => {{
20+
const_eval!($($ty_params $(: ?Sized $(+ $bounds)?)?),+ => () { assert!($($tt)*) })
2321
}};
22+
($($ty_params:ident $(: $bounds:path)?),+ => $($tt:tt)*) => {{
23+
const_eval!($($ty_params $(: $bounds)?),+ => () { assert!($($tt)*) })
24+
}};
25+
// Use const _: () = assert!(..) for no parameter cases instead.
2426
}
2527

26-
/// Uses inline const if available.
27-
///
28-
/// As the name implies, this is intended as a hint. Do not use this for use case
29-
/// that relies on this being computed in a const context (use static_assert instead).
30-
#[cfg(not(atomic_maybe_uninit_no_inline_const))]
31-
#[allow(unused_macros)]
32-
macro_rules! const_hint {
33-
({$($tt:tt)*}) => {
34-
const { $($tt)* }
35-
}
36-
}
37-
#[cfg(atomic_maybe_uninit_no_inline_const)]
38-
#[allow(unused_macros)]
39-
macro_rules! const_hint {
40-
({$($tt:tt)*}) => {
41-
{ $($tt)* }
42-
}
28+
/// Const block emulation without depending on real const block which requires Rust 1.79.
29+
// Inspired by https://github.com/nvzqz/static-assertions/issues/40#issuecomment-1458897730.
30+
macro_rules! const_eval {
31+
($(const $const_params:ident: $ty:ty),+ => $ret:ty { $($tt:tt)* }) => {{
32+
struct _Tmp<$(const $const_params: $ty,)*>;
33+
impl<$(const $const_params: $ty,)*> _Tmp<$($const_params,)*> {
34+
const _VAL: $ret = { $($tt)* };
35+
}
36+
_Tmp::<$($const_params,)*>::_VAL
37+
}};
38+
($($ty_params:ident $(: ?Sized $(+ $bounds:path)?)?),+ => $ret:ty { $($tt:tt)* }) => {{
39+
struct _Tmp<$($ty_params $(: ?Sized $(+ $bounds)?)?),+>(
40+
$(::core::marker::PhantomData<$ty_params>),+
41+
);
42+
impl<$($ty_params $(: ?Sized $(+ $bounds)?)?),+> _Tmp<$($ty_params,)*> {
43+
const _VAL: $ret = { $($tt)* };
44+
}
45+
_Tmp::<$($ty_params,)*>::_VAL
46+
}};
47+
($($ty_params:ident $(: $bounds:path)?),+ => $ret:ty { $($tt:tt)* }) => {{
48+
struct _Tmp<$($ty_params $(: $bounds)?),+>($(::core::marker::PhantomData<$ty_params>),+);
49+
impl<$($ty_params $(: $bounds)?),+> _Tmp<$($ty_params,)*> {
50+
const _VAL: $ret = { $($tt)* };
51+
}
52+
_Tmp::<$($ty_params,)*>::_VAL
53+
}};
54+
(=> $ret:ty { $($tt:tt)* }) => {{
55+
const _VAL: $ret = { $($tt)* };
56+
_VAL
57+
}};
4358
}
4459

4560
/// Make the given function const if the given condition is true.
@@ -152,7 +167,9 @@ macro_rules! debug_assert_atomic_unsafe_precondition {
152167
#[allow(clippy::arithmetic_side_effects)]
153168
{
154169
// Using const block here improves codegen on opt-level=0 https://godbolt.org/z/vrrvTqGda
155-
debug_assert!($ptr.addr() & const_hint!({ core::mem::size_of::<$ty>() - 1 }) == 0);
170+
debug_assert!(
171+
$ptr.addr() & const_eval!(=> usize { core::mem::size_of::<$ty>() - 1 }) == 0
172+
);
156173
}
157174
}};
158175
}
@@ -496,7 +513,8 @@ pub(crate) fn create_sub_word_mask_values<T>(ptr: *mut T) -> (*mut MinWord, RetI
496513
target_arch = "xtensa",
497514
));
498515
const PTR_MASK: usize = mem::size_of::<MinWord>() - 1;
499-
let aligned_ptr = ptr.with_addr(ptr.addr() & const_hint!({ !PTR_MASK })).cast::<MinWord>();
516+
const PTR_INV_MASK: usize = !PTR_MASK;
517+
let aligned_ptr = ptr.with_addr(ptr.addr() & PTR_INV_MASK).cast::<MinWord>();
500518
let ptr_lsb = if SHIFT_MASK {
501519
ptr.addr() & PTR_MASK
502520
} else {
@@ -507,10 +525,10 @@ pub(crate) fn create_sub_word_mask_values<T>(ptr: *mut T) -> (*mut MinWord, RetI
507525
let shift = if cfg!(any(target_endian = "little", target_arch = "s390x")) {
508526
ptr_lsb << 3
509527
} else {
510-
(ptr_lsb ^ const_hint!({ mem::size_of::<MinWord>() - mem::size_of::<T>() })) << 3
528+
(ptr_lsb ^ const_eval!(T => usize { mem::size_of::<MinWord>() - mem::size_of::<T>() })) << 3
511529
};
512530
#[allow(clippy::arithmetic_side_effects)]
513-
let mut mask: RetInt = const_hint!({ (1 << (mem::size_of::<T>() << 3)) - 1 }); // !(0 as T) as RetInt
531+
let mut mask = const_eval!(T => RetInt { (1 << (mem::size_of::<T>() << 3)) - 1 }); // !(0 as T) as RetInt
514532
if SHIFT_MASK {
515533
mask <<= shift;
516534
}

0 commit comments

Comments
 (0)