Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2e42e8

Browse files
committedMar 3, 2024
downgrade ptr.is_aligned_to crate-private
Per #96284 (comment) this API is a mess and is regarded as insufficiently motivated. Removing it from the public API is potentially the only blocker on stabilizing its pleasant brother .is_aligned(). I initially just deleted it completely but it's since become Heavily Used for the purposes of assert_unsafe_precondition, and there's clearly Some Stuff going on with that API that I have no interest in poking.
1 parent 5119208 commit a2e42e8

File tree

9 files changed

+41
-379
lines changed

9 files changed

+41
-379
lines changed
 

‎library/core/src/ptr/const_ptr.rs

Lines changed: 13 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,116 +1501,25 @@ impl<T: ?Sized> *const T {
15011501
/// For non-`Sized` pointees this operation considers only the data pointer,
15021502
/// ignoring the metadata.
15031503
///
1504-
/// # Panics
1505-
///
1506-
/// The function panics if `align` is not a power-of-two (this includes 0).
1507-
///
1508-
/// # Examples
1509-
///
1510-
/// ```
1511-
/// #![feature(pointer_is_aligned)]
1512-
///
1513-
/// // On some platforms, the alignment of i32 is less than 4.
1514-
/// #[repr(align(4))]
1515-
/// struct AlignedI32(i32);
1516-
///
1517-
/// let data = AlignedI32(42);
1518-
/// let ptr = &data as *const AlignedI32;
1519-
///
1520-
/// assert!(ptr.is_aligned_to(1));
1521-
/// assert!(ptr.is_aligned_to(2));
1522-
/// assert!(ptr.is_aligned_to(4));
1523-
///
1524-
/// assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
1525-
/// assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
1526-
///
1527-
/// assert_ne!(ptr.is_aligned_to(8), ptr.wrapping_add(1).is_aligned_to(8));
1528-
/// ```
1529-
///
1530-
/// # At compiletime
1531-
/// **Note: Alignment at compiletime is experimental and subject to change. See the
1532-
/// [tracking issue] for details.**
1533-
///
1534-
/// At compiletime, the compiler may not know where a value will end up in memory.
1535-
/// Calling this function on a pointer created from a reference at compiletime will only
1536-
/// return `true` if the pointer is guaranteed to be aligned. This means that the pointer
1537-
/// cannot be stricter aligned than the reference's underlying allocation.
1538-
///
1539-
/// ```
1540-
/// #![feature(pointer_is_aligned)]
1541-
/// #![feature(const_pointer_is_aligned)]
1504+
/// This is an internal implementation detail of the stdlib, for implementing
1505+
/// low-level things like assert_unsafe_precondition.
15421506
///
1543-
/// // On some platforms, the alignment of i32 is less than 4.
1544-
/// #[repr(align(4))]
1545-
/// struct AlignedI32(i32);
1546-
///
1547-
/// const _: () = {
1548-
/// let data = AlignedI32(42);
1549-
/// let ptr = &data as *const AlignedI32;
1550-
///
1551-
/// assert!(ptr.is_aligned_to(1));
1552-
/// assert!(ptr.is_aligned_to(2));
1553-
/// assert!(ptr.is_aligned_to(4));
1554-
///
1555-
/// // At compiletime, we know for sure that the pointer isn't aligned to 8.
1556-
/// assert!(!ptr.is_aligned_to(8));
1557-
/// assert!(!ptr.wrapping_add(1).is_aligned_to(8));
1558-
/// };
1559-
/// ```
1560-
///
1561-
/// Due to this behavior, it is possible that a runtime pointer derived from a compiletime
1562-
/// pointer is aligned, even if the compiletime pointer wasn't aligned.
1563-
///
1564-
/// ```
1565-
/// #![feature(pointer_is_aligned)]
1566-
/// #![feature(const_pointer_is_aligned)]
1567-
///
1568-
/// // On some platforms, the alignment of i32 is less than 4.
1569-
/// #[repr(align(4))]
1570-
/// struct AlignedI32(i32);
1571-
///
1572-
/// // At compiletime, neither `COMPTIME_PTR` nor `COMPTIME_PTR + 1` is aligned.
1573-
/// const COMPTIME_PTR: *const AlignedI32 = &AlignedI32(42);
1574-
/// const _: () = assert!(!COMPTIME_PTR.is_aligned_to(8));
1575-
/// const _: () = assert!(!COMPTIME_PTR.wrapping_add(1).is_aligned_to(8));
1576-
///
1577-
/// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
1578-
/// let runtime_ptr = COMPTIME_PTR;
1579-
/// assert_ne!(
1580-
/// runtime_ptr.is_aligned_to(8),
1581-
/// runtime_ptr.wrapping_add(1).is_aligned_to(8),
1582-
/// );
1583-
/// ```
1584-
///
1585-
/// If a pointer is created from a fixed address, this function behaves the same during
1586-
/// runtime and compiletime.
1587-
///
1588-
/// ```
1589-
/// #![feature(pointer_is_aligned)]
1590-
/// #![feature(const_pointer_is_aligned)]
1591-
///
1592-
/// const _: () = {
1593-
/// let ptr = 40 as *const u8;
1594-
/// assert!(ptr.is_aligned_to(1));
1595-
/// assert!(ptr.is_aligned_to(2));
1596-
/// assert!(ptr.is_aligned_to(4));
1597-
/// assert!(ptr.is_aligned_to(8));
1598-
/// assert!(!ptr.is_aligned_to(16));
1599-
/// };
1600-
/// ```
1507+
/// # Safety
16011508
///
1602-
/// [tracking issue]: https://github.com/rust-lang/rust/issues/104203
1509+
/// The return value is unspecified if `align` isn't a power of two.
16031510
#[must_use]
16041511
#[inline]
1605-
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
1606-
#[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "104203")]
1607-
pub const fn is_aligned_to(self, align: usize) -> bool {
1608-
if !align.is_power_of_two() {
1609-
panic!("is_aligned_to: align is not a power-of-two");
1610-
}
1611-
1512+
pub(crate) const fn is_aligned_to(self, align: usize) -> bool {
16121513
#[inline]
16131514
fn runtime_impl(ptr: *const (), align: usize) -> bool {
1515+
// classic bitmath magic trick:
1516+
//
1517+
// * a power of two is a single bit set like 00001000
1518+
// * multiples of that must have all those low 0's also clear
1519+
// * subtracting one from the power gets you 00000111
1520+
// * so if we AND that with a multiple, we should get 0
1521+
//
1522+
// Note we get to assume align is a power of two
16141523
ptr.addr() & (align - 1) == 0
16151524
}
16161525

‎library/core/src/ptr/mut_ptr.rs

Lines changed: 1 addition & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,140 +1764,7 @@ impl<T: ?Sized> *mut T {
17641764
where
17651765
T: Sized,
17661766
{
1767-
self.is_aligned_to(mem::align_of::<T>())
1768-
}
1769-
1770-
/// Returns whether the pointer is aligned to `align`.
1771-
///
1772-
/// For non-`Sized` pointees this operation considers only the data pointer,
1773-
/// ignoring the metadata.
1774-
///
1775-
/// # Panics
1776-
///
1777-
/// The function panics if `align` is not a power-of-two (this includes 0).
1778-
///
1779-
/// # Examples
1780-
///
1781-
/// ```
1782-
/// #![feature(pointer_is_aligned)]
1783-
///
1784-
/// // On some platforms, the alignment of i32 is less than 4.
1785-
/// #[repr(align(4))]
1786-
/// struct AlignedI32(i32);
1787-
///
1788-
/// let mut data = AlignedI32(42);
1789-
/// let ptr = &mut data as *mut AlignedI32;
1790-
///
1791-
/// assert!(ptr.is_aligned_to(1));
1792-
/// assert!(ptr.is_aligned_to(2));
1793-
/// assert!(ptr.is_aligned_to(4));
1794-
///
1795-
/// assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
1796-
/// assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
1797-
///
1798-
/// assert_ne!(ptr.is_aligned_to(8), ptr.wrapping_add(1).is_aligned_to(8));
1799-
/// ```
1800-
///
1801-
/// # At compiletime
1802-
/// **Note: Alignment at compiletime is experimental and subject to change. See the
1803-
/// [tracking issue] for details.**
1804-
///
1805-
/// At compiletime, the compiler may not know where a value will end up in memory.
1806-
/// Calling this function on a pointer created from a reference at compiletime will only
1807-
/// return `true` if the pointer is guaranteed to be aligned. This means that the pointer
1808-
/// cannot be stricter aligned than the reference's underlying allocation.
1809-
///
1810-
/// ```
1811-
/// #![feature(pointer_is_aligned)]
1812-
/// #![feature(const_pointer_is_aligned)]
1813-
/// #![feature(const_mut_refs)]
1814-
///
1815-
/// // On some platforms, the alignment of i32 is less than 4.
1816-
/// #[repr(align(4))]
1817-
/// struct AlignedI32(i32);
1818-
///
1819-
/// const _: () = {
1820-
/// let mut data = AlignedI32(42);
1821-
/// let ptr = &mut data as *mut AlignedI32;
1822-
///
1823-
/// assert!(ptr.is_aligned_to(1));
1824-
/// assert!(ptr.is_aligned_to(2));
1825-
/// assert!(ptr.is_aligned_to(4));
1826-
///
1827-
/// // At compiletime, we know for sure that the pointer isn't aligned to 8.
1828-
/// assert!(!ptr.is_aligned_to(8));
1829-
/// assert!(!ptr.wrapping_add(1).is_aligned_to(8));
1830-
/// };
1831-
/// ```
1832-
///
1833-
/// Due to this behavior, it is possible that a runtime pointer derived from a compiletime
1834-
/// pointer is aligned, even if the compiletime pointer wasn't aligned.
1835-
///
1836-
/// ```
1837-
/// #![feature(pointer_is_aligned)]
1838-
/// #![feature(const_pointer_is_aligned)]
1839-
///
1840-
/// // On some platforms, the alignment of i32 is less than 4.
1841-
/// #[repr(align(4))]
1842-
/// struct AlignedI32(i32);
1843-
///
1844-
/// // At compiletime, neither `COMPTIME_PTR` nor `COMPTIME_PTR + 1` is aligned.
1845-
/// // Also, note that mutable references are not allowed in the final value of constants.
1846-
/// const COMPTIME_PTR: *mut AlignedI32 = (&AlignedI32(42) as *const AlignedI32).cast_mut();
1847-
/// const _: () = assert!(!COMPTIME_PTR.is_aligned_to(8));
1848-
/// const _: () = assert!(!COMPTIME_PTR.wrapping_add(1).is_aligned_to(8));
1849-
///
1850-
/// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
1851-
/// let runtime_ptr = COMPTIME_PTR;
1852-
/// assert_ne!(
1853-
/// runtime_ptr.is_aligned_to(8),
1854-
/// runtime_ptr.wrapping_add(1).is_aligned_to(8),
1855-
/// );
1856-
/// ```
1857-
///
1858-
/// If a pointer is created from a fixed address, this function behaves the same during
1859-
/// runtime and compiletime.
1860-
///
1861-
/// ```
1862-
/// #![feature(pointer_is_aligned)]
1863-
/// #![feature(const_pointer_is_aligned)]
1864-
///
1865-
/// const _: () = {
1866-
/// let ptr = 40 as *mut u8;
1867-
/// assert!(ptr.is_aligned_to(1));
1868-
/// assert!(ptr.is_aligned_to(2));
1869-
/// assert!(ptr.is_aligned_to(4));
1870-
/// assert!(ptr.is_aligned_to(8));
1871-
/// assert!(!ptr.is_aligned_to(16));
1872-
/// };
1873-
/// ```
1874-
///
1875-
/// [tracking issue]: https://github.com/rust-lang/rust/issues/104203
1876-
#[must_use]
1877-
#[inline]
1878-
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
1879-
#[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "104203")]
1880-
pub const fn is_aligned_to(self, align: usize) -> bool {
1881-
if !align.is_power_of_two() {
1882-
panic!("is_aligned_to: align is not a power-of-two");
1883-
}
1884-
1885-
#[inline]
1886-
fn runtime_impl(ptr: *mut (), align: usize) -> bool {
1887-
ptr.addr() & (align - 1) == 0
1888-
}
1889-
1890-
#[inline]
1891-
const fn const_impl(ptr: *mut (), align: usize) -> bool {
1892-
// We can't use the address of `self` in a `const fn`, so we use `align_offset` instead.
1893-
// The cast to `()` is used to
1894-
// 1. deal with fat pointers; and
1895-
// 2. ensure that `align_offset` doesn't actually try to compute an offset.
1896-
ptr.align_offset(align) == 0
1897-
}
1898-
1899-
// SAFETY: The two versions are equivalent at runtime.
1900-
unsafe { const_eval_select((self.cast::<()>(), align), const_impl, runtime_impl) }
1767+
(self as *const T).is_aligned()
19011768
}
19021769
}
19031770

‎library/core/src/ptr/non_null.rs

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,118 +1405,6 @@ impl<T: ?Sized> NonNull<T> {
14051405
{
14061406
self.pointer.is_aligned()
14071407
}
1408-
1409-
/// Returns whether the pointer is aligned to `align`.
1410-
///
1411-
/// For non-`Sized` pointees this operation considers only the data pointer,
1412-
/// ignoring the metadata.
1413-
///
1414-
/// # Panics
1415-
///
1416-
/// The function panics if `align` is not a power-of-two (this includes 0).
1417-
///
1418-
/// # Examples
1419-
///
1420-
/// ```
1421-
/// #![feature(pointer_is_aligned)]
1422-
///
1423-
/// // On some platforms, the alignment of i32 is less than 4.
1424-
/// #[repr(align(4))]
1425-
/// struct AlignedI32(i32);
1426-
///
1427-
/// let data = AlignedI32(42);
1428-
/// let ptr = &data as *const AlignedI32;
1429-
///
1430-
/// assert!(ptr.is_aligned_to(1));
1431-
/// assert!(ptr.is_aligned_to(2));
1432-
/// assert!(ptr.is_aligned_to(4));
1433-
///
1434-
/// assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
1435-
/// assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
1436-
///
1437-
/// assert_ne!(ptr.is_aligned_to(8), ptr.wrapping_add(1).is_aligned_to(8));
1438-
/// ```
1439-
///
1440-
/// # At compiletime
1441-
/// **Note: Alignment at compiletime is experimental and subject to change. See the
1442-
/// [tracking issue] for details.**
1443-
///
1444-
/// At compiletime, the compiler may not know where a value will end up in memory.
1445-
/// Calling this function on a pointer created from a reference at compiletime will only
1446-
/// return `true` if the pointer is guaranteed to be aligned. This means that the pointer
1447-
/// cannot be stricter aligned than the reference's underlying allocation.
1448-
///
1449-
/// ```
1450-
/// #![feature(pointer_is_aligned)]
1451-
/// #![feature(const_pointer_is_aligned)]
1452-
///
1453-
/// // On some platforms, the alignment of i32 is less than 4.
1454-
/// #[repr(align(4))]
1455-
/// struct AlignedI32(i32);
1456-
///
1457-
/// const _: () = {
1458-
/// let data = AlignedI32(42);
1459-
/// let ptr = &data as *const AlignedI32;
1460-
///
1461-
/// assert!(ptr.is_aligned_to(1));
1462-
/// assert!(ptr.is_aligned_to(2));
1463-
/// assert!(ptr.is_aligned_to(4));
1464-
///
1465-
/// // At compiletime, we know for sure that the pointer isn't aligned to 8.
1466-
/// assert!(!ptr.is_aligned_to(8));
1467-
/// assert!(!ptr.wrapping_add(1).is_aligned_to(8));
1468-
/// };
1469-
/// ```
1470-
///
1471-
/// Due to this behavior, it is possible that a runtime pointer derived from a compiletime
1472-
/// pointer is aligned, even if the compiletime pointer wasn't aligned.
1473-
///
1474-
/// ```
1475-
/// #![feature(pointer_is_aligned)]
1476-
/// #![feature(const_pointer_is_aligned)]
1477-
///
1478-
/// // On some platforms, the alignment of i32 is less than 4.
1479-
/// #[repr(align(4))]
1480-
/// struct AlignedI32(i32);
1481-
///
1482-
/// // At compiletime, neither `COMPTIME_PTR` nor `COMPTIME_PTR + 1` is aligned.
1483-
/// const COMPTIME_PTR: *const AlignedI32 = &AlignedI32(42);
1484-
/// const _: () = assert!(!COMPTIME_PTR.is_aligned_to(8));
1485-
/// const _: () = assert!(!COMPTIME_PTR.wrapping_add(1).is_aligned_to(8));
1486-
///
1487-
/// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
1488-
/// let runtime_ptr = COMPTIME_PTR;
1489-
/// assert_ne!(
1490-
/// runtime_ptr.is_aligned_to(8),
1491-
/// runtime_ptr.wrapping_add(1).is_aligned_to(8),
1492-
/// );
1493-
/// ```
1494-
///
1495-
/// If a pointer is created from a fixed address, this function behaves the same during
1496-
/// runtime and compiletime.
1497-
///
1498-
/// ```
1499-
/// #![feature(pointer_is_aligned)]
1500-
/// #![feature(const_pointer_is_aligned)]
1501-
///
1502-
/// const _: () = {
1503-
/// let ptr = 40 as *const u8;
1504-
/// assert!(ptr.is_aligned_to(1));
1505-
/// assert!(ptr.is_aligned_to(2));
1506-
/// assert!(ptr.is_aligned_to(4));
1507-
/// assert!(ptr.is_aligned_to(8));
1508-
/// assert!(!ptr.is_aligned_to(16));
1509-
/// };
1510-
/// ```
1511-
///
1512-
/// [tracking issue]: https://github.com/rust-lang/rust/issues/104203
1513-
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
1514-
#[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "104203")]
1515-
#[must_use]
1516-
#[inline]
1517-
pub const fn is_aligned_to(self, align: usize) -> bool {
1518-
self.pointer.is_aligned_to(align)
1519-
}
15201408
}
15211409

15221410
impl<T> NonNull<[T]> {

‎library/core/src/slice/ascii.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ const fn is_ascii(s: &[u8]) -> bool {
386386
// have alignment information it should have given a `usize::MAX` for
387387
// `align_offset` earlier, sending things through the scalar path instead of
388388
// this one, so this check should pass if it's reachable.
389-
debug_assert!(word_ptr.is_aligned_to(mem::align_of::<usize>()));
389+
debug_assert!(word_ptr.is_aligned());
390390

391391
// Read subsequent words until the last aligned word, excluding the last
392392
// aligned word by itself to be done in tail check later, to ensure that

‎library/core/src/sync/atomic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ impl AtomicBool {
425425
/// // Get a pointer to an allocated value
426426
/// let ptr: *mut bool = Box::into_raw(Box::new(false));
427427
///
428-
/// assert!(ptr.is_aligned_to(align_of::<AtomicBool>()));
428+
/// assert!(ptr.cast::<AtomicBool>().is_aligned());
429429
///
430430
/// {
431431
/// // Create an atomic view of the allocated value
@@ -1223,7 +1223,7 @@ impl<T> AtomicPtr<T> {
12231223
/// // Get a pointer to an allocated value
12241224
/// let ptr: *mut *mut u8 = Box::into_raw(Box::new(std::ptr::null_mut()));
12251225
///
1226-
/// assert!(ptr.is_aligned_to(align_of::<AtomicPtr<u8>>()));
1226+
/// assert!(ptr.cast::<AtomicPtr<u8>>().is_aligned());
12271227
///
12281228
/// {
12291229
/// // Create an atomic view of the allocated value
@@ -2206,7 +2206,7 @@ macro_rules! atomic_int {
22062206
/// // Get a pointer to an allocated value
22072207
#[doc = concat!("let ptr: *mut ", stringify!($int_type), " = Box::into_raw(Box::new(0));")]
22082208
///
2209-
#[doc = concat!("assert!(ptr.is_aligned_to(align_of::<", stringify!($atomic_type), ">()));")]
2209+
#[doc = concat!("assert!(ptr.cast::<", stringify!($atomic_type), ">().is_aligned()));")]
22102210
///
22112211
/// {
22122212
/// // Create an atomic view of the allocated value

‎library/core/tests/ptr.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -714,15 +714,15 @@ fn is_aligned() {
714714
let data = 42;
715715
let ptr: *const i32 = &data;
716716
assert!(ptr.is_aligned());
717-
assert!(ptr.is_aligned_to(1));
718-
assert!(ptr.is_aligned_to(2));
719-
assert!(ptr.is_aligned_to(4));
720-
assert!(ptr.wrapping_byte_add(2).is_aligned_to(1));
721-
assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
722-
assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
717+
assert!(ptr.wrapping_add(1).is_aligned());
718+
assert!(ptr.wrapping_byte_add(4).is_aligned());
719+
720+
assert!(!ptr.wrapping_byte_add(1).is_aligned());
721+
assert!(!ptr.wrapping_byte_add(2).is_aligned());
722+
assert!(!ptr.wrapping_byte_add(3).is_aligned());
723723

724724
// At runtime either `ptr` or `ptr+1` is aligned to 8.
725-
assert_ne!(ptr.is_aligned_to(8), ptr.wrapping_add(1).is_aligned_to(8));
725+
assert_ne!(ptr.cast::<u64>().is_aligned(), ptr.wrapping_add(1).cast::<u64>().is_aligned());
726726
}
727727

728728
#[test]
@@ -731,16 +731,16 @@ fn is_aligned_const() {
731731
let data = 42;
732732
let ptr: *const i32 = &data;
733733
assert!(ptr.is_aligned());
734-
assert!(ptr.is_aligned_to(1));
735-
assert!(ptr.is_aligned_to(2));
736-
assert!(ptr.is_aligned_to(4));
737-
assert!(ptr.wrapping_byte_add(2).is_aligned_to(1));
738-
assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
739-
assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
734+
assert!(ptr.wrapping_add(1).is_aligned());
735+
assert!(ptr.wrapping_byte_add(4).is_aligned());
736+
737+
assert!(!ptr.wrapping_byte_add(1).is_aligned());
738+
assert!(!ptr.wrapping_byte_add(2).is_aligned());
739+
assert!(!ptr.wrapping_byte_add(3).is_aligned());
740740

741741
// At comptime neither `ptr` nor `ptr+1` is aligned to 8.
742-
assert!(!ptr.is_aligned_to(8));
743-
assert!(!ptr.wrapping_add(1).is_aligned_to(8));
742+
assert!(!ptr.cast::<u64>().is_aligned());
743+
assert!(!ptr.wrapping_add(1).cast::<u64>().is_aligned());
744744
}
745745
}
746746

‎library/std/src/sys/pal/sgx/abi/usercalls/alloc.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ pub unsafe trait UserSafe {
118118
/// * the pointer is null.
119119
/// * the pointed-to range is not in user memory.
120120
unsafe fn check_ptr(ptr: *const Self) {
121-
let is_aligned = |p: *const u8| -> bool { p.is_aligned_to(Self::align_of()) };
122-
123-
assert!(is_aligned(ptr as *const u8));
121+
assert!(ptr.is_aligned());
124122
assert!(is_user_range(ptr as _, mem::size_of_val(unsafe { &*ptr })));
125123
assert!(!ptr.is_null());
126124
}

‎src/tools/miri/tests/pass-dep/shims/posix_memalign.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn main() {
1313
let size = 64;
1414
assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
1515
assert!(!ptr.is_null());
16-
assert!(ptr.is_aligned_to(align));
16+
assert!(ptr.addr() % align == 0);
1717
ptr.cast::<u8>().write_bytes(1, size);
1818
libc::free(ptr);
1919
}
@@ -25,7 +25,7 @@ fn main() {
2525
let size = 8;
2626
assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
2727
assert!(!ptr.is_null());
28-
assert!(ptr.is_aligned_to(align));
28+
assert!(ptr.addr() % align == 0);
2929
ptr.cast::<u8>().write_bytes(1, size);
3030
libc::free(ptr);
3131
}
@@ -37,7 +37,7 @@ fn main() {
3737
let size = 31;
3838
assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
3939
assert!(!ptr.is_null());
40-
assert!(ptr.is_aligned_to(align));
40+
assert!(ptr.addr() % align == 0);
4141
ptr.cast::<u8>().write_bytes(1, size);
4242
libc::free(ptr);
4343
}
@@ -51,7 +51,7 @@ fn main() {
5151
// We are not required to return null if size == 0, but we currently do.
5252
// It's fine to remove this assert if we start returning non-null pointers.
5353
assert!(ptr.is_null());
54-
assert!(ptr.is_aligned_to(align));
54+
assert!(ptr.addr() % align == 0);
5555
// Regardless of what we return, it must be `free`able.
5656
libc::free(ptr);
5757
}

‎tests/ui/structs-enums/type-sizes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ pub fn main() {
306306

307307
let v = Reorder4 {a: 0, b: 0, ary: [0; 4]};
308308
assert_eq!(size_of::<Reorder4>(), 12);
309-
assert!((&v.ary).as_ptr().is_aligned_to(4), "[u8; 4] should group with align-4 fields");
309+
assert!_eq((&v.ary).as_ptr().addr() % 4, 0, "[u8; 4] should group with align-4 fields");
310310
let v = Reorder2 {a: 0, b: 0, ary: [0; 6]};
311311
assert_eq!(size_of::<Reorder2>(), 10);
312-
assert!((&v.ary).as_ptr().is_aligned_to(2), "[u8; 6] should group with align-2 fields");
312+
assert_eq!((&v.ary).as_ptr().addr() % 2, 0, "[u8; 6] should group with align-2 fields");
313313

314314
let v = VecDummy { r: RawVecDummy { ptr: NonNull::dangling(), cap: 0 }, len: 1 };
315315
assert_eq!(ptr::from_ref(&v), ptr::from_ref(&v.r.ptr).cast(),
@@ -324,7 +324,7 @@ pub fn main() {
324324
assert_eq!(size_of::<Cow<'static, str>>(), size_of::<String>());
325325

326326
let v = ReorderWithNiche {a: 0, b: ' ', c: 0, ary: [0; 8]};
327-
assert!((&v.ary).as_ptr().is_aligned_to(4),
327+
assert_eq!((&v.ary).as_ptr().addr() % 4, 0,
328328
"here [u8; 8] should group with _at least_ align-4 fields");
329329
assert_eq!(ptr::from_ref(&v), ptr::from_ref(&v.b).cast(),
330330
"sort niches to the front where possible");

0 commit comments

Comments
 (0)
Please sign in to comment.