about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Markeffsky <@>2022-10-23 00:47:21 +0200
committerLukas Markeffsky <@>2022-11-19 16:47:42 +0100
commit4696e8906d362c1e10ffe40a5935de0ada45fb48 (patch)
tree828144a6d1e1efcd90155f1c5eb082eca6e1b3e2
parentdf0bcfe6448793e423bd6b8fd5294f10798dd469 (diff)
downloadrust-4696e8906d362c1e10ffe40a5935de0ada45fb48.tar.gz
rust-4696e8906d362c1e10ffe40a5935de0ada45fb48.zip
Schrödinger's pointer
It's aligned *and* not aligned!
-rw-r--r--library/core/src/ptr/const_ptr.rs42
-rw-r--r--library/core/src/ptr/mut_ptr.rs42
2 files changed, 84 insertions, 0 deletions
diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs
index 37679c504b3..d042f22f026 100644
--- a/library/core/src/ptr/const_ptr.rs
+++ b/library/core/src/ptr/const_ptr.rs
@@ -1402,6 +1402,27 @@ impl<T: ?Sized> *const T {
     /// };
     /// ```
     ///
+    /// Due to this behavior, it is possible that a runtime pointer derived from a compiletime
+    /// pointer is aligned, even if the compiletime pointer wasn't aligned.
+    ///
+    #[cfg_attr(bootstrap, doc = "```ignore")]
+    #[cfg_attr(not(bootstrap), doc = "```")]
+    /// #![feature(pointer_is_aligned)]
+    /// #![feature(const_pointer_is_aligned)]
+    ///
+    /// // At compiletime, neither `CONST_PTR` nor `CONST_PTR + 1` is aligned.
+    /// const CONST_PTR: *const i32 = &42;
+    /// const _: () = assert!(!CONST_PTR.cast::<i64>().is_aligned());
+    /// const _: () = assert!(!CONST_PTR.wrapping_add(1).cast::<i64>().is_aligned());
+    ///
+    /// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
+    /// let runtime_ptr = CONST_PTR;
+    /// assert_ne!(
+    ///     runtime_ptr.cast::<i64>().is_aligned(),
+    ///     runtime_ptr.wrapping_add(1).cast::<i64>().is_aligned(),
+    /// );
+    /// ```
+    ///
     /// If a pointer is created from a fixed address, this function behaves the same during
     /// runtime and compiletime.
     ///
@@ -1492,6 +1513,27 @@ impl<T: ?Sized> *const T {
     /// };
     /// ```
     ///
+    /// Due to this behavior, it is possible that a runtime pointer derived from a compiletime
+    /// pointer is aligned, even if the compiletime pointer wasn't aligned.
+    ///
+    #[cfg_attr(bootstrap, doc = "```ignore")]
+    #[cfg_attr(not(bootstrap), doc = "```")]
+    /// #![feature(pointer_is_aligned)]
+    /// #![feature(const_pointer_is_aligned)]
+    ///
+    /// // At compiletime, neither `CONST_PTR` nor `CONST_PTR + 1` is aligned.
+    /// const CONST_PTR: *const i32 = &42;
+    /// const _: () = assert!(!CONST_PTR.is_aligned_to(8));
+    /// const _: () = assert!(!CONST_PTR.wrapping_add(1).is_aligned_to(8));
+    ///
+    /// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
+    /// let runtime_ptr = CONST_PTR;
+    /// assert_ne!(
+    ///     runtime_ptr.is_aligned_to(8),
+    ///     runtime_ptr.wrapping_add(1).is_aligned_to(8),
+    /// );
+    /// ```
+    ///
     /// If a pointer is created from a fixed address, this function behaves the same during
     /// runtime and compiletime.
     ///
diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs
index 91747288689..764fa9d8ba8 100644
--- a/library/core/src/ptr/mut_ptr.rs
+++ b/library/core/src/ptr/mut_ptr.rs
@@ -1670,6 +1670,27 @@ impl<T: ?Sized> *mut T {
     /// };
     /// ```
     ///
+    /// Due to this behavior, it is possible that a runtime pointer derived from a compiletime
+    /// pointer is aligned, even if the compiletime pointer wasn't aligned.
+    ///
+    #[cfg_attr(bootstrap, doc = "```ignore")]
+    #[cfg_attr(not(bootstrap), doc = "```")]
+    /// #![feature(pointer_is_aligned)]
+    /// #![feature(const_pointer_is_aligned)]
+    ///
+    /// // At compiletime, neither `CONST_PTR` nor `CONST_PTR + 1` is aligned.
+    /// const CONST_PTR: *const i32 = &42;
+    /// const _: () = assert!(!CONST_PTR.cast::<i64>().is_aligned());
+    /// const _: () = assert!(!CONST_PTR.wrapping_add(1).cast::<i64>().is_aligned());
+    ///
+    /// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
+    /// let runtime_ptr = CONST_PTR;
+    /// assert_ne!(
+    ///     runtime_ptr.cast::<i64>().is_aligned(),
+    ///     runtime_ptr.wrapping_add(1).cast::<i64>().is_aligned(),
+    /// );
+    /// ```
+    ///
     /// If a pointer is created from a fixed address, this function behaves the same during
     /// runtime and compiletime.
     ///
@@ -1760,6 +1781,27 @@ impl<T: ?Sized> *mut T {
     /// };
     /// ```
     ///
+    /// Due to this behavior, it is possible that a runtime pointer derived from a compiletime
+    /// pointer is aligned, even if the compiletime pointer wasn't aligned.
+    ///
+    #[cfg_attr(bootstrap, doc = "```ignore")]
+    #[cfg_attr(not(bootstrap), doc = "```")]
+    /// #![feature(pointer_is_aligned)]
+    /// #![feature(const_pointer_is_aligned)]
+    ///
+    /// // At compiletime, neither `CONST_PTR` nor `CONST_PTR + 1` is aligned.
+    /// const CONST_PTR: *const i32 = &42;
+    /// const _: () = assert!(!CONST_PTR.is_aligned_to(8));
+    /// const _: () = assert!(!CONST_PTR.wrapping_add(1).is_aligned_to(8));
+    ///
+    /// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
+    /// let runtime_ptr = CONST_PTR;
+    /// assert_ne!(
+    ///     runtime_ptr.is_aligned_to(8),
+    ///     runtime_ptr.wrapping_add(1).is_aligned_to(8),
+    /// );
+    /// ```
+    ///
     /// If a pointer is created from a fixed address, this function behaves the same during
     /// runtime and compiletime.
     ///