about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2021-05-20 00:19:08 +0200
committerGitHub <noreply@github.com>2021-05-20 00:19:08 +0200
commit2065c4b0960a4726f7d6bf53d53ef76f11555de3 (patch)
treedc613dabf0ad48657ab3cc22a9ad1d8aaa4a6be0
parent1207b7fa0bd71e0b3ded4b4cb21c39bf021d95d9 (diff)
parentf9752b493086fd3063c9aef83d2622d322bbc51c (diff)
downloadrust-2065c4b0960a4726f7d6bf53d53ef76f11555de3.tar.gz
rust-2065c4b0960a4726f7d6bf53d53ef76f11555de3.zip
Rollup merge of #85464 - steffahn:fix_ub_in_ptr_swap_docs, r=dtolnay
Fix UB in documented example for `ptr::swap`

Compare [this (short) discussion on zulip](https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/Pointers.20to.20overlapping.20arrays) (or [in the archive](https://zulip-archive.rust-lang.org/122651general/92017Pointerstooverlappingarrays.html), if you don’t have an account).

``@rustbot`` label T-doc T-libs
-rw-r--r--library/core/src/ptr/mod.rs8
1 files changed, 5 insertions, 3 deletions
diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs
index 2c324b15a1a..214d7c8bc15 100644
--- a/library/core/src/ptr/mod.rs
+++ b/library/core/src/ptr/mod.rs
@@ -342,10 +342,12 @@ pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
 /// ```
 /// use std::ptr;
 ///
-/// let mut array = [0, 1, 2, 3];
+/// let mut array: [i32; 4] = [0, 1, 2, 3];
+///
+/// let array_ptr: *mut i32 = array.as_mut_ptr();
 ///
-/// let x = array[0..].as_mut_ptr() as *mut [u32; 3]; // this is `array[0..3]`
-/// let y = array[1..].as_mut_ptr() as *mut [u32; 3]; // this is `array[1..4]`
+/// let x = array_ptr as *mut [i32; 3]; // this is `array[0..3]`
+/// let y = unsafe { array_ptr.add(1) } as *mut [i32; 3]; // this is `array[1..4]`
 ///
 /// unsafe {
 ///     ptr::swap(x, y);