about summary refs log tree commit diff
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2022-04-09 14:14:35 -0700
committerScott McMurray <scottmcm@users.noreply.github.com>2022-05-11 17:16:25 -0700
commite76b3f3b5b1bdb39d064a34f8c9a601633e749c2 (patch)
treed3f8806a82da3810133c3520cdf89acb8a1186a3
parent89a18cb60053a6c1b3b5f817246ddfd917be1e3e (diff)
Rename `unsigned_offset_from` to `sub_ptr`
-rw-r--r--compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs2
-rw-r--r--library/alloc/src/slice.rs2
-rw-r--r--library/alloc/src/vec/drain.rs2
-rw-r--r--library/alloc/src/vec/in_place_collect.rs2
-rw-r--r--library/alloc/src/vec/in_place_drop.rs2
-rw-r--r--library/alloc/src/vec/into_iter.rs2
-rw-r--r--library/core/src/intrinsics.rs2
-rw-r--r--library/core/src/ptr/const_ptr.rs22
-rw-r--r--library/core/src/ptr/mut_ptr.rs26
-rw-r--r--library/core/src/slice/raw.rs4
-rw-r--r--src/test/ui/consts/offset_from.rs2
11 files changed, 46 insertions, 22 deletions
diff --git a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs
index b254ca3bec8..f7a83373e87 100644
--- a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs
+++ b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs
@@ -721,7 +721,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
             let diff_bytes = fx.bcx.ins().isub(ptr, base);
             // FIXME this can be an exact division.
             let diff = if intrinsic == sym::ptr_offset_from_unsigned {
-                // Because diff_bytes ULT isize::MAX, this would be fine as signed,
+                // Because diff_bytes ULE isize::MAX, this would be fine as signed,
                 // but unsigned is slightly easier to codegen, so might as well.
                 fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64)
             } else {
diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs
index 376aa4812bd..199b3c9d029 100644
--- a/library/alloc/src/slice.rs
+++ b/library/alloc/src/slice.rs
@@ -1056,7 +1056,7 @@ where
         fn drop(&mut self) {
             // `T` is not a zero-sized type, and these are pointers into a slice's elements.
             unsafe {
-                let len = self.end.unsigned_offset_from(self.start);
+                let len = self.end.sub_ptr(self.start);
                 ptr::copy_nonoverlapping(self.start, self.dest, len);
             }
         }
diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs
index 1e4d911e7e6..5cdee0bd4da 100644
--- a/library/alloc/src/vec/drain.rs
+++ b/library/alloc/src/vec/drain.rs
@@ -163,7 +163,7 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
             // it from the original vec but also avoid creating a &mut to the front since that could
             // invalidate raw pointers to it which some unsafe code might rely on.
             let vec_ptr = vec.as_mut().as_mut_ptr();
-            let drop_offset = drop_ptr.unsigned_offset_from(vec_ptr);
+            let drop_offset = drop_ptr.sub_ptr(vec_ptr);
             let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len);
             ptr::drop_in_place(to_drop);
         }
diff --git a/library/alloc/src/vec/in_place_collect.rs b/library/alloc/src/vec/in_place_collect.rs
index a393732567b..55dcb84ad16 100644
--- a/library/alloc/src/vec/in_place_collect.rs
+++ b/library/alloc/src/vec/in_place_collect.rs
@@ -250,7 +250,7 @@ where
         let sink =
             self.try_fold::<_, _, Result<_, !>>(sink, write_in_place_with_drop(end)).unwrap();
         // iteration succeeded, don't drop head
-        unsafe { ManuallyDrop::new(sink).dst.unsigned_offset_from(dst_buf) }
+        unsafe { ManuallyDrop::new(sink).dst.sub_ptr(dst_buf) }
     }
 }
 
