Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compiler/rustc_serialize/src/opaque.rs
Original file line number Diff line number Diff line change
@@ -280,13 +280,13 @@ impl<'a> MemDecoder<'a> {
#[inline]
pub fn len(&self) -> usize {
// 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]
pub fn remaining(&self) -> usize {
// SAFETY: This type guarantees current <= end.
unsafe { self.end.sub_ptr(self.current) }
unsafe { self.end.offset_from_unsigned(self.current) }
}

#[cold]
@@ -400,7 +400,7 @@ impl<'a> Decoder for MemDecoder<'a> {
#[inline]
fn position(&self) -> usize {
// SAFETY: This type guarantees start <= current
unsafe { self.current.sub_ptr(self.start) }
unsafe { self.current.offset_from_unsigned(self.start) }
}
}

2 changes: 1 addition & 1 deletion library/alloc/src/vec/drain.rs
Original file line number Diff line number Diff line change
@@ -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
// 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.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);
ptr::drop_in_place(to_drop);
}
2 changes: 1 addition & 1 deletion library/alloc/src/vec/in_place_collect.rs
Original file line number Diff line number Diff line change
@@ -379,7 +379,7 @@ where
let sink =
self.try_fold::<_, _, Result<_, !>>(sink, write_in_place_with_drop(end)).into_ok();
// 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) }
}
}

