Rollup merge of #130966 - RalfJung:ptr-metadata-const-stable, r=scottmcm

make ptr metadata functions callable from stable const fn

So far this was done with a bunch of `rustc_allow_const_fn_unstable`. But those should be the exception, not the norm. If we are confident we can expose the ptr metadata APIs *indirectly* in stable const fn, we should just mark them as `rustc_const_stable`. And we better be confident we can do that since it's already been done a while ago. ;)

In particular this marks two intrinsics as const-stable: `aggregate_raw_ptr`, `ptr_metadata`. This should be uncontroversial, they are trivial to implement in the interpreter.
Cc `@rust-lang/wg-const-eval` `@rust-lang/lang`
This commit is contained in:
Trevor Gross 2024-09-30 19:18:51 -04:00 committed by GitHub
commit a0637597b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 77 additions and 29 deletions

View File

@ -3165,7 +3165,7 @@ pub const fn type_id<T: ?Sized + 'static>() -> u128 {
/// change the possible layouts of pointers.
#[rustc_nounwind]
#[unstable(feature = "core_intrinsics", issue = "none")]
#[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
#[rustc_const_stable(feature = "ptr_metadata_const", since = "CURRENT_RUSTC_VERSION")]
#[rustc_intrinsic]
#[rustc_intrinsic_must_be_overridden]
pub const fn aggregate_raw_ptr<P: AggregateRawPtr<D, Metadata = M>, D, M>(_data: D, _meta: M) -> P {
@ -3190,7 +3190,7 @@ impl<P: ?Sized, T: ptr::Thin> AggregateRawPtr<*mut T> for *mut P {
/// This is used to implement functions like `ptr::metadata`.
#[rustc_nounwind]
#[unstable(feature = "core_intrinsics", issue = "none")]
#[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
#[rustc_const_stable(feature = "ptr_metadata_const", since = "CURRENT_RUSTC_VERSION")]
#[rustc_intrinsic]
#[rustc_intrinsic_must_be_overridden]
pub const fn ptr_metadata<P: ptr::Pointee<Metadata = M> + ?Sized, M>(_ptr: *const P) -> M {

View File

@ -92,7 +92,7 @@ pub const fn cast<U>(self) -> *const U {
/// }
/// ```
#[unstable(feature = "set_ptr_value", issue = "75091")]
#[rustc_const_unstable(feature = "set_ptr_value", issue = "75091")]
#[rustc_const_stable(feature = "ptr_metadata_const", since = "CURRENT_RUSTC_VERSION")]
#[must_use = "returns a new pointer rather than modifying its argument"]
#[inline]
pub const fn with_metadata_of<U>(self, meta: *const U) -> *const U
@ -412,7 +412,6 @@ pub const fn to_raw_parts(self) -> (*const (), <T as super::Pointee>::Metadata)
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_offset(self, count: isize) -> Self {
// SAFETY: the caller must uphold the safety contract for `offset`.
@ -495,7 +494,6 @@ pub const fn wrapping_offset(self, count: isize) -> *const T
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
pub const fn wrapping_byte_offset(self, count: isize) -> Self {
self.cast::<u8>().wrapping_offset(count).with_metadata_of(self)
}
@ -645,7 +643,6 @@ pub fn mask(self, mask: usize) -> *const T {
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_offset_from<U: ?Sized>(self, origin: *const U) -> isize {
// SAFETY: the caller must uphold the safety contract for `offset_from`.
@ -873,7 +870,6 @@ pub const fn guaranteed_ne(self, other: *const T) -> Option<bool>
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_add(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `add`.
@ -956,7 +952,6 @@ pub const fn guaranteed_ne(self, other: *const T) -> Option<bool>
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_sub(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `sub`.
@ -1039,7 +1034,6 @@ pub const fn wrapping_add(self, count: usize) -> Self
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
pub const fn wrapping_byte_add(self, count: usize) -> Self {
self.cast::<u8>().wrapping_add(count).with_metadata_of(self)
}
@ -1120,7 +1114,6 @@ pub const fn wrapping_sub(self, count: usize) -> Self
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
pub const fn wrapping_byte_sub(self, count: usize) -> Self {
self.cast::<u8>().wrapping_sub(count).with_metadata_of(self)
}
@ -1554,7 +1547,6 @@ impl<T> *const [T] {
#[inline]
#[stable(feature = "slice_ptr_len", since = "1.79.0")]
#[rustc_const_stable(feature = "const_slice_ptr_len", since = "1.79.0")]
#[rustc_allow_const_fn_unstable(ptr_metadata)]
pub const fn len(self) -> usize {
metadata(self)
}

View File

@ -92,7 +92,7 @@ pub trait Pointee {
///
/// assert_eq!(std::ptr::metadata("foo"), 3_usize);
/// ```
#[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
#[rustc_const_stable(feature = "ptr_metadata_const", since = "CURRENT_RUSTC_VERSION")]
#[inline]
pub const fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {
ptr_metadata(ptr)
@ -106,7 +106,7 @@ pub const fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {
///
/// [`slice::from_raw_parts`]: crate::slice::from_raw_parts
#[unstable(feature = "ptr_metadata", issue = "81513")]
#[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
#[rustc_const_stable(feature = "ptr_metadata_const", since = "CURRENT_RUSTC_VERSION")]
#[inline]
pub const fn from_raw_parts<T: ?Sized>(
data_pointer: *const impl Thin,
@ -120,7 +120,7 @@ pub const fn from_raw_parts<T: ?Sized>(
///
/// See the documentation of [`from_raw_parts`] for more details.
#[unstable(feature = "ptr_metadata", issue = "81513")]
#[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
#[rustc_const_stable(feature = "ptr_metadata_const", since = "CURRENT_RUSTC_VERSION")]
#[inline]
pub const fn from_raw_parts_mut<T: ?Sized>(
data_pointer: *mut impl Thin,

View File

@ -599,7 +599,6 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_promotable]
#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
#[rustc_allow_const_fn_unstable(ptr_metadata)]
#[rustc_diagnostic_item = "ptr_null"]
pub const fn null<T: ?Sized + Thin>() -> *const T {
from_raw_parts(without_provenance::<()>(0), ())
@ -625,7 +624,6 @@ pub const fn null<T: ?Sized + Thin>() -> *const T {
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_promotable]
#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
#[rustc_allow_const_fn_unstable(ptr_metadata)]
#[rustc_diagnostic_item = "ptr_null_mut"]
pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
from_raw_parts_mut(without_provenance_mut::<()>(0), ())
@ -949,7 +947,6 @@ pub const fn from_mut<T: ?Sized>(r: &mut T) -> *mut T {
#[inline]
#[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
#[rustc_const_stable(feature = "const_slice_from_raw_parts", since = "1.64.0")]
#[rustc_allow_const_fn_unstable(ptr_metadata)]
#[rustc_diagnostic_item = "ptr_slice_from_raw_parts"]
pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
from_raw_parts(data, len)

View File

@ -74,7 +74,7 @@ pub const fn cast<U>(self) -> *mut U {
/// }
/// ```
#[unstable(feature = "set_ptr_value", issue = "75091")]
#[rustc_const_unstable(feature = "set_ptr_value", issue = "75091")]
#[rustc_const_stable(feature = "ptr_metadata_const", since = "CURRENT_RUSTC_VERSION")]
#[must_use = "returns a new pointer rather than modifying its argument"]
#[inline]
pub const fn with_metadata_of<U>(self, meta: *const U) -> *mut U
@ -412,7 +412,6 @@ pub const fn to_raw_parts(self) -> (*mut (), <T as super::Pointee>::Metadata) {
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_offset(self, count: isize) -> Self {
// SAFETY: the caller must uphold the safety contract for `offset`.
@ -492,7 +491,6 @@ pub const fn wrapping_offset(self, count: isize) -> *mut T
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
pub const fn wrapping_byte_offset(self, count: isize) -> Self {
self.cast::<u8>().wrapping_offset(count).with_metadata_of(self)
}
@ -808,7 +806,6 @@ pub const fn guaranteed_ne(self, other: *mut T) -> Option<bool>
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_offset_from<U: ?Sized>(self, origin: *const U) -> isize {
// SAFETY: the caller must uphold the safety contract for `offset_from`.
@ -954,7 +951,6 @@ pub const fn guaranteed_ne(self, other: *mut T) -> Option<bool>
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_add(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `add`.
@ -1037,7 +1033,6 @@ pub const fn guaranteed_ne(self, other: *mut T) -> Option<bool>
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_sub(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `sub`.
@ -1118,7 +1113,6 @@ pub const fn wrapping_add(self, count: usize) -> Self
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
pub const fn wrapping_byte_add(self, count: usize) -> Self {
self.cast::<u8>().wrapping_add(count).with_metadata_of(self)
}
@ -1197,7 +1191,6 @@ pub const fn wrapping_sub(self, count: usize) -> Self
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[rustc_allow_const_fn_unstable(set_ptr_value)]
pub const fn wrapping_byte_sub(self, count: usize) -> Self {
self.cast::<u8>().wrapping_sub(count).with_metadata_of(self)
}
@ -1804,7 +1797,6 @@ impl<T> *mut [T] {
#[inline(always)]
#[stable(feature = "slice_ptr_len", since = "1.79.0")]
#[rustc_const_stable(feature = "const_slice_ptr_len", since = "1.79.0")]
#[rustc_allow_const_fn_unstable(ptr_metadata)]
pub const fn len(self) -> usize {
metadata(self)
}

View File

@ -567,7 +567,6 @@ pub const fn cast<U>(self) -> NonNull<U> {
#[must_use]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[rustc_allow_const_fn_unstable(set_ptr_value)]
#[stable(feature = "non_null_convenience", since = "1.80.0")]
#[rustc_const_stable(feature = "non_null_convenience", since = "1.80.0")]
pub const unsafe fn byte_add(self, count: usize) -> Self {
@ -651,7 +650,6 @@ pub const fn cast<U>(self) -> NonNull<U> {
#[must_use]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[rustc_allow_const_fn_unstable(set_ptr_value)]
#[stable(feature = "non_null_convenience", since = "1.80.0")]
#[rustc_const_stable(feature = "non_null_convenience", since = "1.80.0")]
pub const unsafe fn byte_sub(self, count: usize) -> Self {

View File

@ -111,7 +111,6 @@ impl<T> [T] {
#[lang = "slice_len_fn"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_slice_len", since = "1.39.0")]
#[rustc_allow_const_fn_unstable(ptr_metadata)]
#[inline]
#[must_use]
pub const fn len(&self) -> usize {

View File

@ -0,0 +1,13 @@
#![feature(staged_api, rustc_attrs, intrinsics)]
#![stable(since="1.0.0", feature = "stable")]
extern "rust-intrinsic" {
#[unstable(feature = "unstable", issue = "42")]
#[rustc_const_stable(feature = "stable", since = "1.0.0")]
#[rustc_nounwind]
pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
}
#[unstable(feature = "unstable", issue = "42")]
#[rustc_const_stable(feature = "stable", since = "1.0.0")]
pub const fn some_unstable_fn() {}

View File

@ -0,0 +1,14 @@
//@ aux-build:unstable_but_const_stable.rs
extern crate unstable_but_const_stable;
use unstable_but_const_stable::*;
fn main() {
some_unstable_fn(); //~ERROR use of unstable library feature
unsafe { write_bytes(4 as *mut u8, 0, 0) }; //~ERROR use of unstable library feature
}
const fn const_main() {
some_unstable_fn(); //~ERROR use of unstable library feature
unsafe { write_bytes(4 as *mut u8, 0, 0) }; //~ERROR use of unstable library feature
}

View File

@ -0,0 +1,43 @@
error[E0658]: use of unstable library feature 'unstable'
--> $DIR/unstable-const-stable.rs:7:5
|
LL | some_unstable_fn();
| ^^^^^^^^^^^^^^^^
|
= note: see issue #42 <https://github.com/rust-lang/rust/issues/42> for more information
= help: add `#![feature(unstable)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: use of unstable library feature 'unstable'
--> $DIR/unstable-const-stable.rs:8:14
|
LL | unsafe { write_bytes(4 as *mut u8, 0, 0) };
| ^^^^^^^^^^^
|
= note: see issue #42 <https://github.com/rust-lang/rust/issues/42> for more information
= help: add `#![feature(unstable)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: use of unstable library feature 'unstable'
--> $DIR/unstable-const-stable.rs:12:5
|
LL | some_unstable_fn();
| ^^^^^^^^^^^^^^^^
|
= note: see issue #42 <https://github.com/rust-lang/rust/issues/42> for more information
= help: add `#![feature(unstable)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: use of unstable library feature 'unstable'
--> $DIR/unstable-const-stable.rs:13:14
|
LL | unsafe { write_bytes(4 as *mut u8, 0, 0) };
| ^^^^^^^^^^^
|
= note: see issue #42 <https://github.com/rust-lang/rust/issues/42> for more information
= help: add `#![feature(unstable)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0658`.