1
Fork 0

Rollup merge of #137121 - bend-n:master, r=Noratrieb

stabilize `(const_)ptr_sub_ptr`

Tracking issue: #95892
Closes #95892
FCP Completed: https://github.com/rust-lang/rust/issues/95892#issuecomment-2561139730

r? ````@Noratrieb````
This commit is contained in:
Matthias Krüger 2025-02-23 00:16:18 +01:00 committed by GitHub
commit 929423a4f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 27 additions and 43 deletions

View file

@ -13,7 +13,6 @@
#![feature(core_intrinsics)] #![feature(core_intrinsics)]
#![feature(min_specialization)] #![feature(min_specialization)]
#![feature(never_type)] #![feature(never_type)]
#![feature(ptr_sub_ptr)]
#![feature(rustdoc_internals)] #![feature(rustdoc_internals)]
#![warn(unreachable_pub)] #![warn(unreachable_pub)]
// tidy-alphabetical-end // tidy-alphabetical-end

View file

@ -137,7 +137,6 @@
#![feature(pointer_like_trait)] #![feature(pointer_like_trait)]
#![feature(ptr_internals)] #![feature(ptr_internals)]
#![feature(ptr_metadata)] #![feature(ptr_metadata)]
#![feature(ptr_sub_ptr)]
#![feature(set_ptr_value)] #![feature(set_ptr_value)]
#![feature(sized_type_properties)] #![feature(sized_type_properties)]
#![feature(slice_from_ptr_range)] #![feature(slice_from_ptr_range)]

View file

@ -3675,6 +3675,7 @@ pub const unsafe fn ptr_offset_from<T>(_ptr: *const T, _base: *const T) -> isize
#[rustc_nounwind] #[rustc_nounwind]
#[rustc_intrinsic] #[rustc_intrinsic]
#[rustc_intrinsic_must_be_overridden] #[rustc_intrinsic_must_be_overridden]
#[rustc_intrinsic_const_stable_indirect]
pub const unsafe fn ptr_offset_from_unsigned<T>(_ptr: *const T, _base: *const T) -> usize { pub const unsafe fn ptr_offset_from_unsigned<T>(_ptr: *const T, _base: *const T) -> usize {
unimplemented!() unimplemented!()
} }

View file

