diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index dd11568d70584..2680c8b8cb33c 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -982,6 +982,7 @@ extern "rust-intrinsic" { /// Turning a pointer into a `usize`: /// /// ``` + /// #![feature(strict_provenance)] /// let ptr = &0; /// let ptr_num_transmute = unsafe { /// std::mem::transmute::<&i32, usize>(ptr) diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 1a85e2ef7b607..7f1ae374965dd 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -146,6 +146,7 @@ #![feature(ptr_metadata)] #![feature(slice_ptr_get)] #![feature(str_internals)] +#![feature(strict_provenance)] #![feature(utf16_extra)] #![feature(utf16_extra_const)] #![feature(variant_count)] diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 67c46737fc5a3..09d10573af932 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -64,8 +64,10 @@ impl *const T { /// and cannot be created from one without additional context. /// /// If you would like to treat a pointer like an integer anyway, - /// see [`addr`][#method.addr-1] and [`with_addr`][#method.with_addr-1] for the responsible - /// way to do that. + /// see [`addr`] and [`with_addr`] for the responsible way to do that. + /// + /// [`addr`]: pointer::addr + /// [`with_addr`]: pointer::with_addr #[unstable(feature = "ptr_to_from_bits", issue = "91126")] pub fn to_bits(self) -> [u8; core::mem::size_of::<*const ()>()] where @@ -107,8 +109,10 @@ impl *const T { /// and is equivalent to the deprecated `ptr as usize` cast. /// /// On more complicated platforms like CHERI and segmented architectures, - /// this may remove some important metadata. See [`with_addr`][#method.with_addr-1] for + /// this may remove some important metadata. See [`with_addr`] for /// details on this distinction and why it's important. + /// + /// [`with_addr`]: pointer::with_addr #[unstable(feature = "strict_provenance", issue = "99999999")] pub fn addr(self) -> usize where @@ -151,6 +155,7 @@ impl *const T { /// with tagged pointers. Here we have a tag in the lowest bit: /// /// ```text + /// #![feature(strict_provenance)] /// let my_tagged_ptr: *const T = ...; /// /// // Get the address and do whatever bit tricks we like @@ -342,7 +347,7 @@ impl *const T { /// enables more aggressive compiler optimizations. /// /// [`wrapping_offset`]: #method.wrapping_offset - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Examples /// @@ -429,7 +434,7 @@ impl *const T { /// platform-specific and not at all portable. /// /// [`offset`]: #method.offset - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Examples /// @@ -505,7 +510,7 @@ impl *const T { /// such large allocations either.) /// /// [`add`]: #method.add - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Panics /// @@ -530,6 +535,7 @@ impl *const T { /// *Incorrect* usage: /// /// ```rust,no_run + /// # #![feature(strict_provenance)] /// let ptr1 = Box::into_raw(Box::new(0u8)) as *const u8; /// let ptr2 = Box::into_raw(Box::new(1u8)) as *const u8; /// let diff = (ptr2.addr() as isize).wrapping_sub(ptr1.addr() as isize); @@ -654,7 +660,7 @@ impl *const T { /// enables more aggressive compiler optimizations. /// /// [`wrapping_add`]: #method.wrapping_add - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Examples /// @@ -718,7 +724,7 @@ impl *const T { /// enables more aggressive compiler optimizations. /// /// [`wrapping_sub`]: #method.wrapping_sub - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Examples /// @@ -775,7 +781,7 @@ impl *const T { /// allocated object and then re-entering it later is permitted. /// /// [`add`]: #method.add - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Examples /// @@ -837,7 +843,7 @@ impl *const T { /// allocated object and then re-entering it later is permitted. /// /// [`sub`]: #method.sub - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Examples /// @@ -1183,7 +1189,7 @@ impl *const [T] { /// See also [`slice::from_raw_parts`][]. /// /// [valid]: crate::ptr#safety - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance #[inline] #[unstable(feature = "ptr_as_uninit", issue = "75402")] #[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")] diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index c26014690c4e6..885fc8b3e432a 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -51,7 +51,7 @@ //! has size 0, i.e., even if memory is not actually touched. Consider using //! [`NonNull::dangling`] in such cases. //! -//! ## Allocated Object and Provenance +//! ## Allocated Objects and Provenance //! //! For several operations, such as [`offset`] or field projections (`expr.field`), the notion of an //! "allocated object" becomes relevant. An allocated object is a contiguous region of memory. @@ -297,14 +297,15 @@ pub const fn null_mut() -> *mut T { /// # Example /// /// ``` -/// use core::{ptr, mem}; +/// #![feature(strict_provenance)] +/// use core::ptr; /// /// // I store my ZSTs at the *coolest* address /// let my_good_ptr = ptr::zst_exists::<()>(0xc001_add7); /// /// // "store" and then "load" a ZST at this cool address. -/// my_good_ptr.write(()); -/// let output = my_good_ptr.read(); +/// unsafe { my_good_ptr.write(()); } +/// let output = unsafe { my_good_ptr.read() }; /// ``` #[inline(always)] #[must_use] diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index bbdc0070f8c97..54e892fd57033 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -67,8 +67,10 @@ impl *mut T { /// and cannot be created from one without additional context. /// /// If you would like to treat a pointer like an integer anyway, - /// see [`addr`][#method.addr-1] and [`with_addr`][#method.with_addr-1] for - /// the responsible way to do that. + /// see [`addr`] and [`with_addr`] for the responsible way to do that. + /// + /// [`addr`]: pointer::addr + /// [`with_addr`]: pointer::with_addr #[unstable(feature = "ptr_to_from_bits", issue = "91126")] pub fn to_bits(self) -> [u8; core::mem::size_of::<*mut ()>()] where @@ -110,8 +112,10 @@ impl *mut T { /// and is equivalent to the deprecated `ptr as usize` cast. /// /// On more complicated platforms like CHERI and segmented architectures, - /// this may remove some important metadata. See [`with_addr`][#method.with_addr-1] for + /// this may remove some important metadata. See [`with_addr`] for /// details on this distinction and why it's important. + /// + /// [`with_addr`]: pointer::with_addr #[unstable(feature = "strict_provenance", issue = "99999999")] pub fn addr(self) -> usize where @@ -154,6 +158,7 @@ impl *mut T { /// with tagged pointers. Here we have a tag in the lowest bit: /// /// ```text + /// #![feature(strict_provenance)] /// let my_tagged_ptr: *mut T = ...; /// /// // Get the address and do whatever bit tricks we like @@ -352,7 +357,7 @@ impl *mut T { /// enables more aggressive compiler optimizations. /// /// [`wrapping_offset`]: #method.wrapping_offset - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Examples /// @@ -440,7 +445,7 @@ impl *mut T { /// platform-specific and not at all portable. /// /// [`offset`]: #method.offset - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Examples /// @@ -683,7 +688,7 @@ impl *mut T { /// such large allocations either.) /// /// [`add`]: #method.add - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Panics /// @@ -708,6 +713,7 @@ impl *mut T { /// *Incorrect* usage: /// /// ```rust,no_run + /// #![feature(strict_provenance)] /// let ptr1 = Box::into_raw(Box::new(0u8)); /// let ptr2 = Box::into_raw(Box::new(1u8)); /// let diff = (ptr2.addr() as isize).wrapping_sub(ptr1.addr() as isize); @@ -768,7 +774,7 @@ impl *mut T { /// enables more aggressive compiler optimizations. /// /// [`wrapping_add`]: #method.wrapping_add - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Examples /// @@ -832,7 +838,7 @@ impl *mut T { /// enables more aggressive compiler optimizations. /// /// [`wrapping_sub`]: #method.wrapping_sub - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Examples /// @@ -889,7 +895,7 @@ impl *mut T { /// allocated object and then re-entering it later is permitted. /// /// [`add`]: #method.add - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Examples /// @@ -951,7 +957,7 @@ impl *mut T { /// allocated object and then re-entering it later is permitted. /// /// [`sub`]: #method.sub - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance /// /// # Examples /// @@ -1456,7 +1462,7 @@ impl *mut [T] { /// See also [`slice::from_raw_parts`][]. /// /// [valid]: crate::ptr#safety - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance #[inline] #[unstable(feature = "ptr_as_uninit", issue = "75402")] #[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")] @@ -1508,7 +1514,7 @@ impl *mut [T] { /// See also [`slice::from_raw_parts_mut`][]. /// /// [valid]: crate::ptr#safety - /// [allocated object]: crate::ptr#allocated-object + /// [allocated object]: crate::ptr#allocated-objects-and-provenance #[inline] #[unstable(feature = "ptr_as_uninit", issue = "75402")] #[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")] diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index 130c869b97891..c1b19895f006c 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -489,7 +489,7 @@ impl NonNull<[T]> { /// use std::ptr::NonNull; /// /// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3); - /// assert_eq!(slice.as_mut_ptr(), NonNull::::dangling()); + /// assert_eq!(slice.as_mut_ptr(), NonNull::::dangling().as_ptr()); /// ``` #[inline] #[must_use]