1
Fork 0

rename sub_ptr 😅

This commit is contained in:
bendn 2025-02-23 20:26:28 +07:00
parent 1805b33483
commit c813d8f3e4
No known key found for this signature in database
GPG key ID: 0D9D3A2A3B2A93D6
18 changed files with 51 additions and 51 deletions

View file

@ -280,13 +280,13 @@ impl<'a> MemDecoder<'a> {
#[inline] #[inline]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
// SAFETY: This recovers the length of the original slice, only using members we never modify. // SAFETY: This recovers the length of the original slice, only using members we never modify.
unsafe { self.end.sub_ptr(self.start) } unsafe { self.end.offset_from_unsigned(self.start) }
} }
#[inline] #[inline]
pub fn remaining(&self) -> usize { pub fn remaining(&self) -> usize {
// SAFETY: This type guarantees current <= end. // SAFETY: This type guarantees current <= end.
unsafe { self.end.sub_ptr(self.current) } unsafe { self.end.offset_from_unsigned(self.current) }
} }
#[cold] #[cold]
@ -400,7 +400,7 @@ impl<'a> Decoder for MemDecoder<'a> {
#[inline] #[inline]
fn position(&self) -> usize { fn position(&self) -> usize {
// SAFETY: This type guarantees start <= current // SAFETY: This type guarantees start <= current
unsafe { self.current.sub_ptr(self.start) } unsafe { self.current.offset_from_unsigned(self.start) }
} }
} }

View file

@ -232,7 +232,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 // 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. // invalidate raw pointers to it which some unsafe code might rely on.
let vec_ptr = vec.as_mut().as_mut_ptr(); let vec_ptr = vec.as_mut().as_mut_ptr();
let drop_offset = drop_ptr.sub_ptr(vec_ptr); let drop_offset = drop_ptr.offset_from_unsigned(vec_ptr);
let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len); let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len);
ptr::drop_in_place(to_drop); ptr::drop_in_place(to_drop);
} }

View file

@ -379,7 +379,7 @@ where
let sink = let sink =
self.try_fold::<_, _, Result<_, !>>(sink, write_in_place_with_drop(end)).into_ok(); self.try_fold::<_, _, Result<_, !>>(sink, write_in_place_with_drop(end)).into_ok();
// iteration succeeded, don't drop head // iteration succeeded, don't drop head
unsafe { ManuallyDrop::new(sink).dst.sub_ptr(dst_buf) } unsafe { ManuallyDrop::new(sink).dst.offset_from_unsigned(dst_buf) }
} }
} }

View file

@ -14,7 +14,7 @@ pub(super) struct InPlaceDrop<T> {
impl<T> InPlaceDrop<T> { impl<T> InPlaceDrop<T> {
fn len(&self) -> usize { fn len(&self) -> usize {
unsafe { self.dst.sub_ptr(self.inner) } unsafe { self.dst.offset_from_unsigned(self.inner) }
} }
} }

View file

@ -179,7 +179,7 @@ impl<T, A: Allocator> IntoIter<T, A> {
// say that they're all at the beginning of the "allocation". // say that they're all at the beginning of the "allocation".
0..this.len() 0..this.len()
} else { } else {
this.ptr.sub_ptr(this.buf)..this.end.sub_ptr(buf) this.ptr.offset_from_unsigned(this.buf)..this.end.offset_from_unsigned(buf)
}; };
let cap = this.cap; let cap = this.cap;
let alloc = ManuallyDrop::take(&mut this.alloc); let alloc = ManuallyDrop::take(&mut this.alloc);
@ -230,7 +230,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
let exact = if T::IS_ZST { let exact = if T::IS_ZST {
self.end.addr().wrapping_sub(self.ptr.as_ptr().addr()) self.end.addr().wrapping_sub(self.ptr.as_ptr().addr())
} else { } else {
unsafe { non_null!(self.end, T).sub_ptr(self.ptr) } unsafe { non_null!(self.end, T).offset_from_unsigned(self.ptr) }
}; };
(exact, Some(exact)) (exact, Some(exact))
} }

View file

