From 4696e8906d362c1e10ffe40a5935de0ada45fb48 Mon Sep 17 00:00:00 2001 From: Lukas Markeffsky <@> Date: Sun, 23 Oct 2022 00:47:21 +0200 Subject: [PATCH] =?UTF-8?q?Schr=C3=B6dinger's=20pointer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's aligned *and* not aligned! --- library/core/src/ptr/const_ptr.rs | 42 +++++++++++++++++++++++++++++++ library/core/src/ptr/mut_ptr.rs | 42 +++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) 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 *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::().is_aligned()); + /// const _: () = assert!(!CONST_PTR.wrapping_add(1).cast::().is_aligned()); + /// + /// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned. + /// let runtime_ptr = CONST_PTR; + /// assert_ne!( + /// runtime_ptr.cast::().is_aligned(), + /// runtime_ptr.wrapping_add(1).cast::().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 *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 *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::().is_aligned()); + /// const _: () = assert!(!CONST_PTR.wrapping_add(1).cast::().is_aligned()); + /// + /// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned. + /// let runtime_ptr = CONST_PTR; + /// assert_ne!( + /// runtime_ptr.cast::().is_aligned(), + /// runtime_ptr.wrapping_add(1).cast::().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 *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. ///