@@ -13,33 +13,48 @@ use core::{
1313
1414/// Static assertion without depending on const block which requires Rust 1.79.
1515macro_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