@ -724,7 +724,7 @@ impl<T: ?Sized> *const T {
/// that their safety preconditions are met: /// that their safety preconditions are met:
/// ```rust /// ```rust
/// # unsafe fn blah(ptr: *const i32, origin: *const i32, count: usize) -> bool { unsafe { /// # unsafe fn blah(ptr: *const i32, origin: *const i32, count: usize) -> bool { unsafe {
/// ptr.sub_ptr(origin) == count /// ptr.offset_from_unsigned(origin) == count
/// # && /// # &&
/// origin.add(count) == ptr /// origin.add(count) == ptr
/// # && /// # &&
@ -755,20 +755,20 @@ impl<T: ?Sized> *const T {
/// let ptr1: *const i32 = &a[1]; /// let ptr1: *const i32 = &a[1];
/// let ptr2: *const i32 = &a[3]; /// let ptr2: *const i32 = &a[3];
/// unsafe { /// unsafe {
/// assert_eq!(ptr2.sub_ptr(ptr1), 2); /// assert_eq!(ptr2.offset_from_unsigned(ptr1), 2);
/// assert_eq!(ptr1.add(2), ptr2); /// assert_eq!(ptr1.add(2), ptr2);
/// assert_eq!(ptr2.sub(2), ptr1); /// assert_eq!(ptr2.sub(2), ptr1);
/// assert_eq!(ptr2.sub_ptr(ptr2), 0); /// assert_eq!(ptr2.offset_from_unsigned(ptr2), 0);
/// } /// }
/// ///
/// // This would be incorrect, as the pointers are not correctly ordered: /// // This would be incorrect, as the pointers are not correctly ordered:
/// // ptr1.sub_ptr(ptr2) /// // ptr1.offset_from_unsigned(ptr2)
/// ``` /// ```
#[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")] #[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")] #[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[inline] #[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn sub_ptr(self, origin: *const T) -> usize pub const unsafe fn offset_from_unsigned(self, origin: *const T) -> usize
where where
T: Sized, T: Sized,
{ {
@ -786,7 +786,7 @@ impl<T: ?Sized> *const T {
ub_checks::assert_unsafe_precondition!( ub_checks::assert_unsafe_precondition!(
check_language_ub, check_language_ub,
"ptr::sub_ptr requires `self >= origin`", "ptr::offset_from_unsigned requires `self >= origin`",
( (
this: *const () = self as *const (), this: *const () = self as *const (),
origin: *const () = origin as *const (), origin: *const () = origin as *const (),
@ -804,7 +804,7 @@ impl<T: ?Sized> *const T {
/// units of **bytes**. /// units of **bytes**.
/// ///
/// This is purely a convenience for casting to a `u8` pointer and /// This is purely a convenience for casting to a `u8` pointer and
/// using [`sub_ptr`][pointer::sub_ptr] on it. See that method for /// using [`sub_ptr`][pointer::offset_from_unsigned] on it. See that method for
/// documentation and safety requirements. /// documentation and safety requirements.
/// ///
/// For non-`Sized` pointees this operation considers only the data pointers, /// For non-`Sized` pointees this operation considers only the data pointers,
@ -813,9 +813,9 @@ impl<T: ?Sized> *const T {
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")] #[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[inline] #[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: *const U) -> usize { pub const unsafe fn byte_offset_from_unsigned<U: ?Sized>(self, origin: *const U) -> usize {
// SAFETY: the caller must uphold the safety contract for `sub_ptr`. // SAFETY: the caller must uphold the safety contract for `sub_ptr`.
unsafe { self.cast::<u8>().sub_ptr(origin.cast::<u8>()) } unsafe { self.cast::<u8>().offset_from_unsigned(origin.cast::<u8>()) }
} }
/// Returns whether two pointers are guaranteed to be equal. /// Returns whether two pointers are guaranteed to be equal.

View file

@ -896,7 +896,7 @@ impl<T: ?Sized> *mut T {
/// that their safety preconditions are met: /// that their safety preconditions are met:
/// ```rust /// ```rust
/// # unsafe fn blah(ptr: *mut i32, origin: *mut i32, count: usize) -> bool { unsafe { /// # unsafe fn blah(ptr: *mut i32, origin: *mut i32, count: usize) -> bool { unsafe {
/// ptr.sub_ptr(origin) == count /// ptr.offset_from_unsigned(origin) == count
/// # && /// # &&
/// origin.add(count) == ptr /// origin.add(count) == ptr
/// # && /// # &&
@ -929,10 +929,10 @@ impl<T: ?Sized> *mut T {
/// let ptr1: *mut i32 = p.add(1); /// let ptr1: *mut i32 = p.add(1);
/// let ptr2: *mut i32 = p.add(3); /// let ptr2: *mut i32 = p.add(3);
/// ///
/// assert_eq!(ptr2.sub_ptr(ptr1), 2); /// assert_eq!(ptr2.offset_from_unsigned(ptr1), 2);
/// assert_eq!(ptr1.add(2), ptr2); /// assert_eq!(ptr1.add(2), ptr2);
/// assert_eq!(ptr2.sub(2), ptr1); /// assert_eq!(ptr2.sub(2), ptr1);
/// assert_eq!(ptr2.sub_ptr(ptr2), 0); /// assert_eq!(ptr2.offset_from_unsigned(ptr2), 0);
/// } /// }
/// ///
/// // This would be incorrect, as the pointers are not correctly ordered: /// // This would be incorrect, as the pointers are not correctly ordered:
@ -941,12 +941,12 @@ impl<T: ?Sized> *mut T {
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")] #[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[inline] #[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn sub_ptr(self, origin: *const T) -> usize pub const unsafe fn offset_from_unsigned(self, origin: *const T) -> usize
where where
T: Sized, T: Sized,
{ {
// SAFETY: the caller must uphold the safety contract for `sub_ptr`. // SAFETY: the caller must uphold the safety contract for `sub_ptr`.
unsafe { (self as *const T).sub_ptr(origin) } unsafe { (self as *const T).offset_from_unsigned(origin) }
} }
/// Calculates the distance between two pointers within the same allocation, *where it's known that /// Calculates the distance between two pointers within the same allocation, *where it's known that
@ -954,7 +954,7 @@ impl<T: ?Sized> *mut T {
/// units of **bytes**. /// units of **bytes**.
/// ///
/// This is purely a convenience for casting to a `u8` pointer and /// This is purely a convenience for casting to a `u8` pointer and
/// using [`sub_ptr`][pointer::sub_ptr] on it. See that method for /// using [`sub_ptr`][pointer::offset_from_unsigned] on it. See that method for
/// documentation and safety requirements. /// documentation and safety requirements.
/// ///
/// For non-`Sized` pointees this operation considers only the data pointers, /// For non-`Sized` pointees this operation considers only the data pointers,
@ -963,9 +963,9 @@ impl<T: ?Sized> *mut T {
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")] #[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[inline] #[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: *mut U) -> usize { pub const unsafe fn byte_offset_from_unsigned<U: ?Sized>(self, origin: *mut U) -> usize {
// SAFETY: the caller must uphold the safety contract for `byte_sub_ptr`. // SAFETY: the caller must uphold the safety contract for `byte_sub_ptr`.
unsafe { (self as *const T).byte_sub_ptr(origin) } unsafe { (self as *const T).byte_offset_from_unsigned(origin) }
} }
/// Adds an unsigned offset to a pointer. /// Adds an unsigned offset to a pointer.

View file

@ -857,7 +857,7 @@ impl<T: ?Sized> NonNull<T> {
/// that their safety preconditions are met: /// that their safety preconditions are met:
/// ```rust /// ```rust
/// # unsafe fn blah(ptr: std::ptr::NonNull<u32>, origin: std::ptr::NonNull<u32>, count: usize) -> bool { unsafe { /// # unsafe fn blah(ptr: std::ptr::NonNull<u32>, origin: std::ptr::NonNull<u32>, count: usize) -> bool { unsafe {
/// ptr.sub_ptr(origin) == count /// ptr.offset_from_unsigned(origin) == count
/// # && /// # &&
/// origin.add(count) == ptr /// origin.add(count) == ptr
/// # && /// # &&
@ -890,25 +890,25 @@ impl<T: ?Sized> NonNull<T> {
/// let ptr1: NonNull<u32> = NonNull::from(&a[1]); /// let ptr1: NonNull<u32> = NonNull::from(&a[1]);
/// let ptr2: NonNull<u32> = NonNull::from(&a[3]); /// let ptr2: NonNull<u32> = NonNull::from(&a[3]);
/// unsafe { /// unsafe {
/// assert_eq!(ptr2.sub_ptr(ptr1), 2); /// assert_eq!(ptr2.offset_from_unsigned(ptr1), 2);
/// assert_eq!(ptr1.add(2), ptr2); /// assert_eq!(ptr1.add(2), ptr2);
/// assert_eq!(ptr2.sub(2), ptr1); /// assert_eq!(ptr2.sub(2), ptr1);
/// assert_eq!(ptr2.sub_ptr(ptr2), 0); /// assert_eq!(ptr2.offset_from_unsigned(ptr2), 0);
/// } /// }
/// ///
/// // This would be incorrect, as the pointers are not correctly ordered: /// // This would be incorrect, as the pointers are not correctly ordered:
/// // ptr1.sub_ptr(ptr2) /// // ptr1.offset_from_unsigned(ptr2)
/// ``` /// ```
#[inline] #[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")] #[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")] #[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
pub const unsafe fn sub_ptr(self, subtracted: NonNull<T>) -> usize pub const unsafe fn offset_from_unsigned(self, subtracted: NonNull<T>) -> usize
where where
T: Sized, T: Sized,
{ {
// SAFETY: the caller must uphold the safety contract for `sub_ptr`. // SAFETY: the caller must uphold the safety contract for `sub_ptr`.
unsafe { self.as_ptr().sub_ptr(subtracted.as_ptr()) } unsafe { self.as_ptr().offset_from_unsigned(subtracted.as_ptr()) }
} }
/// Calculates the distance between two pointers within the same allocation, *where it's known that /// Calculates the distance between two pointers within the same allocation, *where it's known that
@ -916,7 +916,7 @@ impl<T: ?Sized> NonNull<T> {
/// units of **bytes**. /// units of **bytes**.
/// ///
/// This is purely a convenience for casting to a `u8` pointer and /// This is purely a convenience for casting to a `u8` pointer and
/// using [`sub_ptr`][NonNull::sub_ptr] on it. See that method for /// using [`sub_ptr`][NonNull::offset_from_unsigned] on it. See that method for
/// documentation and safety requirements. /// documentation and safety requirements.
/// ///
/// For non-`Sized` pointees this operation considers only the data pointers, /// For non-`Sized` pointees this operation considers only the data pointers,
@ -925,9 +925,9 @@ impl<T: ?Sized> NonNull<T> {
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")] #[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")] #[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: NonNull<U>) -> usize { pub const unsafe fn byte_offset_from_unsigned<U: ?Sized>(self, origin: NonNull<U>) -> usize {
// SAFETY: the caller must uphold the safety contract for `byte_sub_ptr`. // SAFETY: the caller must uphold the safety contract for `byte_sub_ptr`.
unsafe { self.as_ptr().byte_sub_ptr(origin.as_ptr()) } unsafe { self.as_ptr().byte_offset_from_unsigned(origin.as_ptr()) }
} }
/// Reads the value from `self` without moving it. This leaves the /// Reads the value from `self` without moving it. This leaves the

View file

@ -54,7 +54,7 @@ macro_rules! len {
// To get rid of some bounds checks (see `position`), we use ptr_sub instead of // To get rid of some bounds checks (see `position`), we use ptr_sub instead of
// offset_from (Tested by `codegen/slice-position-bounds-check`.) // offset_from (Tested by `codegen/slice-position-bounds-check`.)
// SAFETY: by the type invariant pointers are aligned and `start <= end` // SAFETY: by the type invariant pointers are aligned and `start <= end`
unsafe { end.sub_ptr($self.ptr) } unsafe { end.offset_from_unsigned($self.ptr) }
}, },
) )
}}; }};

View file

@ -272,7 +272,7 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
#[rustc_const_unstable(feature = "const_slice_from_ptr_range", issue = "89792")] #[rustc_const_unstable(feature = "const_slice_from_ptr_range", issue = "89792")]
pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] { pub const 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`. // SAFETY: the caller must uphold the safety contract for `from_ptr_range`.
unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } unsafe { from_raw_parts(range.start, range.end.offset_from_unsigned(range.start)) }
} }
/// Forms a mutable slice from a pointer range. /// Forms a mutable slice from a pointer range.
@ -342,5 +342,5 @@ pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
#[rustc_const_unstable(feature = "const_slice_from_mut_ptr_range", issue = "89792")] #[rustc_const_unstable(feature = "const_slice_from_mut_ptr_range", issue = "89792")]
pub const unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] { pub const 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`. // SAFETY: the caller must uphold the safety contract for `from_mut_ptr_range`.
unsafe { from_raw_parts_mut(range.start, range.end.sub_ptr(range.start)) } unsafe { from_raw_parts_mut(range.start, range.end.offset_from_unsigned(range.start)) }
} }

View file

@ -31,9 +31,9 @@ pub fn choose_pivot<T, F: FnMut(&T, &T) -> bool>(v: &[T], is_less: &mut F) -> us
let c = v_base.add(len_div_8 * 7); // [7*floor(n/8), 8*floor(n/8)) let c = v_base.add(len_div_8 * 7); // [7*floor(n/8), 8*floor(n/8))
if len < PSEUDO_MEDIAN_REC_THRESHOLD { if len < PSEUDO_MEDIAN_REC_THRESHOLD {
median3(&*a, &*b, &*c, is_less).sub_ptr(v_base) median3(&*a, &*b, &*c, is_less).offset_from_unsigned(v_base)
} else { } else {
median3_rec(a, b, c, len_div_8, is_less).sub_ptr(v_base) median3_rec(a, b, c, len_div_8, is_less).offset_from_unsigned(v_base)
} }
} }
} }