2 changes: 1 addition & 1 deletion library/alloc/src/vec/in_place_drop.rs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ pub(super) struct InPlaceDrop<T> {

impl<T> InPlaceDrop<T> {
fn len(&self) -> usize {
unsafe { self.dst.sub_ptr(self.inner) }
unsafe { self.dst.offset_from_unsigned(self.inner) }
}
}

4 changes: 2 additions & 2 deletions library/alloc/src/vec/into_iter.rs
Original file line number Diff line number Diff line change
@@ -179,7 +179,7 @@ impl<T, A: Allocator> IntoIter<T, A> {
// say that they're all at the beginning of the "allocation".
0..this.len()
} 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 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 {
self.end.addr().wrapping_sub(self.ptr.as_ptr().addr())
} 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))
}
18 changes: 9 additions & 9 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
@@ -724,7 +724,7 @@ impl<T: ?Sized> *const T {
/// that their safety preconditions are met:
/// ```rust
/// # 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
/// # &&
@@ -755,20 +755,20 @@ impl<T: ?Sized> *const T {
/// let ptr1: *const i32 = &a[1];
/// let ptr2: *const i32 = &a[3];
/// unsafe {
/// assert_eq!(ptr2.sub_ptr(ptr1), 2);
/// assert_eq!(ptr2.offset_from_unsigned(ptr1), 2);
/// assert_eq!(ptr1.add(2), ptr2);
/// 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:
/// // ptr1.sub_ptr(ptr2)
/// // ptr1.offset_from_unsigned(ptr2)
/// ```
#[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[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
T: Sized,
{
@@ -786,7 +786,7 @@ impl<T: ?Sized> *const T {

ub_checks::assert_unsafe_precondition!(
check_language_ub,
"ptr::sub_ptr requires `self >= origin`",
"ptr::offset_from_unsigned requires `self >= origin`",
(
this: *const () = self as *const (),
origin: *const () = origin as *const (),
@@ -804,7 +804,7 @@ impl<T: ?Sized> *const T {
/// units of **bytes**.
///
/// 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.
///
/// 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")]
#[inline]
#[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`.
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.
16 changes: 8 additions & 8 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
@@ -896,7 +896,7 @@ impl<T: ?Sized> *mut T {
/// that their safety preconditions are met:
/// ```rust
/// # 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
/// # &&
@@ -929,10 +929,10 @@ impl<T: ?Sized> *mut T {
/// let ptr1: *mut i32 = p.add(1);
/// 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!(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:
@@ -941,20 +941,20 @@ impl<T: ?Sized> *mut T {
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[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
T: Sized,
{
// 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
/// `self` is equal to or greater than `origin`*. The returned value is in
/// units of **bytes**.
///
/// 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.
///
/// 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")]
#[inline]
#[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`.
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.
18 changes: 9 additions & 9 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
@@ -857,7 +857,7 @@ impl<T: ?Sized> NonNull<T> {
/// that their safety preconditions are met:
/// ```rust
/// # 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
/// # &&
@@ -890,33 +890,33 @@ impl<T: ?Sized> NonNull<T> {
/// let ptr1: NonNull<u32> = NonNull::from(&a[1]);
/// let ptr2: NonNull<u32> = NonNull::from(&a[3]);
/// unsafe {
/// assert_eq!(ptr2.sub_ptr(ptr1), 2);
/// assert_eq!(ptr2.offset_from_unsigned(ptr1), 2);
/// assert_eq!(ptr1.add(2), ptr2);
/// 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:
/// // ptr1.sub_ptr(ptr2)
/// // ptr1.offset_from_unsigned(ptr2)
/// ```
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[stable(feature = "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
T: Sized,
{
// 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
/// `self` is equal to or greater than `origin`*. The returned value is in
/// units of **bytes**.
///
/// 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.
///
/// 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
#[stable(feature = "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`.
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
2 changes: 1 addition & 1 deletion library/core/src/slice/iter/macros.rs
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ macro_rules! len {
// To get rid of some bounds checks (see `position`), we use ptr_sub instead of
// offset_from (Tested by `codegen/slice-position-bounds-check`.)
// SAFETY: by the type invariant pointers are aligned and `start <= end`
unsafe { end.sub_ptr($self.ptr) }
unsafe { end.offset_from_unsigned($self.ptr) }
},
)
}};
4 changes: 2 additions & 2 deletions library/core/src/slice/raw.rs
Original file line number Diff line number Diff line change
@@ -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")]
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`.
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.
@@ -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")]
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`.
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)) }
}
4 changes: 2 additions & 2 deletions library/core/src/slice/sort/shared/pivot.rs
Original file line number Diff line number Diff line change
@@ -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))

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 {
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)
}
}
}
2 changes: 1 addition & 1 deletion library/core/src/slice/sort/stable/merge.rs
Original file line number Diff line number Diff line change
@@ -143,7 +143,7 @@ impl<T> Drop for MergeState<T> {
// leave the input slice `v` with each original element and all possible
// modifications observed.
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);
}
}
2 changes: 1 addition & 1 deletion library/core/src/slice/sort/unstable/quicksort.rs
Original file line number Diff line number Diff line change
@@ -224,7 +224,7 @@ where
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
// with the first wrong-side element of the left side that was initially overwritten by the
Original file line number Diff line number Diff line change
@@ -3,5 +3,5 @@ fn main() {
let arr = [0u8; 8];
let ptr1 = arr.as_ptr();
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
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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
|
LL | let _val = unsafe { ptr1.sub_ptr(ptr2) };
| ^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer has smaller address than second: $ADDR < $ADDR
LL | let _val = unsafe { ptr1.offset_from_unsigned(ptr2) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `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: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
6 changes: 3 additions & 3 deletions src/tools/miri/tests/pass/ptr_offset.rs
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ fn smoke() {
let _val = ptr.wrapping_sub(0);
let _val = unsafe { ptr.sub(0) };
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() {
@@ -32,14 +32,14 @@ fn test_offset_from() {
let y = x.offset(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!((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);

let x = (((x as usize) * 2) / 2) as *const u8;
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);
}
}
6 changes: 3 additions & 3 deletions tests/ui/const-ptr/forbidden_slices.stderr
Original file line number Diff line number Diff line change
@@ -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: 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
note: inside `from_ptr_range::<'_, ()>`
--> $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: 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
note: inside `from_ptr_range::<'_, u32>`
--> $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: 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
note: inside `from_ptr_range::<'_, u32>`
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
2 changes: 1 addition & 1 deletion tests/ui/consts/offset_from.rs
Original file line number Diff line number Diff line change
@@ -44,7 +44,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).sub_ptr(ptr) }
unsafe { ptr.add(2).offset_from_unsigned(ptr) }
};

fn main() {