@@ -1344,10 +1344,13 @@ impl<T: ?Sized> *const T {
13441344 }
13451345
13461346 /// Returns whether the pointer is properly aligned for `T`.
1347+ // #[cfg(not(bootstrap))] -- Calling this function in a const context from the bootstrap
1348+ // compiler will always return false.
13471349 #[ must_use]
13481350 #[ inline]
13491351 #[ unstable( feature = "pointer_is_aligned" , issue = "96284" ) ]
1350- pub fn is_aligned ( self ) -> bool
1352+ #[ rustc_const_unstable( feature = "const_pointer_is_aligned" , issue = "none" ) ]
1353+ pub const fn is_aligned ( self ) -> bool
13511354 where
13521355 T : Sized ,
13531356 {
@@ -1362,16 +1365,26 @@ impl<T: ?Sized> *const T {
13621365 /// # Panics
13631366 ///
13641367 /// The function panics if `align` is not a power-of-two (this includes 0).
1368+ // #[cfg(not(bootstrap))] -- Calling this function in a const context from the bootstrap
1369+ // compiler will always return false.
13651370 #[ must_use]
13661371 #[ inline]
13671372 #[ unstable( feature = "pointer_is_aligned" , issue = "96284" ) ]
1368- pub fn is_aligned_to ( self , align : usize ) -> bool {
1369- if !align. is_power_of_two ( ) {
1370- panic ! ( "is_aligned_to: align is not a power-of-two" ) ;
1373+ #[ rustc_const_unstable( feature = "const_pointer_is_aligned" , issue = "none" ) ]
1374+ pub const fn is_aligned_to ( self , align : usize ) -> bool {
1375+ assert ! ( align. is_power_of_two( ) , "is_aligned_to: align is not a power-of-two" ) ;
1376+
1377+ #[ inline]
1378+ fn runtime ( ptr : * const u8 , align : usize ) -> bool {
1379+ ptr. addr ( ) & ( align - 1 ) == 0
1380+ }
1381+
1382+ const fn comptime ( ptr : * const u8 , align : usize ) -> bool {
1383+ ptr. align_offset ( align) == 0
13711384 }
13721385
1373- // Cast is needed for `T: !Sized`
1374- self . cast :: < u8 > ( ) . addr ( ) & align - 1 == 0
1386+ // SAFETY: `ptr.align_offset(align)` returns 0 if and only if the pointer is already aligned.
1387+ unsafe { intrinsics :: const_eval_select ( ( self . cast :: < u8 > ( ) , align) , comptime , runtime ) }
13751388 }
13761389}
13771390
0 commit comments