View file

@ -143,7 +143,7 @@ impl<T> Drop for MergeState<T> {
// leave the input slice `v` with each original element and all possible // leave the input slice `v` with each original element and all possible
// modifications observed. // modifications observed.
unsafe { unsafe {
let len = self.end.sub_ptr(self.start); let len = self.end.offset_from_unsigned(self.start);
ptr::copy_nonoverlapping(self.start, self.dst, len); ptr::copy_nonoverlapping(self.start, self.dst, len);
} }
} }

View file

@ -224,7 +224,7 @@ where
left = left.add(1); left = left.add(1);
} }
left.sub_ptr(v_base) left.offset_from_unsigned(v_base)
// `gap_opt` goes out of scope and overwrites the last wrong-side element on the right side // `gap_opt` goes out of scope and overwrites the last wrong-side element on the right side
// with the first wrong-side element of the left side that was initially overwritten by the // with the first wrong-side element of the left side that was initially overwritten by the

View file

@ -3,5 +3,5 @@ fn main() {
let arr = [0u8; 8]; let arr = [0u8; 8];
let ptr1 = arr.as_ptr(); let ptr1 = arr.as_ptr();
let ptr2 = ptr1.wrapping_add(4); let ptr2 = ptr1.wrapping_add(4);
let _val = unsafe { ptr1.sub_ptr(ptr2) }; //~ERROR: first pointer has smaller address than second let _val = unsafe { ptr1.offset_from_unsigned(ptr2) }; //~ERROR: first pointer has smaller address than second
} }

