Skip to content

Commit 4696e89

Browse files
author
Lukas Markeffsky
committed
Schrödinger's pointer
It's aligned *and* not aligned!
1 parent df0bcfe commit 4696e89

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

library/core/src/ptr/const_ptr.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,27 @@ impl<T: ?Sized> *const T {
14021402
/// };
14031403
/// ```
14041404
///
1405+
/// Due to this behavior, it is possible that a runtime pointer derived from a compiletime
1406+
/// pointer is aligned, even if the compiletime pointer wasn't aligned.
1407+
///
1408+
#[cfg_attr(bootstrap, doc = "```ignore")]
1409+
#[cfg_attr(not(bootstrap), doc = "```")]
1410+
/// #![feature(pointer_is_aligned)]
1411+
/// #![feature(const_pointer_is_aligned)]
1412+
///
1413+
/// // At compiletime, neither `CONST_PTR` nor `CONST_PTR + 1` is aligned.
1414+
/// const CONST_PTR: *const i32 = &42;
1415+
/// const _: () = assert!(!CONST_PTR.cast::<i64>().is_aligned());
1416+
/// const _: () = assert!(!CONST_PTR.wrapping_add(1).cast::<i64>().is_aligned());
1417+
///
1418+
/// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
1419+
/// let runtime_ptr = CONST_PTR;
1420+
/// assert_ne!(
1421+
/// runtime_ptr.cast::<i64>().is_aligned(),
1422+
/// runtime_ptr.wrapping_add(1).cast::<i64>().is_aligned(),
1423+
/// );
1424+
/// ```
1425+
///
14051426
/// If a pointer is created from a fixed address, this function behaves the same during
14061427
/// runtime and compiletime.
14071428
///
@@ -1492,6 +1513,27 @@ impl<T: ?Sized> *const T {
14921513
/// };
14931514
/// ```
14941515
///
1516+
/// Due to this behavior, it is possible that a runtime pointer derived from a compiletime
1517+
/// pointer is aligned, even if the compiletime pointer wasn't aligned.
1518+
///
1519+
#[cfg_attr(bootstrap, doc = "```ignore")]
1520+
#[cfg_attr(not(bootstrap), doc = "```")]
1521+
/// #![feature(pointer_is_aligned)]
1522+
/// #![feature(const_pointer_is_aligned)]
1523+
///
1524+
/// // At compiletime, neither `CONST_PTR` nor `CONST_PTR + 1` is aligned.
1525+
/// const CONST_PTR: *const i32 = &42;
1526+
/// const _: () = assert!(!CONST_PTR.is_aligned_to(8));
1527+
/// const _: () = assert!(!CONST_PTR.wrapping_add(1).is_aligned_to(8));
1528+
///
1529+
/// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
1530+
/// let runtime_ptr = CONST_PTR;
1531+
/// assert_ne!(
1532+
/// runtime_ptr.is_aligned_to(8),
1533+
/// runtime_ptr.wrapping_add(1).is_aligned_to(8),
1534+
/// );
1535+
/// ```
1536+
///
14951537
/// If a pointer is created from a fixed address, this function behaves the same during
14961538
/// runtime and compiletime.
14971539
///

library/core/src/ptr/mut_ptr.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,27 @@ impl<T: ?Sized> *mut T {
16701670
/// };
16711671
/// ```
16721672
///
1673+
/// Due to this behavior, it is possible that a runtime pointer derived from a compiletime
1674+
/// pointer is aligned, even if the compiletime pointer wasn't aligned.
1675+
///
1676+
#[cfg_attr(bootstrap, doc = "```ignore")]
1677+
#[cfg_attr(not(bootstrap), doc = "```")]
1678+
/// #![feature(pointer_is_aligned)]
1679+
/// #![feature(const_pointer_is_aligned)]
1680+
///
1681+
/// // At compiletime, neither `CONST_PTR` nor `CONST_PTR + 1` is aligned.
1682+
/// const CONST_PTR: *const i32 = &42;
1683+
/// const _: () = assert!(!CONST_PTR.cast::<i64>().is_aligned());
1684+
/// const _: () = assert!(!CONST_PTR.wrapping_add(1).cast::<i64>().is_aligned());
1685+
///
1686+
/// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
1687+
/// let runtime_ptr = CONST_PTR;
1688+
/// assert_ne!(
1689+
/// runtime_ptr.cast::<i64>().is_aligned(),
1690+
/// runtime_ptr.wrapping_add(1).cast::<i64>().is_aligned(),
1691+
/// );
1692+
/// ```
1693+
///
16731694
/// If a pointer is created from a fixed address, this function behaves the same during
16741695
/// runtime and compiletime.
16751696
///
@@ -1760,6 +1781,27 @@ impl<T: ?Sized> *mut T {
17601781
/// };
17611782
/// ```
17621783
///
1784+
/// Due to this behavior, it is possible that a runtime pointer derived from a compiletime
1785+
/// pointer is aligned, even if the compiletime pointer wasn't aligned.
1786+
///
1787+
#[cfg_attr(bootstrap, doc = "```ignore")]
1788+
#[cfg_attr(not(bootstrap), doc = "```")]
1789+
/// #![feature(pointer_is_aligned)]
1790+
/// #![feature(const_pointer_is_aligned)]
1791+
///
1792+
/// // At compiletime, neither `CONST_PTR` nor `CONST_PTR + 1` is aligned.
1793+
/// const CONST_PTR: *const i32 = &42;
1794+
/// const _: () = assert!(!CONST_PTR.is_aligned_to(8));
1795+
/// const _: () = assert!(!CONST_PTR.wrapping_add(1).is_aligned_to(8));
1796+
///
1797+
/// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
1798+
/// let runtime_ptr = CONST_PTR;
1799+
/// assert_ne!(
1800+
/// runtime_ptr.is_aligned_to(8),
1801+
/// runtime_ptr.wrapping_add(1).is_aligned_to(8),
1802+
/// );
1803+
/// ```
1804+
///
17631805
/// If a pointer is created from a fixed address, this function behaves the same during
17641806
/// runtime and compiletime.
17651807
///

0 commit comments

Comments
 (0)