@@ -1344,10 +1344,13 @@ impl<T: ?Sized> *const T {
1344
1344
}
1345
1345
1346
1346
/// 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.
1347
1349
#[ must_use]
1348
1350
#[ inline]
1349
1351
#[ 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
1351
1354
where
1352
1355
T : Sized ,
1353
1356
{
@@ -1362,16 +1365,26 @@ impl<T: ?Sized> *const T {
1362
1365
/// # Panics
1363
1366
///
1364
1367
/// 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.
1365
1370
#[ must_use]
1366
1371
#[ inline]
1367
1372
#[ 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
1371
1384
}
1372
1385
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 ) }
1375
1388
}
1376
1389
}
1377
1390
0 commit comments