View file

@ -1,8 +1,8 @@
error: Undefined Behavior: `ptr_offset_from_unsigned` called when first pointer has smaller address than second: $ADDR < $ADDR error: Undefined Behavior: `ptr_offset_from_unsigned` called when first pointer has smaller address than second: $ADDR < $ADDR
--> tests/fail/intrinsics/ptr_offset_from_unsigned_neg.rs:LL:CC --> tests/fail/intrinsics/ptr_offset_from_unsigned_neg.rs:LL:CC
| |
LL | let _val = unsafe { ptr1.sub_ptr(ptr2) }; LL | let _val = unsafe { ptr1.offset_from_unsigned(ptr2) };
| ^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer has smaller address than second: $ADDR < $ADDR | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer has smaller address than second: $ADDR < $ADDR
| |
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

View file

@ -21,7 +21,7 @@ fn smoke() {
let _val = ptr.wrapping_sub(0); let _val = ptr.wrapping_sub(0);
let _val = unsafe { ptr.sub(0) }; let _val = unsafe { ptr.sub(0) };
let _val = unsafe { ptr.offset_from(ptr) }; let _val = unsafe { ptr.offset_from(ptr) };
let _val = unsafe { ptr.sub_ptr(ptr) }; let _val = unsafe { ptr.offset_from_unsigned(ptr) };
} }
fn test_offset_from() { fn test_offset_from() {
@ -32,14 +32,14 @@ fn test_offset_from() {
let y = x.offset(12); let y = x.offset(12);
assert_eq!(y.offset_from(x), 12); assert_eq!(y.offset_from(x), 12);
assert_eq!(y.sub_ptr(x), 12); assert_eq!(y.offset_from_unsigned(x), 12);
assert_eq!(x.offset_from(y), -12); assert_eq!(x.offset_from(y), -12);
assert_eq!((y as *const u32).offset_from(x as *const u32), 12 / 4); assert_eq!((y as *const u32).offset_from(x as *const u32), 12 / 4);
assert_eq!((x as *const u32).offset_from(y as *const u32), -12 / 4); assert_eq!((x as *const u32).offset_from(y as *const u32), -12 / 4);
let x = (((x as usize) * 2) / 2) as *const u8; let x = (((x as usize) * 2) / 2) as *const u8;
assert_eq!(y.offset_from(x), 12); assert_eq!(y.offset_from(x), 12);
assert_eq!(y.sub_ptr(x), 12); assert_eq!(y.offset_from_unsigned(x), 12);
assert_eq!(x.offset_from(y), -12); assert_eq!(x.offset_from(y), -12);
} }
} }