@ -723,7 +723,6 @@ impl<T: ?Sized> *const T {
/// to [`sub`](#method.sub)). The following are all equivalent, assuming /// to [`sub`](#method.sub)). The following are all equivalent, assuming
/// that their safety preconditions are met: /// that their safety preconditions are met:
/// ```rust /// ```rust
/// # #![feature(ptr_sub_ptr)]
/// # 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.sub_ptr(origin) == count
/// # && /// # &&
@ -752,8 +751,6 @@ impl<T: ?Sized> *const T {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(ptr_sub_ptr)]
///
/// let a = [0; 5]; /// let a = [0; 5];
/// let ptr1: *const i32 = &a[1]; /// let ptr1: *const i32 = &a[1];
/// let ptr2: *const i32 = &a[3]; /// let ptr2: *const i32 = &a[3];
@ -767,8 +764,8 @@ impl<T: ?Sized> *const T {
/// // 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.sub_ptr(ptr2)
/// ``` /// ```
#[unstable(feature = "ptr_sub_ptr", issue = "95892")] #[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")] #[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 sub_ptr(self, origin: *const T) -> usize
@ -812,8 +809,8 @@ impl<T: ?Sized> *const T {
/// ///
/// For non-`Sized` pointees this operation considers only the data pointers, /// For non-`Sized` pointees this operation considers only the data pointers,
/// ignoring the metadata. /// ignoring the metadata.
#[unstable(feature = "ptr_sub_ptr", issue = "95892")] #[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")] #[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_sub_ptr<U: ?Sized>(self, origin: *const U) -> usize {

View file

@ -895,7 +895,6 @@ impl<T: ?Sized> *mut T {
/// to [`sub`](#method.sub)). The following are all equivalent, assuming /// to [`sub`](#method.sub)). The following are all equivalent, assuming
/// that their safety preconditions are met: /// that their safety preconditions are met:
/// ```rust /// ```rust
/// # #![feature(ptr_sub_ptr)]
/// # 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.sub_ptr(origin) == count
/// # && /// # &&
@ -924,8 +923,6 @@ impl<T: ?Sized> *mut T {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(ptr_sub_ptr)]
///
/// let mut a = [0; 5]; /// let mut a = [0; 5];
/// let p: *mut i32 = a.as_mut_ptr(); /// let p: *mut i32 = a.as_mut_ptr();
/// unsafe { /// unsafe {
@ -940,8 +937,8 @@ impl<T: ?Sized> *mut T {
/// ///
/// // This would be incorrect, as the pointers are not correctly ordered: /// // This would be incorrect, as the pointers are not correctly ordered:
/// // ptr1.offset_from(ptr2) /// // ptr1.offset_from(ptr2)
#[unstable(feature = "ptr_sub_ptr", issue = "95892")] #[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")] #[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 sub_ptr(self, origin: *const T) -> usize
@ -962,8 +959,8 @@ impl<T: ?Sized> *mut T {
/// ///
/// For non-`Sized` pointees this operation considers only the data pointers, /// For non-`Sized` pointees this operation considers only the data pointers,
/// ignoring the metadata. /// ignoring the metadata.
#[unstable(feature = "ptr_sub_ptr", issue = "95892")] #[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")] #[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_sub_ptr<U: ?Sized>(self, origin: *mut U) -> usize {

View file

@ -856,7 +856,6 @@ impl<T: ?Sized> NonNull<T> {
/// to [`sub`](#method.sub)). The following are all equivalent, assuming /// to [`sub`](#method.sub)). The following are all equivalent, assuming
/// that their safety preconditions are met: /// that their safety preconditions are met:
/// ```rust /// ```rust
/// # #![feature(ptr_sub_ptr)]
/// # 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.sub_ptr(origin) == count
/// # && /// # &&
@ -885,7 +884,6 @@ impl<T: ?Sized> NonNull<T> {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(ptr_sub_ptr)]
/// use std::ptr::NonNull; /// use std::ptr::NonNull;
/// ///
/// let a = [0; 5]; /// let a = [0; 5];
@ -903,8 +901,8 @@ impl<T: ?Sized> NonNull<T> {
/// ``` /// ```
#[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
#[unstable(feature = "ptr_sub_ptr", issue = "95892")] #[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")] #[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 sub_ptr(self, subtracted: NonNull<T>) -> usize
where where
T: Sized, T: Sized,
@ -925,8 +923,8 @@ impl<T: ?Sized> NonNull<T> {
/// ignoring the metadata. /// ignoring the metadata.
#[inline(always)] #[inline(always)]
#[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
#[unstable(feature = "ptr_sub_ptr", issue = "95892")] #[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")] #[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_sub_ptr<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_sub_ptr(origin.as_ptr()) }

View file

@ -1,6 +1,4 @@
//@normalize-stderr-test: "\d+ < \d+" -> "$$ADDR < $$ADDR" //@normalize-stderr-test: "\d+ < \d+" -> "$$ADDR < $$ADDR"
#![feature(ptr_sub_ptr)]
fn main() { fn main() {
let arr = [0u8; 8]; let arr = [0u8; 8];
let ptr1 = arr.as_ptr(); let ptr1 = arr.as_ptr();

View file

@ -1,5 +1,4 @@
//@compile-flags: -Zmiri-permissive-provenance //@compile-flags: -Zmiri-permissive-provenance
#![feature(ptr_sub_ptr)]
use std::{mem, ptr}; use std::{mem, ptr};
fn main() { fn main() {

View file

@ -1,8 +1,5 @@
//@ run-pass //@ run-pass
#![feature(const_ptr_sub_ptr)]
#![feature(ptr_sub_ptr)]
struct Struct { struct Struct {
field: (), field: (),
} }

View file

@ -1,5 +1,4 @@
//@ normalize-stderr: "\d+ bytes" -> "$$BYTES bytes" //@ normalize-stderr: "\d+ bytes" -> "$$BYTES bytes"
#![feature(const_ptr_sub_ptr)]
#![feature(core_intrinsics)] #![feature(core_intrinsics)]
use std::intrinsics::{ptr_offset_from, ptr_offset_from_unsigned}; use std::intrinsics::{ptr_offset_from, ptr_offset_from_unsigned};

View file

@ -1,5 +1,5 @@
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/offset_from_ub.rs:19:27 --> $DIR/offset_from_ub.rs:18:27
| |
LL | let offset = unsafe { ptr_offset_from(field_ptr, base_ptr) }; LL | let offset = unsafe { ptr_offset_from(field_ptr, base_ptr) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on two different pointers that are not both derived from the same allocation | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on two different pointers that are not both derived from the same allocation
@ -12,67 +12,67 @@ error[E0080]: evaluation of constant value failed
note: inside `std::ptr::const_ptr::<impl *const u8>::offset_from` note: inside `std::ptr::const_ptr::<impl *const u8>::offset_from`
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
note: inside `NOT_PTR` note: inside `NOT_PTR`
--> $DIR/offset_from_ub.rs:25:14 --> $DIR/offset_from_ub.rs:24:14
| |
LL | unsafe { (42 as *const u8).offset_from(&5u8) as usize } LL | unsafe { (42 as *const u8).offset_from(&5u8) as usize }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/offset_from_ub.rs:32:14 --> $DIR/offset_from_ub.rs:31:14
| |
LL | unsafe { ptr_offset_from(field_ptr, base_ptr as *const u16) } LL | unsafe { ptr_offset_from(field_ptr, base_ptr as *const u16) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ exact_div: 1_isize cannot be divided by 2_isize without remainder | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ exact_div: 1_isize cannot be divided by 2_isize without remainder
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/offset_from_ub.rs:39:14 --> $DIR/offset_from_ub.rs:38:14
| |
LL | unsafe { ptr_offset_from(ptr2, ptr1) } LL | unsafe { ptr_offset_from(ptr2, ptr1) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on two different pointers that are not both derived from the same allocation | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on two different pointers that are not both derived from the same allocation
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/offset_from_ub.rs:48:14 --> $DIR/offset_from_ub.rs:47:14
| |
LL | unsafe { ptr_offset_from(end_ptr, start_ptr) } LL | unsafe { ptr_offset_from(end_ptr, start_ptr) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on two different pointers where the memory range between them is not in-bounds of an allocation | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on two different pointers where the memory range between them is not in-bounds of an allocation
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/offset_from_ub.rs:57:14 --> $DIR/offset_from_ub.rs:56:14
| |
LL | unsafe { ptr_offset_from(start_ptr, end_ptr) } LL | unsafe { ptr_offset_from(start_ptr, end_ptr) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on two different pointers where the memory range between them is not in-bounds of an allocation | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on two different pointers where the memory range between them is not in-bounds of an allocation
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/offset_from_ub.rs:66:14 --> $DIR/offset_from_ub.rs:65:14
| |
LL | unsafe { ptr_offset_from_unsigned(field_ptr, base_ptr) } LL | unsafe { ptr_offset_from_unsigned(field_ptr, base_ptr) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called on two different pointers that are not both derived from the same allocation | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called on two different pointers that are not both derived from the same allocation
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/offset_from_ub.rs:73:14 --> $DIR/offset_from_ub.rs:72:14
| |
LL | unsafe { ptr_offset_from(ptr2, ptr1) } LL | unsafe { ptr_offset_from(ptr2, ptr1) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called when first pointer is too far ahead of second | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called when first pointer is too far ahead of second
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/offset_from_ub.rs:79:14 --> $DIR/offset_from_ub.rs:78:14
| |
LL | unsafe { ptr_offset_from(ptr1, ptr2) } LL | unsafe { ptr_offset_from(ptr1, ptr2) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called when first pointer is too far before second | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called when first pointer is too far before second
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/offset_from_ub.rs:87:14 --> $DIR/offset_from_ub.rs:86:14
| |
LL | unsafe { ptr_offset_from(ptr1, ptr2) } LL | unsafe { ptr_offset_from(ptr1, ptr2) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called when first pointer is too far before second | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called when first pointer is too far before second
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/offset_from_ub.rs:94:14 --> $DIR/offset_from_ub.rs:93:14
| |
LL | unsafe { ptr_offset_from_unsigned(p, p.add(2) ) } LL | unsafe { ptr_offset_from_unsigned(p, p.add(2) ) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer has smaller offset than second: 0 < 8 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer has smaller offset than second: 0 < 8
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/offset_from_ub.rs:101:14 --> $DIR/offset_from_ub.rs:100:14
| |
LL | unsafe { ptr_offset_from_unsigned(ptr2, ptr1) } LL | unsafe { ptr_offset_from_unsigned(ptr2, ptr1) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer is too far ahead of second | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer is too far ahead of second
@ -85,7 +85,7 @@ error[E0080]: evaluation of constant value failed
note: inside `std::ptr::const_ptr::<impl *const u8>::offset_from` note: inside `std::ptr::const_ptr::<impl *const u8>::offset_from`
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
note: inside `OFFSET_VERY_FAR1` note: inside `OFFSET_VERY_FAR1`
--> $DIR/offset_from_ub.rs:110:14 --> $DIR/offset_from_ub.rs:109:14
| |
LL | unsafe { ptr2.offset_from(ptr1) } LL | unsafe { ptr2.offset_from(ptr1) }
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^
@ -98,7 +98,7 @@ error[E0080]: evaluation of constant value failed
note: inside `std::ptr::const_ptr::<impl *const u8>::offset_from` note: inside `std::ptr::const_ptr::<impl *const u8>::offset_from`
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
note: inside `OFFSET_VERY_FAR2` note: inside `OFFSET_VERY_FAR2`
--> $DIR/offset_from_ub.rs:116:14 --> $DIR/offset_from_ub.rs:115:14
| |
LL | unsafe { ptr1.offset_from(ptr2.wrapping_offset(1)) } LL | unsafe { ptr1.offset_from(ptr2.wrapping_offset(1)) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^