Skip to content

Commit 8cc6326

Browse files
feature(strict_provenance) in doc examples
and unsafe and ptr methods and cleaning up unused uses and intra-doc links...
1 parent 81b7942 commit 8cc6326

File tree

6 files changed

+43
-28
lines changed

6 files changed

+43
-28
lines changed

library/core/src/intrinsics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@ extern "rust-intrinsic" {
982982
/// Turning a pointer into a `usize`:
983983
///
984984
/// ```
985+
/// #![feature(strict_provenance)]
985986
/// let ptr = &0;
986987
/// let ptr_num_transmute = unsafe {
987988
/// std::mem::transmute::<&i32, usize>(ptr)

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
#![feature(ptr_metadata)]
147147
#![feature(slice_ptr_get)]
148148
#![feature(str_internals)]
149+
#![feature(strict_provenance)]
149150
#![feature(utf16_extra)]
150151
#![feature(utf16_extra_const)]
151152
#![feature(variant_count)]

library/core/src/ptr/const_ptr.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ impl<T: ?Sized> *const T {
6464
/// and cannot be created from one without additional context.
6565
///
6666
/// If you would like to treat a pointer like an integer anyway,
67-
/// see [`addr`][#method.addr-1] and [`with_addr`][#method.with_addr-1] for the responsible
68-
/// way to do that.
67+
/// see [`addr`] and [`with_addr`] for the responsible way to do that.
68+
///
69+
/// [`addr`]: pointer::addr
70+
/// [`with_addr`]: pointer::with_addr
6971
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
7072
pub fn to_bits(self) -> [u8; core::mem::size_of::<*const ()>()]
7173
where
@@ -107,8 +109,10 @@ impl<T: ?Sized> *const T {
107109
/// and is equivalent to the deprecated `ptr as usize` cast.
108110
///
109111
/// On more complicated platforms like CHERI and segmented architectures,
110-
/// this may remove some important metadata. See [`with_addr`][#method.with_addr-1] for
112+
/// this may remove some important metadata. See [`with_addr`] for
111113
/// details on this distinction and why it's important.
114+
///
115+
/// [`with_addr`]: pointer::with_addr
112116
#[unstable(feature = "strict_provenance", issue = "99999999")]
113117
pub fn addr(self) -> usize
114118
where
@@ -151,6 +155,7 @@ impl<T: ?Sized> *const T {
151155
/// with tagged pointers. Here we have a tag in the lowest bit:
152156
///
153157
/// ```text
158+
/// #![feature(strict_provenance)]
154159
/// let my_tagged_ptr: *const T = ...;
155160
///
156161
/// // Get the address and do whatever bit tricks we like
@@ -342,7 +347,7 @@ impl<T: ?Sized> *const T {
342347
/// enables more aggressive compiler optimizations.
343348
///
344349
/// [`wrapping_offset`]: #method.wrapping_offset
345-
/// [allocated object]: crate::ptr#allocated-object
350+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
346351
///
347352
/// # Examples
348353
///
@@ -429,7 +434,7 @@ impl<T: ?Sized> *const T {
429434
/// platform-specific and not at all portable.
430435
///
431436
/// [`offset`]: #method.offset
432-
/// [allocated object]: crate::ptr#allocated-object
437+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
433438
///
434439
/// # Examples
435440
///
@@ -505,7 +510,7 @@ impl<T: ?Sized> *const T {
505510
/// such large allocations either.)
506511
///
507512
/// [`add`]: #method.add
508-
/// [allocated object]: crate::ptr#allocated-object
513+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
509514
///
510515
/// # Panics
511516
///
@@ -530,6 +535,7 @@ impl<T: ?Sized> *const T {
530535
/// *Incorrect* usage:
531536
///
532537
/// ```rust,no_run
538+
/// # #![feature(strict_provenance)]
533539
/// let ptr1 = Box::into_raw(Box::new(0u8)) as *const u8;
534540
/// let ptr2 = Box::into_raw(Box::new(1u8)) as *const u8;
535541
/// let diff = (ptr2.addr() as isize).wrapping_sub(ptr1.addr() as isize);
@@ -654,7 +660,7 @@ impl<T: ?Sized> *const T {
654660
/// enables more aggressive compiler optimizations.
655661
///
656662
/// [`wrapping_add`]: #method.wrapping_add
657-
/// [allocated object]: crate::ptr#allocated-object
663+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
658664
///
659665
/// # Examples
660666
///
@@ -718,7 +724,7 @@ impl<T: ?Sized> *const T {
718724
/// enables more aggressive compiler optimizations.
719725
///
720726
/// [`wrapping_sub`]: #method.wrapping_sub
721-
/// [allocated object]: crate::ptr#allocated-object
727+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
722728
///
723729
/// # Examples
724730
///
@@ -775,7 +781,7 @@ impl<T: ?Sized> *const T {
775781
/// allocated object and then re-entering it later is permitted.
776782
///
777783
/// [`add`]: #method.add
778-
/// [allocated object]: crate::ptr#allocated-object
784+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
779785
///
780786
/// # Examples
781787
///
@@ -837,7 +843,7 @@ impl<T: ?Sized> *const T {
837843
/// allocated object and then re-entering it later is permitted.
838844
///
839845
/// [`sub`]: #method.sub
840-
/// [allocated object]: crate::ptr#allocated-object
846+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
841847
///
842848
/// # Examples
843849
///
@@ -1183,7 +1189,7 @@ impl<T> *const [T] {
11831189
/// See also [`slice::from_raw_parts`][].
11841190
///
11851191
/// [valid]: crate::ptr#safety
1186-
/// [allocated object]: crate::ptr#allocated-object
1192+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
11871193
#[inline]
11881194
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
11891195
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]

library/core/src/ptr/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
//! has size 0, i.e., even if memory is not actually touched. Consider using
5252
//! [`NonNull::dangling`] in such cases.
5353
//!
54-
//! ## Allocated Object and Provenance
54+
//! ## Allocated Objects and Provenance
5555
//!
5656
//! For several operations, such as [`offset`] or field projections (`expr.field`), the notion of an
5757
//! "allocated object" becomes relevant. An allocated object is a contiguous region of memory.
@@ -297,14 +297,15 @@ pub const fn null_mut<T>() -> *mut T {
297297
/// # Example
298298
///
299299
/// ```
300-
/// use core::{ptr, mem};
300+
/// #![feature(strict_provenance)]
301+
/// use core::ptr;
301302
///
302303
/// // I store my ZSTs at the *coolest* address
303304
/// let my_good_ptr = ptr::zst_exists::<()>(0xc001_add7);
304305
///
305306
/// // "store" and then "load" a ZST at this cool address.
306-
/// my_good_ptr.write(());
307-
/// let output = my_good_ptr.read();
307+
/// unsafe { my_good_ptr.write(()); }
308+
/// let output = unsafe { my_good_ptr.read() };
308309
/// ```
309310
#[inline(always)]
310311
#[must_use]

library/core/src/ptr/mut_ptr.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ impl<T: ?Sized> *mut T {
6767
/// and cannot be created from one without additional context.
6868
///
6969
/// If you would like to treat a pointer like an integer anyway,
70-
/// see [`addr`][#method.addr-1] and [`with_addr`][#method.with_addr-1] for
71-
/// the responsible way to do that.
70+
/// see [`addr`] and [`with_addr`] for the responsible way to do that.
71+
///
72+
/// [`addr`]: pointer::addr
73+
/// [`with_addr`]: pointer::with_addr
7274
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
7375
pub fn to_bits(self) -> [u8; core::mem::size_of::<*mut ()>()]
7476
where
@@ -110,8 +112,10 @@ impl<T: ?Sized> *mut T {
110112
/// and is equivalent to the deprecated `ptr as usize` cast.
111113
///
112114
/// On more complicated platforms like CHERI and segmented architectures,
113-
/// this may remove some important metadata. See [`with_addr`][#method.with_addr-1] for
115+
/// this may remove some important metadata. See [`with_addr`] for
114116
/// details on this distinction and why it's important.
117+
///
118+
/// [`with_addr`]: pointer::with_addr
115119
#[unstable(feature = "strict_provenance", issue = "99999999")]
116120
pub fn addr(self) -> usize
117121
where
@@ -154,6 +158,7 @@ impl<T: ?Sized> *mut T {
154158
/// with tagged pointers. Here we have a tag in the lowest bit:
155159
///
156160
/// ```text
161+
/// #![feature(strict_provenance)]
157162
/// let my_tagged_ptr: *mut T = ...;
158163
///
159164
/// // Get the address and do whatever bit tricks we like
@@ -352,7 +357,7 @@ impl<T: ?Sized> *mut T {
352357
/// enables more aggressive compiler optimizations.
353358
///
354359
/// [`wrapping_offset`]: #method.wrapping_offset
355-
/// [allocated object]: crate::ptr#allocated-object
360+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
356361
///
357362
/// # Examples
358363
///
@@ -440,7 +445,7 @@ impl<T: ?Sized> *mut T {
440445
/// platform-specific and not at all portable.
441446
///
442447
/// [`offset`]: #method.offset
443-
/// [allocated object]: crate::ptr#allocated-object
448+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
444449
///
445450
/// # Examples
446451
///
@@ -683,7 +688,7 @@ impl<T: ?Sized> *mut T {
683688
/// such large allocations either.)
684689
///
685690
/// [`add`]: #method.add
686-
/// [allocated object]: crate::ptr#allocated-object
691+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
687692
///
688693
/// # Panics
689694
///
@@ -708,6 +713,7 @@ impl<T: ?Sized> *mut T {
708713
/// *Incorrect* usage:
709714
///
710715
/// ```rust,no_run
716+
/// #![feature(strict_provenance)]
711717
/// let ptr1 = Box::into_raw(Box::new(0u8));
712718
/// let ptr2 = Box::into_raw(Box::new(1u8));
713719
/// let diff = (ptr2.addr() as isize).wrapping_sub(ptr1.addr() as isize);
@@ -768,7 +774,7 @@ impl<T: ?Sized> *mut T {
768774
/// enables more aggressive compiler optimizations.
769775
///
770776
/// [`wrapping_add`]: #method.wrapping_add
771-
/// [allocated object]: crate::ptr#allocated-object
777+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
772778
///
773779
/// # Examples
774780
///
@@ -832,7 +838,7 @@ impl<T: ?Sized> *mut T {
832838
/// enables more aggressive compiler optimizations.
833839
///
834840
/// [`wrapping_sub`]: #method.wrapping_sub
835-
/// [allocated object]: crate::ptr#allocated-object
841+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
836842
///
837843
/// # Examples
838844
///
@@ -889,7 +895,7 @@ impl<T: ?Sized> *mut T {
889895
/// allocated object and then re-entering it later is permitted.
890896
///
891897
/// [`add`]: #method.add
892-
/// [allocated object]: crate::ptr#allocated-object
898+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
893899
///
894900
/// # Examples
895901
///
@@ -951,7 +957,7 @@ impl<T: ?Sized> *mut T {
951957
/// allocated object and then re-entering it later is permitted.
952958
///
953959
/// [`sub`]: #method.sub
954-
/// [allocated object]: crate::ptr#allocated-object
960+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
955961
///
956962
/// # Examples
957963
///
@@ -1456,7 +1462,7 @@ impl<T> *mut [T] {
14561462
/// See also [`slice::from_raw_parts`][].
14571463
///
14581464
/// [valid]: crate::ptr#safety
1459-
/// [allocated object]: crate::ptr#allocated-object
1465+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
14601466
#[inline]
14611467
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
14621468
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
@@ -1508,7 +1514,7 @@ impl<T> *mut [T] {
15081514
/// See also [`slice::from_raw_parts_mut`][].
15091515
///
15101516
/// [valid]: crate::ptr#safety
1511-
/// [allocated object]: crate::ptr#allocated-object
1517+
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
15121518
#[inline]
15131519
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
15141520
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]

library/core/src/ptr/non_null.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ impl<T> NonNull<[T]> {
489489
/// use std::ptr::NonNull;
490490
///
491491
/// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
492-
/// assert_eq!(slice.as_mut_ptr(), NonNull::<i8>::dangling());
492+
/// assert_eq!(slice.as_mut_ptr(), NonNull::<i8>::dangling().as_ptr());
493493
/// ```
494494
#[inline]
495495
#[must_use]

0 commit comments

Comments
 (0)