View file

@ -104,7 +104,7 @@ error[E0080]: could not evaluate static initializer
| |
= note: the evaluated program panicked at 'assertion failed: 0 < pointee_size && pointee_size <= isize::MAX as usize', $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL = note: the evaluated program panicked at 'assertion failed: 0 < pointee_size && pointee_size <= isize::MAX as usize', $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
| |
note: inside `std::ptr::const_ptr::<impl *const ()>::sub_ptr` note: inside `std::ptr::const_ptr::<impl *const ()>::offset_from_unsigned`
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
note: inside `from_ptr_range::<'_, ()>` note: inside `from_ptr_range::<'_, ()>`
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
@ -192,7 +192,7 @@ error[E0080]: could not evaluate static initializer
| |
= note: `ptr_offset_from_unsigned` called on two different pointers that are not both derived from the same allocation = note: `ptr_offset_from_unsigned` called on two different pointers that are not both derived from the same allocation
| |
note: inside `std::ptr::const_ptr::<impl *const u32>::sub_ptr` note: inside `std::ptr::const_ptr::<impl *const u32>::offset_from_unsigned`
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
note: inside `from_ptr_range::<'_, u32>` note: inside `from_ptr_range::<'_, u32>`
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
@ -207,7 +207,7 @@ error[E0080]: could not evaluate static initializer
| |
= note: `ptr_offset_from_unsigned` called on two different pointers that are not both derived from the same allocation = note: `ptr_offset_from_unsigned` called on two different pointers that are not both derived from the same allocation
| |
note: inside `std::ptr::const_ptr::<impl *const u32>::sub_ptr` note: inside `std::ptr::const_ptr::<impl *const u32>::offset_from_unsigned`
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
note: inside `from_ptr_range::<'_, u32>` note: inside `from_ptr_range::<'_, u32>`
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL --> $SRC_DIR/core/src/slice/raw.rs:LL:COL

View file

@ -44,7 +44,7 @@ pub const OFFSET_EQUAL_INTS: isize = {
pub const OFFSET_UNSIGNED: usize = { pub const OFFSET_UNSIGNED: usize = {
let a = ['a', 'b', 'c']; let a = ['a', 'b', 'c'];
let ptr = a.as_ptr(); let ptr = a.as_ptr();
unsafe { ptr.add(2).sub_ptr(ptr) } unsafe { ptr.add(2).offset_from_unsigned(ptr) }
}; };
fn main() { fn main() {