diff --git a/library/alloc/src/vec/in_place_drop.rs b/library/alloc/src/vec/in_place_drop.rs
index 62334f6daba..1b1ef9130fa 100644
--- a/library/alloc/src/vec/in_place_drop.rs
+++ b/library/alloc/src/vec/in_place_drop.rs
@@ -10,7 +10,7 @@ pub(super) struct InPlaceDrop<T> {
 
 impl<T> InPlaceDrop<T> {
     fn len(&self) -> usize {
-        unsafe { self.dst.unsigned_offset_from(self.inner) }
+        unsafe { self.dst.sub_ptr(self.inner) }
     }
 }
 
diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs
index b4797f37813..9b84a1d9b4b 100644
--- a/library/alloc/src/vec/into_iter.rs
+++ b/library/alloc/src/vec/into_iter.rs
@@ -169,7 +169,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
         let exact = if mem::size_of::<T>() == 0 {
             self.end.addr().wrapping_sub(self.ptr.addr())
         } else {
-            unsafe { self.end.unsigned_offset_from(self.ptr) }
+            unsafe { self.end.sub_ptr(self.ptr) }
         };
         (exact, Some(exact))
     }
diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs
index 10de5ff2d1d..88e4262922d 100644
--- a/library/core/src/intrinsics.rs
+++ b/library/core/src/intrinsics.rs
@@ -1903,7 +1903,7 @@ extern "rust-intrinsic" {
     #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
     pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
 
-    /// See documentation of `<*const T>::unsigned_offset_from` for details.
+    /// See documentation of `<*const T>::sub_ptr` for details.
     #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
     #[cfg(not(bootstrap))]
     pub fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize;
diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs
index 203c78fda0b..5be855bfabb 100644
--- a/library/core/src/ptr/const_ptr.rs
+++ b/library/core/src/ptr/const_ptr.rs
@@ -622,8 +622,20 @@ impl<T: ?Sized> *const T {
     /// but it provides slightly more information to the optimizer, which can
     /// sometimes allow it to optimize slightly better with some backends.
     ///
-    /// This method is the inverse of [`add`](#method.add) (and, with the parameters
-    /// in the other order, of [`sub`](#method.sub)).
+    /// This method can be though of as recovering the `count` that was passed
+    /// to [`add`](#method.add) (or, with the parameters in the other order,
+    /// to [`sub`](#method.sub)).  The following are all equivalent, assuming
+    /// that their safety preconditions are met:
+    /// ```rust
+    /// # #![feature(ptr_unsigned_offset_from)]
+    /// # unsafe fn blah(ptr: *const i32, origin: *const i32, count: usize) -> bool {
+    /// ptr.sub_ptr(origin) == count
+    /// # &&
+    /// origin.add(count) == ptr
+    /// # &&
+    /// ptr.sub(count) == origin
+    /// # }
+    /// ```
     ///
     /// # Safety
     ///
@@ -650,10 +662,10 @@ impl<T: ?Sized> *const T {
     /// let ptr1: *const i32 = &a[1];
     /// let ptr2: *const i32 = &a[3];
     /// unsafe {
-    ///     assert_eq!(ptr2.unsigned_offset_from(ptr1), 2);
+    ///     assert_eq!(ptr2.sub_ptr(ptr1), 2);
     ///     assert_eq!(ptr1.add(2), ptr2);
     ///     assert_eq!(ptr2.sub(2), ptr1);
-    ///     assert_eq!(ptr2.unsigned_offset_from(ptr2), 0);
+    ///     assert_eq!(ptr2.sub_ptr(ptr2), 0);
     /// }
     ///
     /// // This would be incorrect, as the pointers are not correctly ordered:
@@ -662,7 +674,7 @@ impl<T: ?Sized> *const T {
     #[unstable(feature = "ptr_unsigned_offset_from", issue = "88888888")]
     #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
     #[inline]
-    pub const unsafe fn unsigned_offset_from(self, origin: *const T) -> usize
+    pub const unsafe fn sub_ptr(self, origin: *const T) -> usize
     where
         T: Sized,
     {
diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs
index a0457df1b88..c7f297f426b 100644
--- a/library/core/src/ptr/mut_ptr.rs
+++ b/library/core/src/ptr/mut_ptr.rs
@@ -798,8 +798,20 @@ impl<T: ?Sized> *mut T {
     /// but it provides slightly more information to the optimizer, which can
     /// sometimes allow it to optimize slightly better with some backends.
     ///
-    /// This method is the inverse of [`add`](#method.add) (and, with the parameters
-    /// in the other order, of [`sub`](#method.sub)).
+    /// This method can be though of as recovering the `count` that was passed
+    /// to [`add`](#method.add) (or, with the parameters in the other order,
+    /// to [`sub`](#method.sub)).  The following are all equivalent, assuming
+    /// that their safety preconditions are met:
+    /// ```rust
+    /// # #![feature(ptr_unsigned_offset_from)]
+    /// # unsafe fn blah(ptr: *mut i32, origin: *mut i32, count: usize) -> bool {
+    /// ptr.sub_ptr(origin) == count
+    /// # &&
+    /// origin.add(count) == ptr
+    /// # &&
+    /// ptr.sub(count) == origin
+    /// # }
+    /// ```
     ///
     /// # Safety
     ///
@@ -828,10 +840,10 @@ impl<T: ?Sized> *mut T {
     ///     let ptr1: *mut i32 = p.add(1);
     ///     let ptr2: *mut i32 = p.add(3);
     ///
-    ///     assert_eq!(ptr2.unsigned_offset_from(ptr1), 2);
+    ///     assert_eq!(ptr2.sub_ptr(ptr1), 2);
     ///     assert_eq!(ptr1.add(2), ptr2);
     ///     assert_eq!(ptr2.sub(2), ptr1);
-    ///     assert_eq!(ptr2.unsigned_offset_from(ptr2), 0);
+    ///     assert_eq!(ptr2.sub_ptr(ptr2), 0);
     /// }
     ///
     /// // This would be incorrect, as the pointers are not correctly ordered:
@@ -839,12 +851,12 @@ impl<T: ?Sized> *mut T {
     #[unstable(feature = "ptr_unsigned_offset_from", issue = "88888888")]
     #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
     #[inline]
-    pub const unsafe fn unsigned_offset_from(self, origin: *const T) -> usize
+    pub const unsafe fn sub_ptr(self, origin: *const T) -> usize
     where
         T: Sized,
     {
-        // SAFETY: the caller must uphold the safety contract for `unsigned_offset_from`.
-        unsafe { (self as *const T).unsigned_offset_from(origin) }
+        // SAFETY: the caller must uphold the safety contract for `sub_ptr`.
+        unsafe { (self as *const T).sub_ptr(origin) }
     }
 
     /// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
diff --git a/library/core/src/slice/raw.rs b/library/core/src/slice/raw.rs
index e95f8a3143a..6bc60b04b5c 100644
--- a/library/core/src/slice/raw.rs
+++ b/library/core/src/slice/raw.rs
@@ -215,7 +215,7 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
 #[unstable(feature = "slice_from_ptr_range", issue = "89792")]
 pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
     // SAFETY: the caller must uphold the safety contract for `from_ptr_range`.
-    unsafe { from_raw_parts(range.start, range.end.unsigned_offset_from(range.start)) }
+    unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) }
 }
 
 /// Performs the same functionality as [`from_ptr_range`], except that a
@@ -265,5 +265,5 @@ pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
 #[unstable(feature = "slice_from_ptr_range", issue = "89792")]
 pub unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] {
     // SAFETY: the caller must uphold the safety contract for `from_mut_ptr_range`.
-    unsafe { from_raw_parts_mut(range.start, range.end.unsigned_offset_from(range.start)) }
+    unsafe { from_raw_parts_mut(range.start, range.end.sub_ptr(range.start)) }
 }
diff --git a/src/test/ui/consts/offset_from.rs b/src/test/ui/consts/offset_from.rs
index ace6c5b85ad..ad834e885a5 100644
--- a/src/test/ui/consts/offset_from.rs
+++ b/src/test/ui/consts/offset_from.rs
@@ -47,7 +47,7 @@ pub const OFFSET_EQUAL_INTS: isize = {
 pub const OFFSET_UNSIGNED: usize = {
     let a = ['a', 'b', 'c'];
     let ptr = a.as_ptr();
-    unsafe { ptr.add(2).unsigned_offset_from(ptr) }
+    unsafe { ptr.add(2).sub_ptr(ptr) }
 };
 
 fn main() {