@@ -1363,10 +1363,13 @@ impl<T: ?Sized> *const T {
13631363 }
13641364
13651365 /// Returns whether the pointer is properly aligned for `T`.
1366+ // #[cfg(not(bootstrap))] -- Calling this function in a const context from the bootstrap
1367+ // compiler will always return false.
13661368 #[ must_use]
13671369 #[ inline]
13681370 #[ unstable( feature = "pointer_is_aligned" , issue = "96284" ) ]
1369- pub fn is_aligned ( self ) -> bool
1371+ #[ rustc_const_unstable( feature = "const_pointer_is_aligned" , issue = "none" ) ]
1372+ pub const fn is_aligned ( self ) -> bool
13701373 where
13711374 T : Sized ,
13721375 {
@@ -1381,16 +1384,26 @@ impl<T: ?Sized> *const T {
13811384 /// # Panics
13821385 ///
13831386 /// The function panics if `align` is not a power-of-two (this includes 0).
1387+ // #[cfg(not(bootstrap))] -- Calling this function in a const context from the bootstrap
1388+ // compiler will always return false.
13841389 #[ must_use]
13851390 #[ inline]
13861391 #[ unstable( feature = "pointer_is_aligned" , issue = "96284" ) ]
1387- pub fn is_aligned_to ( self , align : usize ) -> bool {
1388- if !align. is_power_of_two ( ) {
1389- panic ! ( "is_aligned_to: align is not a power-of-two" ) ;
1392+ #[ rustc_const_unstable( feature = "const_pointer_is_aligned" , issue = "none" ) ]
1393+ pub const fn is_aligned_to ( self , align : usize ) -> bool {
1394+ assert ! ( align. is_power_of_two( ) , "is_aligned_to: align is not a power-of-two" ) ;
1395+
1396+ #[ inline]
1397+ fn runtime ( ptr : * const u8 , align : usize ) -> bool {
1398+ ptr. addr ( ) & ( align - 1 ) == 0
1399+ }
1400+
1401+ const fn comptime ( ptr : * const u8 , align : usize ) -> bool {
1402+ ptr. align_offset ( align) == 0
13901403 }
13911404
1392- // Cast is needed for `T: !Sized`
1393- self . cast :: < u8 > ( ) . addr ( ) & align - 1 == 0
1405+ // SAFETY: `ptr.align_offset(align)` returns 0 if and only if the pointer is already aligned.
1406+ unsafe { intrinsics :: const_eval_select ( ( self . cast :: < u8 > ( ) , align) , comptime , runtime ) }
13941407 }
13951408}
13961409
0 commit comments