about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Kimock <kimockb@gmail.com>2022-04-03 13:23:27 -0400
committerBen Kimock <kimockb@gmail.com>2022-04-03 16:16:33 -0400
commitf4a7ed433834e8ea2775097a9bf433880d6e34a3 (patch)
tree8e439b642f4d65fd311b972b4c3516634c8232fe
parent15a242a432c9c40a60def102209a5d40900b7b9d (diff)
Fix &mut invalidation in ptr::swap doctest
Under Stacked Borrows with raw pointer tagging, the previous code was UB
because the code which creates the the second pointer borrows the array
through a tag in the borrow stacks below the Unique tag that our first
pointer is based on, thus invalidating the first pointer.

This is not definitely a bug and may never be real UB, but I desperately
want people to write code that conforms to SB with raw pointer tagging
so that I can write good diagnostics. The alternative aliasing models
aren't possible to diagnose well due to state space explosion.
Therefore, it would be super cool if the standard library nudged people
towards writing code that is valid with respect to SB with raw pointer
tagging.
-rw-r--r--library/core/src/ptr/mod.rs5
1 files changed, 3 insertions, 2 deletions
diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs
index e2a19c1b581..91e2bd7953f 100644
--- a/library/core/src/ptr/mod.rs
+++ b/library/core/src/ptr/mod.rs
@@ -637,8 +637,9 @@ pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
 ///
 /// let mut array = [0, 1, 2, 3];
 ///
-/// let x = array[0..].as_mut_ptr() as *mut [u32; 2]; // this is `array[0..2]`
-/// let y = array[2..].as_mut_ptr() as *mut [u32; 2]; // this is `array[2..4]`
+/// let (x, y) = array.split_at_mut(2);
+/// let x = x.as_mut_ptr().cast::<[u32; 2]>(); // this is `array[0..2]`
+/// let y = y.as_mut_ptr().cast::<[u32; 2]>(); // this is `array[2..4]`
 ///
 /// unsafe {
 ///     ptr::swap(x, y);