diff --git a/library/alloc/src/borrow.rs b/library/alloc/src/borrow.rs index 07f51b7614ff8..df87d9a3abbee 100644 --- a/library/alloc/src/borrow.rs +++ b/library/alloc/src/borrow.rs @@ -17,9 +17,11 @@ use crate::fmt; use crate::string::String; #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, B: ?Sized> Borrow for Cow<'a, B> +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a, B: ?Sized> const Borrow for Cow<'a, B> where B: ToOwned, + B::Owned: ~const Borrow, { fn borrow(&self) -> &B { &**self @@ -326,9 +328,10 @@ impl Cow<'_, B> { } #[stable(feature = "rust1", since = "1.0.0")] -impl Deref for Cow<'_, B> +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Deref for Cow<'_, B> where - B::Owned: Borrow, + B::Owned: ~const Borrow, { type Target = B; @@ -439,7 +442,11 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for Cow<'_, T> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for Cow<'_, T> +where + T::Owned: ~const Borrow, +{ fn as_ref(&self) -> &T { self } diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 3db37f1d16f3d..f6fd9dc0df2aa 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -606,7 +606,8 @@ impl Box { /// /// This conversion does not allocate on the heap and happens in place. #[unstable(feature = "box_into_boxed_slice", issue = "71582")] - pub fn into_boxed_slice(boxed: Self) -> Box<[T], A> { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_boxed_slice(boxed: Self) -> Box<[T], A> { let (raw, alloc) = Box::into_raw_with_allocator(boxed); unsafe { Box::from_raw_in(raw as *mut [T; 1], alloc) } } @@ -1268,7 +1269,8 @@ impl Box { /// [memory layout]: self#memory-layout #[unstable(feature = "allocator_api", issue = "32838")] #[inline] - pub unsafe fn from_raw_in(raw: *mut T, alloc: A) -> Self { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + pub const unsafe fn from_raw_in(raw: *mut T, alloc: A) -> Self { Box(unsafe { Unique::new_unchecked(raw) }, alloc) } @@ -1375,7 +1377,8 @@ impl Box { #[must_use = "losing the pointer will leak memory"] #[unstable(feature = "allocator_api", issue = "32838")] #[inline] - pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + pub const fn into_raw_with_allocator(b: Self) -> (*mut T, A) { let mut b = mem::ManuallyDrop::new(b); // We carefully get the raw pointer out in a way that Miri's aliasing model understands what // is happening: using the primitive "deref" of `Box`. In case `A` is *not* `Global`, we @@ -1436,7 +1439,8 @@ impl Box { #[unstable(feature = "allocator_api", issue = "32838")] // #[unstable(feature = "box_vec_non_null", reason = "new API", issue = "130364")] #[inline] - pub fn into_non_null_with_allocator(b: Self) -> (NonNull, A) { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + pub const fn into_non_null_with_allocator(b: Self) -> (NonNull, A) { let (ptr, alloc) = Box::into_raw_with_allocator(b); // SAFETY: `Box` is guaranteed to be non-null. unsafe { (NonNull::new_unchecked(ptr), alloc) } @@ -1449,7 +1453,12 @@ impl Box { )] #[inline] #[doc(hidden)] - pub fn into_unique(b: Self) -> (Unique, A) { + #[rustc_const_unstable( + feature = "ptr_internals", + issue = "none", + reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead" + )] + pub const fn into_unique(b: Self) -> (Unique, A) { let (ptr, alloc) = Box::into_raw_with_allocator(b); unsafe { (Unique::from(&mut *ptr), alloc) } } @@ -1641,7 +1650,8 @@ impl Box { /// let bar = Pin::from(foo); /// ``` #[stable(feature = "box_into_pin", since = "1.63.0")] - pub fn into_pin(boxed: Self) -> Pin + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_pin(boxed: Self) -> Pin where A: 'static, { @@ -1942,7 +1952,8 @@ impl fmt::Pointer for Box { } #[stable(feature = "rust1", since = "1.0.0")] -impl Deref for Box { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Deref for Box { type Target = T; fn deref(&self) -> &T { @@ -1951,7 +1962,8 @@ impl Deref for Box { } #[stable(feature = "rust1", since = "1.0.0")] -impl DerefMut for Box { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const DerefMut for Box { fn deref_mut(&mut self) -> &mut T { &mut **self } @@ -2028,28 +2040,32 @@ unsafe impl PinCoerceUnsized for Box {} impl, U: ?Sized> DispatchFromDyn> for Box {} #[stable(feature = "box_borrow", since = "1.1.0")] -impl Borrow for Box { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow for Box { fn borrow(&self) -> &T { &**self } } #[stable(feature = "box_borrow", since = "1.1.0")] -impl BorrowMut for Box { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const BorrowMut for Box { fn borrow_mut(&mut self) -> &mut T { &mut **self } } #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")] -impl AsRef for Box { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for Box { fn as_ref(&self) -> &T { &**self } } #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")] -impl AsMut for Box { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsMut for Box { fn as_mut(&mut self) -> &mut T { &mut **self } diff --git a/library/alloc/src/boxed/convert.rs b/library/alloc/src/boxed/convert.rs index 8062658020239..d7efd67394159 100644 --- a/library/alloc/src/boxed/convert.rs +++ b/library/alloc/src/boxed/convert.rs @@ -40,7 +40,8 @@ impl From for Box { } #[stable(feature = "pin", since = "1.33.0")] -impl From> for Pin> +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Pin> where A: 'static, { @@ -228,7 +229,8 @@ impl From> for Box { } #[stable(feature = "boxed_str_conv", since = "1.19.0")] -impl From> for Box<[u8], A> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Box<[u8], A> { /// Converts a `Box` into a `Box<[u8]>` /// /// This conversion does not allocate on the heap and happens in place. @@ -247,8 +249,7 @@ impl From> for Box<[u8], A> { /// ``` #[inline] fn from(s: Box) -> Self { - let (raw, alloc) = Box::into_raw_with_allocator(s); - unsafe { Box::from_raw_in(raw as *mut [u8], alloc) } + s.into_boxed_bytes() } } @@ -275,10 +276,11 @@ impl From<[T; N]> for Box<[T]> { /// # Safety /// /// `boxed_slice.len()` must be exactly `N`. -unsafe fn boxed_slice_as_array_unchecked( +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +const unsafe fn boxed_slice_as_array_unchecked( boxed_slice: Box<[T], A>, ) -> Box<[T; N], A> { - debug_assert_eq!(boxed_slice.len(), N); + debug_assert!(boxed_slice.len() == N); let (ptr, alloc) = Box::into_raw_with_allocator(boxed_slice); // SAFETY: Pointer and allocator came from an existing box, @@ -287,6 +289,7 @@ unsafe fn boxed_slice_as_array_unchecked( } #[stable(feature = "boxed_slice_try_from", since = "1.43.0")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl TryFrom> for Box<[T; N]> { type Error = Box<[T]>; diff --git a/library/alloc/src/boxed/thin.rs b/library/alloc/src/boxed/thin.rs index 1cce36606d2c0..bcf5a18497063 100644 --- a/library/alloc/src/boxed/thin.rs +++ b/library/alloc/src/boxed/thin.rs @@ -4,6 +4,7 @@ use core::error::Error; use core::fmt::{self, Debug, Display, Formatter}; +use core::intrinsics::const_eval_select; #[cfg(not(no_global_oom_handling))] use core::intrinsics::{const_allocate, const_make_global}; use core::marker::PhantomData; @@ -138,7 +139,8 @@ impl Display for ThinBox { } #[unstable(feature = "thin_box", issue = "92791")] -impl Deref for ThinBox { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Deref for ThinBox { type Target = T; fn deref(&self) -> &T { @@ -150,6 +152,7 @@ impl Deref for ThinBox { } #[unstable(feature = "thin_box", issue = "92791")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl DerefMut for ThinBox { fn deref_mut(&mut self) -> &mut T { let value = self.data(); @@ -172,17 +175,20 @@ impl Drop for ThinBox { #[unstable(feature = "thin_box", issue = "92791")] impl ThinBox { - fn meta(&self) -> ::Metadata { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const fn meta(&self) -> ::Metadata { // Safety: // - NonNull and valid. unsafe { *self.with_header().header() } } - fn data(&self) -> *mut u8 { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const fn data(&self) -> *mut u8 { self.with_header().value() } - fn with_header(&self) -> &WithHeader<::Metadata> { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const fn with_header(&self) -> &WithHeader<::Metadata> { // SAFETY: both types are transparent to `NonNull` unsafe { &*((&raw const self.ptr) as *const WithHeader<_>) } } @@ -398,7 +404,8 @@ impl WithHeader { } } - fn header(&self) -> *mut H { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const fn header(&self) -> *mut H { // Safety: // - At least `size_of::()` bytes are allocated ahead of the pointer. // - We know that H will be aligned because the middle pointer is aligned to the greater @@ -407,11 +414,18 @@ impl WithHeader { // will always result in an aligned header pointer, it just may not point to the // beginning of the allocation. let hp = unsafe { self.0.as_ptr().sub(Self::header_size()) as *mut H }; - debug_assert!(hp.is_aligned()); + + const fn ignore_alignment_const(_hp: *mut H) {} + fn check_alignment_rt(hp: *mut H) { + debug_assert!(hp.is_aligned()); + } + const_eval_select((hp,), ignore_alignment_const, check_alignment_rt); + hp } - fn value(&self) -> *mut u8 { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const fn value(&self) -> *mut u8 { self.0.as_ptr() } diff --git a/library/alloc/src/bstr.rs b/library/alloc/src/bstr.rs index 338c7ac7f8876..47788de96c21b 100644 --- a/library/alloc/src/bstr.rs +++ b/library/alloc/src/bstr.rs @@ -15,6 +15,7 @@ use core::ops::{ use core::str::FromStr; use core::{fmt, hash}; +use crate::alloc::Allocator; use crate::borrow::{Cow, ToOwned}; use crate::boxed::Box; #[cfg(not(no_rc))] @@ -48,23 +49,27 @@ pub struct ByteString(pub Vec); impl ByteString { #[inline] - pub(crate) fn as_bytes(&self) -> &[u8] { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub(crate) const fn as_bytes(&self) -> &[u8] { &self.0 } #[inline] - pub(crate) fn as_bytestr(&self) -> &ByteStr { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub(crate) const fn as_bytestr(&self) -> &ByteStr { ByteStr::new(&self.0) } #[inline] - pub(crate) fn as_mut_bytestr(&mut self) -> &mut ByteStr { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub(crate) const fn as_mut_bytestr(&mut self) -> &mut ByteStr { ByteStr::from_bytes_mut(&mut self.0) } } #[unstable(feature = "bstr", issue = "134915")] -impl Deref for ByteString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Deref for ByteString { type Target = Vec; #[inline] @@ -74,7 +79,8 @@ impl Deref for ByteString { } #[unstable(feature = "bstr", issue = "134915")] -impl DerefMut for ByteString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const DerefMut for ByteString { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 @@ -101,7 +107,8 @@ impl fmt::Display for ByteString { } #[unstable(feature = "bstr", issue = "134915")] -impl AsRef<[u8]> for ByteString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef<[u8]> for ByteString { #[inline] fn as_ref(&self) -> &[u8] { &self.0 @@ -109,7 +116,8 @@ impl AsRef<[u8]> for ByteString { } #[unstable(feature = "bstr", issue = "134915")] -impl AsRef for ByteString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for ByteString { #[inline] fn as_ref(&self) -> &ByteStr { self.as_bytestr() @@ -117,7 +125,8 @@ impl AsRef for ByteString { } #[unstable(feature = "bstr", issue = "134915")] -impl AsMut<[u8]> for ByteString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsMut<[u8]> for ByteString { #[inline] fn as_mut(&mut self) -> &mut [u8] { &mut self.0 @@ -125,7 +134,8 @@ impl AsMut<[u8]> for ByteString { } #[unstable(feature = "bstr", issue = "134915")] -impl AsMut for ByteString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsMut for ByteString { #[inline] fn as_mut(&mut self) -> &mut ByteStr { self.as_mut_bytestr() @@ -133,7 +143,8 @@ impl AsMut for ByteString { } #[unstable(feature = "bstr", issue = "134915")] -impl Borrow<[u8]> for ByteString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow<[u8]> for ByteString { #[inline] fn borrow(&self) -> &[u8] { &self.0 @@ -141,7 +152,8 @@ impl Borrow<[u8]> for ByteString { } #[unstable(feature = "bstr", issue = "134915")] -impl Borrow for ByteString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow for ByteString { #[inline] fn borrow(&self) -> &ByteStr { self.as_bytestr() @@ -152,7 +164,8 @@ impl Borrow for ByteString { // `impl Borrow for String` omitted to avoid inference failures #[unstable(feature = "bstr", issue = "134915")] -impl BorrowMut<[u8]> for ByteString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const BorrowMut<[u8]> for ByteString { #[inline] fn borrow_mut(&mut self) -> &mut [u8] { &mut self.0 @@ -160,7 +173,8 @@ impl BorrowMut<[u8]> for ByteString { } #[unstable(feature = "bstr", issue = "134915")] -impl BorrowMut for ByteString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const BorrowMut for ByteString { #[inline] fn borrow_mut(&mut self) -> &mut ByteStr { self.as_mut_bytestr() @@ -211,7 +225,8 @@ impl Default for ByteString { // } #[unstable(feature = "bstr", issue = "134915")] -impl From for Vec { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for Vec { #[inline] fn from(s: ByteString) -> Self { s.0 @@ -245,7 +260,8 @@ impl<'a> From<&'a ByteStr> for ByteString { } #[unstable(feature = "bstr", issue = "134915")] -impl<'a> From for Cow<'a, ByteStr> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From for Cow<'a, ByteStr> { #[inline] fn from(s: ByteString) -> Self { Cow::Owned(s) @@ -253,7 +269,8 @@ impl<'a> From for Cow<'a, ByteStr> { } #[unstable(feature = "bstr", issue = "134915")] -impl<'a> From<&'a ByteString> for Cow<'a, ByteStr> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From<&'a ByteString> for Cow<'a, ByteStr> { #[inline] fn from(s: &'a ByteString) -> Self { Cow::Borrowed(s.as_bytestr()) @@ -568,7 +585,8 @@ impl ToOwned for ByteStr { } #[unstable(feature = "bstr", issue = "134915")] -impl TryFrom for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const TryFrom for String { type Error = crate::string::FromUtf8Error; #[inline] @@ -578,7 +596,8 @@ impl TryFrom for String { } #[unstable(feature = "bstr", issue = "134915")] -impl<'a> TryFrom<&'a ByteString> for &'a str { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const TryFrom<&'a ByteString> for &'a str { type Error = crate::str::Utf8Error; #[inline] @@ -598,7 +617,8 @@ impl Clone for Box { } #[unstable(feature = "bstr", issue = "134915")] -impl<'a> From<&'a ByteStr> for Cow<'a, ByteStr> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From<&'a ByteStr> for Cow<'a, ByteStr> { #[inline] fn from(s: &'a ByteStr) -> Self { Cow::Borrowed(s) @@ -606,60 +626,74 @@ impl<'a> From<&'a ByteStr> for Cow<'a, ByteStr> { } #[unstable(feature = "bstr", issue = "134915")] -impl From> for Box { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Box { #[inline] - fn from(s: Box<[u8]>) -> Box { + fn from(s: Box<[u8], A>) -> Box { + let (raw, alloc) = Box::into_raw_with_allocator(s); + // SAFETY: `ByteStr` is a transparent wrapper around `[u8]`. - unsafe { Box::from_raw(Box::into_raw(s) as _) } + unsafe { Box::from_raw_in(raw as _, alloc) } } } #[unstable(feature = "bstr", issue = "134915")] -impl From> for Box<[u8]> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Box<[u8], A> { #[inline] - fn from(s: Box) -> Box<[u8]> { + fn from(s: Box) -> Box<[u8], A> { + let (raw, alloc) = Box::into_raw_with_allocator(s); + // SAFETY: `ByteStr` is a transparent wrapper around `[u8]`. - unsafe { Box::from_raw(Box::into_raw(s) as _) } + unsafe { Box::from_raw_in(raw as _, alloc) } } } #[unstable(feature = "bstr", issue = "134915")] #[cfg(not(no_rc))] -impl From> for Rc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Rc { #[inline] - fn from(s: Rc<[u8]>) -> Rc { + fn from(s: Rc<[u8], A>) -> Rc { + let (raw, alloc) = Rc::into_raw_with_allocator(s); // SAFETY: `ByteStr` is a transparent wrapper around `[u8]`. - unsafe { Rc::from_raw(Rc::into_raw(s) as _) } + unsafe { Rc::from_raw_in(raw as _, alloc) } } } #[unstable(feature = "bstr", issue = "134915")] #[cfg(not(no_rc))] -impl From> for Rc<[u8]> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Rc<[u8], A> { #[inline] - fn from(s: Rc) -> Rc<[u8]> { + fn from(s: Rc) -> Rc<[u8], A> { + let (raw, alloc) = Rc::into_raw_with_allocator(s); // SAFETY: `ByteStr` is a transparent wrapper around `[u8]`. - unsafe { Rc::from_raw(Rc::into_raw(s) as _) } + unsafe { Rc::from_raw_in(raw as _, alloc) } } } #[unstable(feature = "bstr", issue = "134915")] #[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))] -impl From> for Arc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Arc { #[inline] - fn from(s: Arc<[u8]>) -> Arc { + fn from(s: Arc<[u8], A>) -> Arc { + let (raw, alloc) = Arc::into_raw_with_allocator(s); // SAFETY: `ByteStr` is a transparent wrapper around `[u8]`. - unsafe { Arc::from_raw(Arc::into_raw(s) as _) } + unsafe { Arc::from_raw_in(raw as _, alloc) } } } #[unstable(feature = "bstr", issue = "134915")] #[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))] -impl From> for Arc<[u8]> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Arc<[u8], A> { #[inline] - fn from(s: Arc) -> Arc<[u8]> { + fn from(s: Arc) -> Arc<[u8], A> { + let (raw, alloc) = Arc::into_raw_with_allocator(s); // SAFETY: `ByteStr` is a transparent wrapper around `[u8]`. - unsafe { Arc::from_raw(Arc::into_raw(s) as _) } + unsafe { Arc::from_raw_in(raw as _, alloc) } } } diff --git a/library/alloc/src/collections/mod.rs b/library/alloc/src/collections/mod.rs index fac4d1a65abcb..212d7c8465b6e 100644 --- a/library/alloc/src/collections/mod.rs +++ b/library/alloc/src/collections/mod.rs @@ -128,8 +128,9 @@ pub use realalloc::collections::TryReserveErrorKind; reason = "Uncertain how much info should be exposed", issue = "48043" )] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] #[cfg(not(test))] -impl From for TryReserveError { +impl const From for TryReserveError { #[inline] fn from(kind: TryReserveErrorKind) -> Self { Self { kind } @@ -137,8 +138,9 @@ impl From for TryReserveError { } #[unstable(feature = "try_reserve_kind", reason = "new API", issue = "48043")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] #[cfg(not(test))] -impl From for TryReserveErrorKind { +impl const From for TryReserveErrorKind { /// Always evaluates to [`TryReserveErrorKind::CapacityOverflow`]. #[inline] fn from(_: LayoutError) -> Self { diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index 93bdad7538007..1142363a55cdf 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -178,8 +178,9 @@ impl FromVecWithNulError { /// ``` #[must_use] #[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")] - pub fn as_bytes(&self) -> &[u8] { - &self.bytes[..] + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn as_bytes(&self) -> &[u8] { + &*self.bytes } /// Returns the bytes that were attempted to convert to a [`CString`]. @@ -204,7 +205,8 @@ impl FromVecWithNulError { /// ``` #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")] - pub fn into_bytes(self) -> Vec { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_bytes(self) -> Vec { self.bytes } } @@ -470,11 +472,13 @@ impl CString { /// assert_eq!(err.utf8_error().valid_up_to(), 1); /// ``` #[stable(feature = "cstring_into", since = "1.7.0")] - pub fn into_string(self) -> Result { - String::from_utf8(self.into_bytes()).map_err(|e| IntoStringError { - error: e.utf8_error(), - inner: unsafe { Self::_from_vec_unchecked(e.into_bytes()) }, - }) + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_string(self) -> Result { + match str::from_utf8(self.as_bytes_with_nul()) { + // SAFETY: We verified that the string is valid UTF-8. + Ok(_) => Ok(unsafe { String::from_utf8_unchecked(self.into_bytes()) }), + Err(error) => Err(IntoStringError { error, inner: self }), + } } /// Consumes the `CString` and returns the underlying byte buffer. @@ -494,10 +498,15 @@ impl CString { /// ``` #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "cstring_into", since = "1.7.0")] - pub fn into_bytes(self) -> Vec { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_bytes(self) -> Vec { let mut vec = self.into_inner().into_vec(); - let _nul = vec.pop(); - debug_assert_eq!(_nul, Some(0u8)); + + // SAFETY: CString invariant. + unsafe { + vec.pop_zero_byte(); + } + vec } @@ -515,7 +524,8 @@ impl CString { /// ``` #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "cstring_into", since = "1.7.0")] - pub fn into_bytes_with_nul(self) -> Vec { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_bytes_with_nul(self) -> Vec { self.into_inner().into_vec() } @@ -538,7 +548,8 @@ impl CString { #[inline] #[must_use] #[stable(feature = "rust1", since = "1.0.0")] - pub fn as_bytes(&self) -> &[u8] { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn as_bytes(&self) -> &[u8] { // SAFETY: CString has a length at least 1 unsafe { self.inner.get_unchecked(..self.inner.len() - 1) } } @@ -558,7 +569,8 @@ impl CString { #[inline] #[must_use] #[stable(feature = "rust1", since = "1.0.0")] - pub fn as_bytes_with_nul(&self) -> &[u8] { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn as_bytes_with_nul(&self) -> &[u8] { &self.inner } @@ -578,7 +590,8 @@ impl CString { #[must_use] #[stable(feature = "as_c_str", since = "1.20.0")] #[rustc_diagnostic_item = "cstring_as_c_str"] - pub fn as_c_str(&self) -> &CStr { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn as_c_str(&self) -> &CStr { unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) } } @@ -599,7 +612,8 @@ impl CString { /// Bypass "move out of struct which implements [`Drop`] trait" restriction. #[inline] - fn into_inner(self) -> Box<[u8]> { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const fn into_inner(self) -> Box<[u8]> { // Rationale: `mem::forget(self)` invalidates the previous call to `ptr::read(&self.inner)` // so we use `ManuallyDrop` to ensure `self` is not dropped. // Then we can return the box directly without invalidating it. @@ -705,7 +719,8 @@ impl Drop for CString { } #[stable(feature = "rust1", since = "1.0.0")] -impl ops::Deref for CString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const ops::Deref for CString { type Target = CStr; #[inline] @@ -724,7 +739,8 @@ impl fmt::Debug for CString { } #[stable(feature = "cstring_into", since = "1.7.0")] -impl From for Vec { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for Vec { /// Converts a [`CString`] into a [Vec]<[u8]>. /// /// The conversion consumes the [`CString`], and removes the terminating NUL byte. @@ -744,7 +760,8 @@ impl Default for CString { } #[stable(feature = "cstr_borrow", since = "1.3.0")] -impl Borrow for CString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow for CString { #[inline] fn borrow(&self) -> &CStr { self @@ -794,12 +811,13 @@ impl From> for Box { } #[stable(feature = "c_string_from_box", since = "1.18.0")] -impl From> for CString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for CString { /// Converts a [Box]<[CStr]> into a [`CString`] without copying or allocating. #[inline] fn from(s: Box) -> CString { - let raw = Box::into_raw(s) as *mut [u8]; - CString { inner: unsafe { Box::from_raw(raw) } } + let (raw, alloc) = Box::into_raw_with_allocator(s); + CString { inner: unsafe { Box::from_raw_in(raw as *mut [u8], alloc) } } } } @@ -839,7 +857,8 @@ impl FromStr for CString { } #[stable(feature = "c_string_from_str", since = "1.85.0")] -impl TryFrom for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const TryFrom for String { type Error = IntoStringError; /// Converts a [`CString`] into a [`String`] if it contains valid UTF-8 data. @@ -869,7 +888,8 @@ impl From for Box { } #[stable(feature = "cow_from_cstr", since = "1.28.0")] -impl<'a> From for Cow<'a, CStr> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From for Cow<'a, CStr> { /// Converts a [`CString`] into an owned [`Cow`] without copying or allocating. #[inline] fn from(s: CString) -> Cow<'a, CStr> { @@ -878,7 +898,8 @@ impl<'a> From for Cow<'a, CStr> { } #[stable(feature = "cow_from_cstr", since = "1.28.0")] -impl<'a> From<&'a CStr> for Cow<'a, CStr> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From<&'a CStr> for Cow<'a, CStr> { /// Converts a [`CStr`] into a borrowed [`Cow`] without copying or allocating. #[inline] fn from(s: &'a CStr) -> Cow<'a, CStr> { @@ -887,7 +908,8 @@ impl<'a> From<&'a CStr> for Cow<'a, CStr> { } #[stable(feature = "cow_from_cstr", since = "1.28.0")] -impl<'a> From<&'a CString> for Cow<'a, CStr> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From<&'a CString> for Cow<'a, CStr> { /// Converts a `&`[`CString`] into a borrowed [`Cow`] without copying or allocating. #[inline] fn from(s: &'a CString) -> Cow<'a, CStr> { @@ -1049,14 +1071,16 @@ impl IntoStringError { /// error. #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "cstring_into", since = "1.7.0")] - pub fn into_cstring(self) -> CString { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_cstring(self) -> CString { self.inner } /// Access the underlying UTF-8 error that was the cause of this error. #[must_use] #[stable(feature = "cstring_into", since = "1.7.0")] - pub fn utf8_error(&self) -> Utf8Error { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn utf8_error(&self) -> Utf8Error { self.error } } @@ -1150,7 +1174,8 @@ impl ops::Index for CString { } #[stable(feature = "cstring_asref", since = "1.7.0")] -impl AsRef for CString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for CString { #[inline] fn as_ref(&self) -> &CStr { self diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 6b6e4df4cba72..01a98627a8fcc 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -107,9 +107,11 @@ #![feature(char_max_len)] #![feature(clone_to_uninit)] #![feature(coerce_unsized)] +#![feature(const_convert)] #![feature(const_default)] #![feature(const_eval_select)] #![feature(const_heap)] +#![feature(const_index)] #![feature(const_trait_impl)] #![feature(core_intrinsics)] #![feature(deprecated_suggestion)] diff --git a/library/alloc/src/raw_vec/mod.rs b/library/alloc/src/raw_vec/mod.rs index 40716755aad34..a2be790979916 100644 --- a/library/alloc/src/raw_vec/mod.rs +++ b/library/alloc/src/raw_vec/mod.rs @@ -44,7 +44,8 @@ const ZERO_CAP: Cap = unsafe { Cap::new_unchecked(0) }; /// `Cap(cap)`, except if `T` is a ZST then `Cap::ZERO`. /// /// # Safety: cap must be <= `isize::MAX`. -unsafe fn new_cap(cap: usize) -> Cap { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +const unsafe fn new_cap(cap: usize) -> Cap { if T::IS_ZST { ZERO_CAP } else { unsafe { Cap::new_unchecked(cap) } } } @@ -251,7 +252,8 @@ impl RawVec { /// If the `ptr` and `capacity` come from a `RawVec` created via `alloc`, then this is /// guaranteed. #[inline] - pub(crate) unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub(crate) const unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self { // SAFETY: Precondition passed to the caller unsafe { let ptr = ptr.cast(); @@ -493,7 +495,8 @@ impl RawVecInner { } #[inline] - unsafe fn from_raw_parts_in(ptr: *mut u8, cap: Cap, alloc: A) -> Self { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const unsafe fn from_raw_parts_in(ptr: *mut u8, cap: Cap, alloc: A) -> Self { Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap, alloc } } diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 5018ff4ad71f3..1a4c2d6aa7f71 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -352,25 +352,28 @@ impl Rc { impl Rc { #[inline(always)] - fn inner(&self) -> &RcInner { + const fn inner(&self) -> &RcInner { // This unsafety is ok because while this Rc is alive we're guaranteed // that the inner pointer is valid. unsafe { self.ptr.as_ref() } } #[inline] - fn into_inner_with_allocator(this: Self) -> (NonNull>, A) { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + const fn into_inner_with_allocator(this: Self) -> (NonNull>, A) { let this = mem::ManuallyDrop::new(this); (this.ptr, unsafe { ptr::read(&this.alloc) }) } #[inline] - unsafe fn from_inner_in(ptr: NonNull>, alloc: A) -> Self { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + const unsafe fn from_inner_in(ptr: NonNull>, alloc: A) -> Self { Self { ptr, phantom: PhantomData, alloc } } #[inline] - unsafe fn from_ptr_in(ptr: *mut RcInner, alloc: A) -> Self { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + const unsafe fn from_ptr_in(ptr: *mut RcInner, alloc: A) -> Self { unsafe { Self::from_inner_in(NonNull::new_unchecked(ptr), alloc) } } @@ -1452,7 +1455,8 @@ impl Rc { /// ``` #[must_use = "losing the pointer will leak memory"] #[unstable(feature = "allocator_api", issue = "32838")] - pub fn into_raw_with_allocator(this: Self) -> (*const T, A) { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + pub const fn into_raw_with_allocator(this: Self) -> (*const T, A) { let this = mem::ManuallyDrop::new(this); let ptr = Self::as_ptr(&this); // Safety: `this` is ManuallyDrop so the allocator will not be double-dropped @@ -1478,7 +1482,8 @@ impl Rc { /// ``` #[stable(feature = "weak_into_raw", since = "1.45.0")] #[rustc_never_returns_null_ptr] - pub fn as_ptr(this: &Self) -> *const T { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + pub const fn as_ptr(this: &Self) -> *const T { let ptr: *mut RcInner = NonNull::as_ptr(this.ptr); // SAFETY: This cannot go through Deref::deref or Rc::inner because @@ -1555,7 +1560,8 @@ impl Rc { /// } /// ``` #[unstable(feature = "allocator_api", issue = "32838")] - pub unsafe fn from_raw_in(ptr: *const T, alloc: A) -> Self { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + pub const unsafe fn from_raw_in(ptr: *const T, alloc: A) -> Self { let offset = unsafe { data_offset(ptr) }; // Reverse the offset to find the original RcInner. @@ -2248,7 +2254,8 @@ impl RcFromSlice for Rc<[T]> { } #[stable(feature = "rust1", since = "1.0.0")] -impl Deref for Rc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Deref for Rc { type Target = T; #[inline(always)] @@ -2853,7 +2860,8 @@ where } #[stable(feature = "shared_from_str", since = "1.62.0")] -impl From> for Rc<[u8]> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Rc<[u8], A> { /// Converts a reference-counted string slice into a byte slice. /// /// # Example @@ -2865,14 +2873,16 @@ impl From> for Rc<[u8]> { /// assert_eq!("eggplant".as_bytes(), bytes.as_ref()); /// ``` #[inline] - fn from(rc: Rc) -> Self { + fn from(rc: Rc) -> Self { + let (raw, alloc) = Rc::into_raw_with_allocator(rc); // SAFETY: `str` has the same layout as `[u8]`. - unsafe { Rc::from_raw(Rc::into_raw(rc) as *const [u8]) } + unsafe { Rc::from_raw_in(raw as *const [u8], alloc) } } } #[stable(feature = "boxed_slice_try_from", since = "1.43.0")] -impl TryFrom> for Rc<[T; N], A> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const TryFrom> for Rc<[T; N], A> { type Error = Rc<[T], A>; fn try_from(boxed_slice: Rc<[T], A>) -> Result { @@ -3641,14 +3651,16 @@ impl<'a> RcInnerPtr for WeakInner<'a> { } #[stable(feature = "rust1", since = "1.0.0")] -impl borrow::Borrow for Rc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const borrow::Borrow for Rc { fn borrow(&self) -> &T { &**self } } #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")] -impl AsRef for Rc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for Rc { fn as_ref(&self) -> &T { &**self } @@ -3663,7 +3675,8 @@ impl Unpin for Rc {} /// /// The pointer must point to (and have valid metadata for) a previously /// valid instance of T, but the T is allowed to be dropped. -unsafe fn data_offset(ptr: *const T) -> usize { +#[rustc_const_unstable(feature = "allocator_api", issue = "32838")] +const unsafe fn data_offset(ptr: *const T) -> usize { // Align the unsized value to the end of the RcInner. // Because RcInner is repr(C), it will always be the last field in memory. // SAFETY: since the only unsized types possible are slices, trait objects, @@ -3674,7 +3687,8 @@ unsafe fn data_offset(ptr: *const T) -> usize { } #[inline] -fn data_offset_align(align: usize) -> usize { +#[rustc_const_unstable(feature = "allocator_api", issue = "32838")] +const fn data_offset_align(align: usize) -> usize { let layout = Layout::new::>(); layout.size() + layout.padding_needed_for(align) } @@ -3773,28 +3787,32 @@ impl fmt::Pointer for UniqueRc { } #[unstable(feature = "unique_rc_arc", issue = "112566")] -impl borrow::Borrow for UniqueRc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const borrow::Borrow for UniqueRc { fn borrow(&self) -> &T { &**self } } #[unstable(feature = "unique_rc_arc", issue = "112566")] -impl borrow::BorrowMut for UniqueRc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const borrow::BorrowMut for UniqueRc { fn borrow_mut(&mut self) -> &mut T { &mut **self } } #[unstable(feature = "unique_rc_arc", issue = "112566")] -impl AsRef for UniqueRc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for UniqueRc { fn as_ref(&self) -> &T { &**self } } #[unstable(feature = "unique_rc_arc", issue = "112566")] -impl AsMut for UniqueRc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsMut for UniqueRc { fn as_mut(&mut self) -> &mut T { &mut **self } @@ -4058,7 +4076,8 @@ impl UniqueRc { } #[unstable(feature = "unique_rc_arc", issue = "112566")] -impl Deref for UniqueRc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Deref for UniqueRc { type Target = T; fn deref(&self) -> &T { @@ -4068,7 +4087,8 @@ impl Deref for UniqueRc { } #[unstable(feature = "unique_rc_arc", issue = "112566")] -impl DerefMut for UniqueRc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const DerefMut for UniqueRc { fn deref_mut(&mut self) -> &mut T { // SAFETY: This pointer was allocated at creation time so we know it is valid. We know we // have unique ownership and therefore it's safe to make a mutable reference because diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index b4da56578c894..6c924bd48d6c9 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -477,7 +477,8 @@ impl [T] { #[stable(feature = "rust1", since = "1.0.0")] #[inline] #[rustc_diagnostic_item = "slice_into_vec"] - pub fn into_vec(self: Box) -> Vec { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_vec(self: Box) -> Vec { unsafe { let len = self.len(); let (b, alloc) = Box::into_raw_with_allocator(self); @@ -788,16 +789,18 @@ impl> Join<&[T]> for [V] { //////////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] -impl Borrow<[T]> for Vec { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow<[T]> for Vec { fn borrow(&self) -> &[T] { - &self[..] + &**self } } #[stable(feature = "rust1", since = "1.0.0")] -impl BorrowMut<[T]> for Vec { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const BorrowMut<[T]> for Vec { fn borrow_mut(&mut self) -> &mut [T] { - &mut self[..] + &mut **self } } diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index 22cdd8ecde022..55f58a695acff 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -6,7 +6,6 @@ // Many of the usings in this module are only used in the test configuration. // It's cleaner to just turn off the unused_imports warning than to fix them. #![allow(unused_imports)] - use core::borrow::{Borrow, BorrowMut}; use core::iter::FusedIterator; use core::mem::MaybeUninit; @@ -49,6 +48,7 @@ pub use core::str::{from_raw_parts, from_raw_parts_mut}; use core::unicode::conversions; use core::{mem, ptr}; +use crate::alloc::Allocator; use crate::borrow::ToOwned; use crate::boxed::Box; use crate::slice::{Concat, Join, SliceIndex}; @@ -186,18 +186,20 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl Borrow for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow for String { #[inline] fn borrow(&self) -> &str { - &self[..] + &**self } } #[stable(feature = "string_borrow_mut", since = "1.36.0")] -impl BorrowMut for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const BorrowMut for String { #[inline] fn borrow_mut(&mut self) -> &mut str { - &mut self[..] + &mut **self } } @@ -234,8 +236,10 @@ impl str { #[stable(feature = "str_box_extras", since = "1.20.0")] #[must_use = "`self` will be dropped if the result is not used"] #[inline] - pub fn into_boxed_bytes(self: Box) -> Box<[u8]> { - self.into() + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_boxed_bytes(self: Box) -> Box<[u8], A> { + let (raw, alloc) = Box::into_raw_with_allocator(self); + unsafe { Box::from_raw_in(raw as *mut [u8], alloc) } } /// Replaces all matches of a pattern with another string. @@ -498,7 +502,8 @@ impl str { #[rustc_allow_incoherent_impl] #[must_use = "`self` will be dropped if the result is not used"] #[inline] - pub fn into_string(self: Box) -> String { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_string(self: Box) -> String { let slice = Box::<[u8]>::from(self); unsafe { String::from_utf8_unchecked(slice.into_vec()) } } diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index a189c00a6b61d..4740b50c3cdcc 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -560,7 +560,8 @@ impl String { #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "string_from_utf8"] - pub fn from_utf8(vec: Vec) -> Result { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn from_utf8(vec: Vec) -> Result { match str::from_utf8(&vec) { Ok(..) => Ok(String { vec }), Err(e) => Err(FromUtf8Error { bytes: vec, error: e }), @@ -1024,7 +1025,8 @@ impl String { #[inline] #[must_use] #[stable(feature = "rust1", since = "1.0.0")] - pub unsafe fn from_utf8_unchecked(bytes: Vec) -> String { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const unsafe fn from_utf8_unchecked(bytes: Vec) -> String { String { vec: bytes } } @@ -2168,8 +2170,9 @@ impl FromUtf8Error { /// ``` #[must_use] #[stable(feature = "from_utf8_error_as_bytes", since = "1.26.0")] - pub fn as_bytes(&self) -> &[u8] { - &self.bytes[..] + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn as_bytes(&self) -> &[u8] { + &*self.bytes } /// Converts the bytes into a `String` lossily, substituting invalid UTF-8 @@ -2238,7 +2241,8 @@ impl FromUtf8Error { /// ``` #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "rust1", since = "1.0.0")] - pub fn into_bytes(self) -> Vec { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_bytes(self) -> Vec { self.bytes } @@ -2265,7 +2269,8 @@ impl FromUtf8Error { /// ``` #[must_use] #[stable(feature = "rust1", since = "1.0.0")] - pub fn utf8_error(&self) -> Utf8Error { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn utf8_error(&self) -> Utf8Error { self.error } } @@ -2730,7 +2735,8 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl ops::Deref for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const ops::Deref for String { type Target = str; #[inline] @@ -2743,7 +2749,8 @@ impl ops::Deref for String { unsafe impl ops::DerefPure for String {} #[stable(feature = "derefmut_for_string", since = "1.3.0")] -impl ops::DerefMut for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const ops::DerefMut for String { #[inline] fn deref_mut(&mut self) -> &mut str { self.as_mut_str() @@ -3022,7 +3029,8 @@ impl SpecToString for fmt::Arguments<'_> { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for String { #[inline] fn as_ref(&self) -> &str { self @@ -3030,7 +3038,8 @@ impl AsRef for String { } #[stable(feature = "string_as_mut", since = "1.43.0")] -impl AsMut for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsMut for String { #[inline] fn as_mut(&mut self) -> &mut str { self @@ -3038,7 +3047,8 @@ impl AsMut for String { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef<[u8]> for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef<[u8]> for String { #[inline] fn as_ref(&self) -> &[u8] { self.as_bytes() @@ -3083,7 +3093,8 @@ impl From<&String> for String { // note: test pulls in std, which causes errors here #[stable(feature = "string_from_box", since = "1.18.0")] -impl From> for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for String { /// Converts the given boxed `str` slice to a [`String`]. /// It is notable that the `str` slice is owned. /// @@ -3146,7 +3157,8 @@ impl<'a> From> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From<&'a str> for Cow<'a, str> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From<&'a str> for Cow<'a, str> { /// Converts a string slice into a [`Borrowed`] variant. /// No heap allocation is performed, and the string /// is not copied. @@ -3167,7 +3179,8 @@ impl<'a> From<&'a str> for Cow<'a, str> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From for Cow<'a, str> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From for Cow<'a, str> { /// Converts a [`String`] into an [`Owned`] variant. /// No heap allocation is performed, and the string /// is not copied. @@ -3190,7 +3203,8 @@ impl<'a> From for Cow<'a, str> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_from_string_ref", since = "1.28.0")] -impl<'a> From<&'a String> for Cow<'a, str> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From<&'a String> for Cow<'a, str> { /// Converts a [`String`] reference into a [`Borrowed`] variant. /// No heap allocation is performed, and the string /// is not copied. @@ -3235,7 +3249,8 @@ impl<'a> FromIterator for Cow<'a, str> { } #[stable(feature = "from_string_for_vec_u8", since = "1.14.0")] -impl From for Vec { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for Vec { /// Converts the given [`String`] to a vector [`Vec`] that holds values of type [`u8`]. /// /// # Examples @@ -3254,7 +3269,8 @@ impl From for Vec { } #[stable(feature = "try_from_vec_u8_for_string", since = "1.87.0")] -impl TryFrom> for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const TryFrom> for String { type Error = FromUtf8Error; /// Converts the given [`Vec`] into a [`String`] if it contains valid UTF-8 data. /// diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index b8925f4544f44..ebc78ac867f1a 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -293,18 +293,21 @@ impl Arc { impl Arc { #[inline] - fn into_inner_with_allocator(this: Self) -> (NonNull>, A) { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + const fn into_inner_with_allocator(this: Self) -> (NonNull>, A) { let this = mem::ManuallyDrop::new(this); (this.ptr, unsafe { ptr::read(&this.alloc) }) } #[inline] - unsafe fn from_inner_in(ptr: NonNull>, alloc: A) -> Self { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + const unsafe fn from_inner_in(ptr: NonNull>, alloc: A) -> Self { Self { ptr, phantom: PhantomData, alloc } } #[inline] - unsafe fn from_ptr_in(ptr: *mut ArcInner, alloc: A) -> Self { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + const unsafe fn from_ptr_in(ptr: *mut ArcInner, alloc: A) -> Self { unsafe { Self::from_inner_in(NonNull::new_unchecked(ptr), alloc) } } } @@ -1602,7 +1605,8 @@ impl Arc { /// ``` #[must_use = "losing the pointer will leak memory"] #[unstable(feature = "allocator_api", issue = "32838")] - pub fn into_raw_with_allocator(this: Self) -> (*const T, A) { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + pub const fn into_raw_with_allocator(this: Self) -> (*const T, A) { let this = mem::ManuallyDrop::new(this); let ptr = Self::as_ptr(&this); // Safety: `this` is ManuallyDrop so the allocator will not be double-dropped @@ -1629,7 +1633,8 @@ impl Arc { #[must_use] #[stable(feature = "rc_as_ptr", since = "1.45.0")] #[rustc_never_returns_null_ptr] - pub fn as_ptr(this: &Self) -> *const T { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + pub const fn as_ptr(this: &Self) -> *const T { let ptr: *mut ArcInner = NonNull::as_ptr(this.ptr); // SAFETY: This cannot go through Deref::deref or RcInnerPtr::inner because @@ -1707,7 +1712,8 @@ impl Arc { /// ``` #[inline] #[unstable(feature = "allocator_api", issue = "32838")] - pub unsafe fn from_raw_in(ptr: *const T, alloc: A) -> Self { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + pub const unsafe fn from_raw_in(ptr: *const T, alloc: A) -> Self { unsafe { let offset = data_offset(ptr); @@ -1917,7 +1923,8 @@ impl Arc { } #[inline] - fn inner(&self) -> &ArcInner { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const fn inner(&self) -> &ArcInner { // This unsafety is ok because while this arc is alive we're guaranteed // that the inner pointer is valid. Furthermore, we know that the // `ArcInner` structure itself is `Sync` because the inner data is @@ -2233,7 +2240,8 @@ impl Clone for Arc { impl UseCloned for Arc {} #[stable(feature = "rust1", since = "1.0.0")] -impl Deref for Arc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Deref for Arc { type Target = T; #[inline] @@ -3878,7 +3886,8 @@ where } #[stable(feature = "shared_from_str", since = "1.62.0")] -impl From> for Arc<[u8]> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Arc<[u8], A> { /// Converts an atomically reference-counted string slice into a byte slice. /// /// # Example @@ -3890,14 +3899,16 @@ impl From> for Arc<[u8]> { /// assert_eq!("eggplant".as_bytes(), bytes.as_ref()); /// ``` #[inline] - fn from(rc: Arc) -> Self { + fn from(rc: Arc) -> Self { + let (raw, alloc) = Arc::into_raw_with_allocator(rc); // SAFETY: `str` has the same layout as `[u8]`. - unsafe { Arc::from_raw(Arc::into_raw(rc) as *const [u8]) } + unsafe { Arc::from_raw_in(raw as *const [u8], alloc) } } } #[stable(feature = "boxed_slice_try_from", since = "1.43.0")] -impl TryFrom> for Arc<[T; N], A> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const TryFrom> for Arc<[T; N], A> { type Error = Arc<[T], A>; fn try_from(boxed_slice: Arc<[T], A>) -> Result { @@ -3997,14 +4008,16 @@ impl> ToArcSlice for I { } #[stable(feature = "rust1", since = "1.0.0")] -impl borrow::Borrow for Arc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const borrow::Borrow for Arc { fn borrow(&self) -> &T { &**self } } #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")] -impl AsRef for Arc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for Arc { fn as_ref(&self) -> &T { &**self } @@ -4019,7 +4032,8 @@ impl Unpin for Arc {} /// /// The pointer must point to (and have valid metadata for) a previously /// valid instance of T, but the T is allowed to be dropped. -unsafe fn data_offset(ptr: *const T) -> usize { +#[rustc_const_unstable(feature = "allocator_api", issue = "32838")] +const unsafe fn data_offset(ptr: *const T) -> usize { // Align the unsized value to the end of the ArcInner. // Because RcInner is repr(C), it will always be the last field in memory. // SAFETY: since the only unsized types possible are slices, trait objects, @@ -4030,7 +4044,8 @@ unsafe fn data_offset(ptr: *const T) -> usize { } #[inline] -fn data_offset_align(align: usize) -> usize { +#[rustc_const_unstable(feature = "allocator_api", issue = "32838")] +const fn data_offset_align(align: usize) -> usize { let layout = Layout::new::>(); layout.size() + layout.padding_needed_for(align) } @@ -4207,28 +4222,32 @@ impl fmt::Pointer for UniqueArc { } #[unstable(feature = "unique_rc_arc", issue = "112566")] -impl borrow::Borrow for UniqueArc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const borrow::Borrow for UniqueArc { fn borrow(&self) -> &T { &**self } } #[unstable(feature = "unique_rc_arc", issue = "112566")] -impl borrow::BorrowMut for UniqueArc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const borrow::BorrowMut for UniqueArc { fn borrow_mut(&mut self) -> &mut T { &mut **self } } #[unstable(feature = "unique_rc_arc", issue = "112566")] -impl AsRef for UniqueArc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for UniqueArc { fn as_ref(&self) -> &T { &**self } } #[unstable(feature = "unique_rc_arc", issue = "112566")] -impl AsMut for UniqueArc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsMut for UniqueArc { fn as_mut(&mut self) -> &mut T { &mut **self } @@ -4488,7 +4507,8 @@ impl UniqueArc { } #[unstable(feature = "unique_rc_arc", issue = "112566")] -impl Deref for UniqueArc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Deref for UniqueArc { type Target = T; fn deref(&self) -> &T { @@ -4502,7 +4522,8 @@ impl Deref for UniqueArc { unsafe impl PinCoerceUnsized for UniqueArc {} #[unstable(feature = "unique_rc_arc", issue = "112566")] -impl DerefMut for UniqueArc { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const DerefMut for UniqueArc { fn deref_mut(&mut self) -> &mut T { // SAFETY: This pointer was allocated at creation time so we know it is valid. We know we // have unique ownership and therefore it's safe to make a mutable reference because diff --git a/library/alloc/src/vec/cow.rs b/library/alloc/src/vec/cow.rs index 4deb35efffc14..dc00f9b9f4b98 100644 --- a/library/alloc/src/vec/cow.rs +++ b/library/alloc/src/vec/cow.rs @@ -2,7 +2,8 @@ use super::Vec; use crate::borrow::Cow; #[stable(feature = "cow_from_vec", since = "1.8.0")] -impl<'a, T: Clone> From<&'a [T]> for Cow<'a, [T]> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a, T: Clone> const From<&'a [T]> for Cow<'a, [T]> { /// Creates a [`Borrowed`] variant of [`Cow`] /// from a slice. /// @@ -15,7 +16,8 @@ impl<'a, T: Clone> From<&'a [T]> for Cow<'a, [T]> { } #[stable(feature = "cow_from_array_ref", since = "1.77.0")] -impl<'a, T: Clone, const N: usize> From<&'a [T; N]> for Cow<'a, [T]> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a, T: Clone, const N: usize> const From<&'a [T; N]> for Cow<'a, [T]> { /// Creates a [`Borrowed`] variant of [`Cow`] /// from a reference to an array. /// @@ -28,7 +30,8 @@ impl<'a, T: Clone, const N: usize> From<&'a [T; N]> for Cow<'a, [T]> { } #[stable(feature = "cow_from_vec", since = "1.8.0")] -impl<'a, T: Clone> From> for Cow<'a, [T]> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a, T: Clone> const From> for Cow<'a, [T]> { /// Creates an [`Owned`] variant of [`Cow`] /// from an owned instance of [`Vec`]. /// @@ -41,7 +44,8 @@ impl<'a, T: Clone> From> for Cow<'a, [T]> { } #[stable(feature = "cow_from_vec_ref", since = "1.28.0")] -impl<'a, T: Clone> From<&'a Vec> for Cow<'a, [T]> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a, T: Clone> const From<&'a Vec> for Cow<'a, [T]> { /// Creates a [`Borrowed`] variant of [`Cow`] /// from a reference to [`Vec`]. /// diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 9856e9c18ec68..a77c51b8edf36 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1057,7 +1057,13 @@ impl Vec { /// ``` #[inline] #[unstable(feature = "allocator_api", issue = "32838")] - pub unsafe fn from_raw_parts_in(ptr: *mut T, length: usize, capacity: usize, alloc: A) -> Self { + #[rustc_const_unstable(feature = "allocator_api", issue = "32838")] + pub const unsafe fn from_raw_parts_in( + ptr: *mut T, + length: usize, + capacity: usize, + alloc: A, + ) -> Self { ub_checks::assert_unsafe_precondition!( check_library_ub, "Vec::from_raw_parts_in requires that length <= capacity", @@ -3392,7 +3398,8 @@ impl ExtendFromWithinSpec for Vec { //////////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] -impl ops::Deref for Vec { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const ops::Deref for Vec { type Target = [T]; #[inline] @@ -3402,7 +3409,8 @@ impl ops::Deref for Vec { } #[stable(feature = "rust1", since = "1.0.0")] -impl ops::DerefMut for Vec { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const ops::DerefMut for Vec { #[inline] fn deref_mut(&mut self) -> &mut [T] { self.as_mut_slice() @@ -3832,6 +3840,26 @@ impl Vec { } } +impl Vec { + /// Private function solely to make [`CString::into_bytes`] const without marking any methods + /// here const-unstable. + /// + /// # Safety + /// + /// The vec must be nonempty. + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub(crate) const unsafe fn pop_zero_byte(&mut self) { + // SAFETY: The vec is nonempty. + unsafe { + self.len -= 1; + core::hint::assert_unchecked(self.len < self.capacity()); + } + + // SAFETY: The vec was nonempty. + debug_assert!(unsafe { ptr::read(self.as_ptr().add(self.len())) } == 0); + } +} + /// Extend implementation that copies elements out of references before pushing them onto the Vec. /// /// This implementation is specialized for slice iterators, where it uses [`copy_from_slice`] to @@ -3927,28 +3955,32 @@ impl fmt::Debug for Vec { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef> for Vec { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef> for Vec { fn as_ref(&self) -> &Vec { self } } #[stable(feature = "vec_as_mut", since = "1.5.0")] -impl AsMut> for Vec { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsMut> for Vec { fn as_mut(&mut self) -> &mut Vec { self } } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef<[T]> for Vec { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef<[T]> for Vec { fn as_ref(&self) -> &[T] { self } } #[stable(feature = "vec_as_mut", since = "1.5.0")] -impl AsMut<[T]> for Vec { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsMut<[T]> for Vec { fn as_mut(&mut self) -> &mut [T] { self } @@ -4061,7 +4093,8 @@ where // note: test pulls in std, which causes errors here #[stable(feature = "vec_from_box", since = "1.18.0")] -impl From> for Vec { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Vec { /// Converts a boxed slice into a vector by transferring ownership of /// the existing heap allocation. /// diff --git a/library/alloc/src/vec/peek_mut.rs b/library/alloc/src/vec/peek_mut.rs index c0dd941ed3933..9830ae18b0bab 100644 --- a/library/alloc/src/vec/peek_mut.rs +++ b/library/alloc/src/vec/peek_mut.rs @@ -36,7 +36,8 @@ impl<'a, T> PeekMut<'a, T> { } #[unstable(feature = "vec_peek_mut", issue = "122742")] -impl<'a, T> Deref for PeekMut<'a, T> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a, T> const Deref for PeekMut<'a, T> { type Target = T; fn deref(&self) -> &Self::Target { @@ -46,7 +47,8 @@ impl<'a, T> Deref for PeekMut<'a, T> { } #[unstable(feature = "vec_peek_mut", issue = "122742")] -impl<'a, T> DerefMut for PeekMut<'a, T> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a, T> const DerefMut for PeekMut<'a, T> { fn deref_mut(&mut self) -> &mut Self::Target { let idx = self.vec.len() - 1; // SAFETY: PeekMut is only constructed if the vec is non-empty diff --git a/library/alloctests/lib.rs b/library/alloctests/lib.rs index 3241b4b00454b..68426404376f7 100644 --- a/library/alloctests/lib.rs +++ b/library/alloctests/lib.rs @@ -46,6 +46,7 @@ // Language features: // tidy-alphabetical-start #![feature(cfg_sanitize)] +#![feature(const_trait_impl)] #![feature(dropck_eyepatch)] #![feature(lang_items)] #![feature(min_specialization)] diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 1c23218552aa1..0e62e851483b9 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -198,7 +198,7 @@ impl Error for TryFromSliceError { } #[stable(feature = "try_from_slice_error", since = "1.36.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for TryFromSliceError { fn from(x: Infallible) -> TryFromSliceError { match x {} @@ -206,7 +206,8 @@ impl const From for TryFromSliceError { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef<[T]> for [T; N] { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef<[T]> for [T; N] { #[inline] fn as_ref(&self) -> &[T] { &self[..] @@ -214,7 +215,8 @@ impl AsRef<[T]> for [T; N] { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsMut<[T]> for [T; N] { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsMut<[T]> for [T; N] { #[inline] fn as_mut(&mut self) -> &mut [T] { &mut self[..] @@ -222,14 +224,16 @@ impl AsMut<[T]> for [T; N] { } #[stable(feature = "array_borrow", since = "1.4.0")] -impl Borrow<[T]> for [T; N] { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow<[T]> for [T; N] { fn borrow(&self) -> &[T] { self } } #[stable(feature = "array_borrow", since = "1.4.0")] -impl BorrowMut<[T]> for [T; N] { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const BorrowMut<[T]> for [T; N] { fn borrow_mut(&mut self) -> &mut [T] { self } @@ -248,7 +252,8 @@ impl BorrowMut<[T]> for [T; N] { /// assert_eq!(512, u16::from_le_bytes(bytes_tail)); /// ``` #[stable(feature = "try_from", since = "1.34.0")] -impl TryFrom<&[T]> for [T; N] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const TryFrom<&[T]> for [T; N] where T: Copy, { @@ -273,7 +278,8 @@ where /// assert_eq!(512, u16::from_le_bytes(bytes_tail)); /// ``` #[stable(feature = "try_from_mut_slice_to_array", since = "1.59.0")] -impl TryFrom<&mut [T]> for [T; N] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const TryFrom<&mut [T]> for [T; N] where T: Copy, { @@ -298,7 +304,8 @@ where /// assert_eq!(512, u16::from_le_bytes(*bytes_tail)); /// ``` #[stable(feature = "try_from", since = "1.34.0")] -impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a, T, const N: usize> const TryFrom<&'a [T]> for &'a [T; N] { type Error = TryFromSliceError; #[inline] @@ -320,7 +327,8 @@ impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] { /// assert_eq!(512, u16::from_le_bytes(*bytes_tail)); /// ``` #[stable(feature = "try_from", since = "1.34.0")] -impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N] { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a, T, const N: usize> const TryFrom<&'a mut [T]> for &'a mut [T; N] { type Error = TryFromSliceError; #[inline] diff --git a/library/core/src/ascii/ascii_char.rs b/library/core/src/ascii/ascii_char.rs index 054ddf844700e..ba9d388903c51 100644 --- a/library/core/src/ascii/ascii_char.rs +++ b/library/core/src/ascii/ascii_char.rs @@ -546,7 +546,7 @@ macro_rules! into_int_impl { ($($ty:ty)*) => { $( #[unstable(feature = "ascii_char", issue = "110998")] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for $ty { #[inline] fn from(chr: AsciiChar) -> $ty { diff --git a/library/core/src/borrow.rs b/library/core/src/borrow.rs index da05f236d2fef..6ce7e4656b99e 100644 --- a/library/core/src/borrow.rs +++ b/library/core/src/borrow.rs @@ -154,6 +154,8 @@ /// [`String`]: ../../std/string/struct.String.html #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "Borrow"] +#[const_trait] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub trait Borrow { /// Immutably borrows from an owned value. /// @@ -185,6 +187,8 @@ pub trait Borrow { /// for more information on borrowing as another type. #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "BorrowMut"] +#[const_trait] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub trait BorrowMut: Borrow { /// Mutably borrows from an owned value. /// @@ -206,7 +210,8 @@ pub trait BorrowMut: Borrow { } #[stable(feature = "rust1", since = "1.0.0")] -impl Borrow for T { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow for T { #[rustc_diagnostic_item = "noop_method_borrow"] fn borrow(&self) -> &T { self @@ -214,28 +219,32 @@ impl Borrow for T { } #[stable(feature = "rust1", since = "1.0.0")] -impl BorrowMut for T { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const BorrowMut for T { fn borrow_mut(&mut self) -> &mut T { self } } #[stable(feature = "rust1", since = "1.0.0")] -impl Borrow for &T { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow for &T { fn borrow(&self) -> &T { self } } #[stable(feature = "rust1", since = "1.0.0")] -impl Borrow for &mut T { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow for &mut T { fn borrow(&self) -> &T { self } } #[stable(feature = "rust1", since = "1.0.0")] -impl BorrowMut for &mut T { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const BorrowMut for &mut T { fn borrow_mut(&mut self) -> &mut T { self } diff --git a/library/core/src/bstr/mod.rs b/library/core/src/bstr/mod.rs index 13127d645a257..d69fd561782eb 100644 --- a/library/core/src/bstr/mod.rs +++ b/library/core/src/bstr/mod.rs @@ -63,14 +63,16 @@ impl ByteStr { /// ``` #[inline] #[unstable(feature = "bstr", issue = "134915")] - pub fn new>(bytes: &B) -> &Self { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn new>(bytes: &B) -> &Self { ByteStr::from_bytes(bytes.as_ref()) } #[doc(hidden)] #[unstable(feature = "bstr_internals", issue = "none")] #[inline] - pub fn from_bytes(slice: &[u8]) -> &Self { + #[rustc_const_unstable(feature = "bstr_internals", issue = "none")] + pub const fn from_bytes(slice: &[u8]) -> &Self { // SAFETY: `ByteStr` is a transparent wrapper around `[u8]`, so we can turn a reference to // the wrapped type into a reference to the wrapper type. unsafe { &*(slice as *const [u8] as *const Self) } @@ -79,7 +81,8 @@ impl ByteStr { #[doc(hidden)] #[unstable(feature = "bstr_internals", issue = "none")] #[inline] - pub fn from_bytes_mut(slice: &mut [u8]) -> &mut Self { + #[rustc_const_unstable(feature = "bstr_internals", issue = "none")] + pub const fn from_bytes_mut(slice: &mut [u8]) -> &mut Self { // SAFETY: `ByteStr` is a transparent wrapper around `[u8]`, so we can turn a reference to // the wrapped type into a reference to the wrapper type. unsafe { &mut *(slice as *mut [u8] as *mut Self) } @@ -88,20 +91,23 @@ impl ByteStr { #[doc(hidden)] #[unstable(feature = "bstr_internals", issue = "none")] #[inline] - pub fn as_bytes(&self) -> &[u8] { + #[rustc_const_unstable(feature = "bstr_internals", issue = "none")] + pub const fn as_bytes(&self) -> &[u8] { &self.0 } #[doc(hidden)] #[unstable(feature = "bstr_internals", issue = "none")] #[inline] - pub fn as_bytes_mut(&mut self) -> &mut [u8] { + #[rustc_const_unstable(feature = "bstr_internals", issue = "none")] + pub const fn as_bytes_mut(&mut self) -> &mut [u8] { &mut self.0 } } #[unstable(feature = "bstr", issue = "134915")] -impl Deref for ByteStr { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Deref for ByteStr { type Target = [u8]; #[inline] @@ -111,7 +117,8 @@ impl Deref for ByteStr { } #[unstable(feature = "bstr", issue = "134915")] -impl DerefMut for ByteStr { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const DerefMut for ByteStr { #[inline] fn deref_mut(&mut self) -> &mut [u8] { &mut self.0 @@ -185,7 +192,8 @@ impl fmt::Display for ByteStr { } #[unstable(feature = "bstr", issue = "134915")] -impl AsRef<[u8]> for ByteStr { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef<[u8]> for ByteStr { #[inline] fn as_ref(&self) -> &[u8] { &self.0 @@ -193,7 +201,8 @@ impl AsRef<[u8]> for ByteStr { } #[unstable(feature = "bstr", issue = "134915")] -impl AsRef for ByteStr { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for ByteStr { #[inline] fn as_ref(&self) -> &ByteStr { self @@ -203,7 +212,8 @@ impl AsRef for ByteStr { // `impl AsRef for [u8]` omitted to avoid widespread inference failures #[unstable(feature = "bstr", issue = "134915")] -impl AsRef for str { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for str { #[inline] fn as_ref(&self) -> &ByteStr { ByteStr::new(self) @@ -211,7 +221,8 @@ impl AsRef for str { } #[unstable(feature = "bstr", issue = "134915")] -impl AsMut<[u8]> for ByteStr { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsMut<[u8]> for ByteStr { #[inline] fn as_mut(&mut self) -> &mut [u8] { &mut self.0 @@ -225,7 +236,8 @@ impl AsMut<[u8]> for ByteStr { // `impl Borrow for str` omitted to avoid widespread inference failures #[unstable(feature = "bstr", issue = "134915")] -impl Borrow<[u8]> for ByteStr { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow<[u8]> for ByteStr { #[inline] fn borrow(&self) -> &[u8] { &self.0 @@ -235,7 +247,8 @@ impl Borrow<[u8]> for ByteStr { // `impl BorrowMut for [u8]` omitted to avoid widespread inference failures #[unstable(feature = "bstr", issue = "134915")] -impl BorrowMut<[u8]> for ByteStr { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const BorrowMut<[u8]> for ByteStr { #[inline] fn borrow_mut(&mut self) -> &mut [u8] { &mut self.0 @@ -303,7 +316,8 @@ impl<'a> Default for &'a mut ByteStr { // } #[unstable(feature = "bstr", issue = "134915")] -impl<'a> TryFrom<&'a ByteStr> for &'a str { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const TryFrom<&'a ByteStr> for &'a str { type Error = crate::str::Utf8Error; #[inline] @@ -313,7 +327,8 @@ impl<'a> TryFrom<&'a ByteStr> for &'a str { } #[unstable(feature = "bstr", issue = "134915")] -impl<'a> TryFrom<&'a mut ByteStr> for &'a mut str { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const TryFrom<&'a mut ByteStr> for &'a mut str { type Error = crate::str::Utf8Error; #[inline] diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs index d6d1bf2effae2..0b8a802793b9d 100644 --- a/library/core/src/cell.rs +++ b/library/core/src/cell.rs @@ -390,7 +390,8 @@ impl Ord for Cell { } #[stable(feature = "cell_from", since = "1.12.0")] -impl From for Cell { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for Cell { /// Creates a new `Cell` containing the given value. fn from(t: T) -> Cell { Cell::new(t) @@ -1402,7 +1403,8 @@ impl Ord for RefCell { } #[stable(feature = "cell_from", since = "1.12.0")] -impl From for RefCell { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for RefCell { /// Creates a new `RefCell` containing the given value. fn from(t: T) -> RefCell { RefCell::new(t) @@ -1483,7 +1485,7 @@ pub struct Ref<'b, T: ?Sized + 'b> { } #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_deref", issue = "88955")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const Deref for Ref<'_, T> { type Target = T; @@ -1874,7 +1876,7 @@ pub struct RefMut<'b, T: ?Sized + 'b> { } #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_deref", issue = "88955")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const Deref for RefMut<'_, T> { type Target = T; @@ -1886,7 +1888,7 @@ impl const Deref for RefMut<'_, T> { } #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_deref", issue = "88955")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const DerefMut for RefMut<'_, T> { #[inline] fn deref_mut(&mut self) -> &mut T { @@ -2341,7 +2343,8 @@ impl const Default for UnsafeCell { } #[stable(feature = "cell_from", since = "1.12.0")] -impl From for UnsafeCell { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for UnsafeCell { /// Creates a new `UnsafeCell` containing the given value. fn from(t: T) -> UnsafeCell { UnsafeCell::new(t) @@ -2446,7 +2449,8 @@ impl const Default for SyncUnsafeCell { } #[unstable(feature = "sync_unsafe_cell", issue = "95439")] -impl From for SyncUnsafeCell { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for SyncUnsafeCell { /// Creates a new `SyncUnsafeCell` containing the given value. fn from(t: T) -> SyncUnsafeCell { SyncUnsafeCell::new(t) diff --git a/library/core/src/cell/once.rs b/library/core/src/cell/once.rs index c6c96571d33c9..833be059d75f7 100644 --- a/library/core/src/cell/once.rs +++ b/library/core/src/cell/once.rs @@ -395,7 +395,8 @@ impl PartialEq for OnceCell { impl Eq for OnceCell {} #[stable(feature = "once_cell", since = "1.70.0")] -impl From for OnceCell { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for OnceCell { /// Creates a new `OnceCell` which already contains the given `value`. #[inline] fn from(value: T) -> Self { diff --git a/library/core/src/char/convert.rs b/library/core/src/char/convert.rs index 23061cb663bc6..047a331c2caa2 100644 --- a/library/core/src/char/convert.rs +++ b/library/core/src/char/convert.rs @@ -36,7 +36,7 @@ pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char { } #[stable(feature = "char_convert", since = "1.13.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for u32 { /// Converts a [`char`] into a [`u32`]. /// @@ -54,7 +54,7 @@ impl const From for u32 { } #[stable(feature = "more_char_conversions", since = "1.51.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for u64 { /// Converts a [`char`] into a [`u64`]. /// @@ -74,7 +74,7 @@ impl const From for u64 { } #[stable(feature = "more_char_conversions", since = "1.51.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for u128 { /// Converts a [`char`] into a [`u128`]. /// @@ -98,7 +98,8 @@ impl const From for u128 { /// /// See [`impl From for char`](char#impl-From-for-char) for details on the encoding. #[stable(feature = "u8_from_char", since = "1.59.0")] -impl TryFrom for u8 { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const TryFrom for u8 { type Error = TryFromCharError; /// Tries to convert a [`char`] into a [`u8`]. @@ -113,7 +114,11 @@ impl TryFrom for u8 { /// ``` #[inline] fn try_from(c: char) -> Result { - u8::try_from(u32::from(c)).map_err(|_| TryFromCharError(())) + // FIXME(const-hack): this should use map_err instead + match u8::try_from(u32::from(c)) { + Ok(b) => Ok(b), + Err(_) => Err(TryFromCharError(())), + } } } @@ -122,7 +127,8 @@ impl TryFrom for u8 { /// /// This corresponds to the UCS-2 encoding, as specified in ISO/IEC 10646:2003. #[stable(feature = "u16_from_char", since = "1.74.0")] -impl TryFrom for u16 { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const TryFrom for u16 { type Error = TryFromCharError; /// Tries to convert a [`char`] into a [`u16`]. @@ -137,7 +143,11 @@ impl TryFrom for u16 { /// ``` #[inline] fn try_from(c: char) -> Result { - u16::try_from(u32::from(c)).map_err(|_| TryFromCharError(())) + // FIXME(const-hack): this should use map_err instead + match u16::try_from(u32::from(c)) { + Ok(x) => Ok(x), + Err(_) => Err(TryFromCharError(())), + } } } @@ -160,7 +170,7 @@ impl TryFrom for u16 { /// for a superset of Windows-1252 that fills the remaining blanks with corresponding /// C0 and C1 control codes. #[stable(feature = "char_convert", since = "1.13.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for char { /// Converts a [`u8`] into a [`char`]. /// @@ -251,7 +261,7 @@ const fn char_try_from_u32(i: u32) -> Result { } #[stable(feature = "try_from", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const TryFrom for char { type Error = CharTryFromError; diff --git a/library/core/src/convert/mod.rs b/library/core/src/convert/mod.rs index 220a24caf09ee..ba9e2a5107a33 100644 --- a/library/core/src/convert/mod.rs +++ b/library/core/src/convert/mod.rs @@ -217,7 +217,7 @@ pub const fn identity(x: T) -> T { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "AsRef"] #[const_trait] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub trait AsRef: PointeeSized { /// Converts this type into a shared reference of the (usually inferred) input type. #[stable(feature = "rust1", since = "1.0.0")] @@ -370,7 +370,7 @@ pub trait AsRef: PointeeSized { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "AsMut"] #[const_trait] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub trait AsMut: PointeeSized { /// Converts this type into a mutable reference of the (usually inferred) input type. #[stable(feature = "rust1", since = "1.0.0")] @@ -449,7 +449,7 @@ pub trait AsMut: PointeeSized { #[rustc_diagnostic_item = "Into"] #[stable(feature = "rust1", since = "1.0.0")] #[doc(search_unbox)] -#[rustc_const_unstable(feature = "const_from", issue = "143773")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] #[const_trait] pub trait Into: Sized { /// Converts this type into the (usually inferred) input type. @@ -586,7 +586,7 @@ pub trait Into: Sized { note = "to coerce a `{T}` into a `{Self}`, use `&*` as a prefix", ))] #[doc(search_unbox)] -#[rustc_const_unstable(feature = "const_from", issue = "143773")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] #[const_trait] pub trait From: Sized { /// Converts to this type from the input type. @@ -615,7 +615,7 @@ pub trait From: Sized { /// [`Into`], see there for details. #[rustc_diagnostic_item = "TryInto"] #[stable(feature = "try_from", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_from", issue = "143773")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] #[const_trait] pub trait TryInto: Sized { /// The type returned in the event of a conversion error. @@ -695,7 +695,7 @@ pub trait TryInto: Sized { /// [`try_from`]: TryFrom::try_from #[rustc_diagnostic_item = "TryFrom"] #[stable(feature = "try_from", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_from", issue = "143773")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] #[const_trait] pub trait TryFrom: Sized { /// The type returned in the event of a conversion error. @@ -714,7 +714,7 @@ pub trait TryFrom: Sized { // As lifts over & #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const AsRef for &T where T: ~const AsRef, @@ -727,7 +727,7 @@ where // As lifts over &mut #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const AsRef for &mut T where T: ~const AsRef, @@ -748,7 +748,7 @@ where // AsMut lifts over &mut #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const AsMut for &mut T where T: ~const AsMut, @@ -769,7 +769,7 @@ where // From implies Into #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_from", issue = "143773")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const Into for T where U: ~const From, @@ -787,7 +787,7 @@ where // From (and thus Into) is reflexive #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_from", issue = "143773")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for T { /// Returns the argument unchanged. #[inline(always)] @@ -804,7 +804,7 @@ impl const From for T { #[stable(feature = "convert_infallible", since = "1.34.0")] #[rustc_reservation_impl = "permitting this impl would forbid us from adding \ `impl From for T` later; see rust-lang/rust#64715 for details"] -#[rustc_const_unstable(feature = "const_from", issue = "143773")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for T { fn from(t: !) -> T { t @@ -813,7 +813,7 @@ impl const From for T { // TryFrom implies TryInto #[stable(feature = "try_from", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_from", issue = "143773")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const TryInto for T where U: ~const TryFrom, @@ -829,7 +829,7 @@ where // Infallible conversions are semantically equivalent to fallible conversions // with an uninhabited error type. #[stable(feature = "try_from", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_from", issue = "143773")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const TryFrom for T where U: ~const Into, @@ -847,7 +847,7 @@ where //////////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const AsRef<[T]> for [T] { #[inline(always)] fn as_ref(&self) -> &[T] { @@ -856,7 +856,7 @@ impl const AsRef<[T]> for [T] { } #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const AsMut<[T]> for [T] { #[inline(always)] fn as_mut(&mut self) -> &mut [T] { @@ -865,7 +865,7 @@ impl const AsMut<[T]> for [T] { } #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const AsRef for str { #[inline(always)] fn as_ref(&self) -> &str { @@ -874,7 +874,7 @@ impl const AsRef for str { } #[stable(feature = "as_mut_str_for_str", since = "1.51.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const AsMut for str { #[inline(always)] fn as_mut(&mut self) -> &mut str { @@ -936,7 +936,7 @@ impl const AsMut for str { pub enum Infallible {} #[stable(feature = "convert_infallible", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_clone", issue = "142757")] impl const Clone for Infallible { fn clone(&self) -> Infallible { match *self {} @@ -990,7 +990,7 @@ impl Ord for Infallible { } #[stable(feature = "convert_infallible", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for Infallible { #[inline] fn from(x: !) -> Self { diff --git a/library/core/src/convert/num.rs b/library/core/src/convert/num.rs index affb4eb64d39d..6ae588a4e044f 100644 --- a/library/core/src/convert/num.rs +++ b/library/core/src/convert/num.rs @@ -69,7 +69,7 @@ macro_rules! impl_from { }; ($Small:ty => $Large:ty, #[$attr:meta], $doc:expr $(,)?) => { #[$attr] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From<$Small> for $Large { // Rustdocs on the impl block show a "[+] show undocumented items" toggle. // Rustdocs on functions do not. @@ -201,7 +201,7 @@ macro_rules! impl_float_from_bool { )? ) => { #[stable(feature = "float_from_bool", since = "1.68.0")] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for $float { #[doc = concat!("Converts a [`bool`] to [`", stringify!($float),"`] losslessly.")] /// The resulting value is positive `0.0` for `false` and `1.0` for `true` values. @@ -252,7 +252,7 @@ impl_float_from_bool!( macro_rules! impl_try_from_unbounded { ($source:ty => $($target:ty),+) => {$( #[stable(feature = "try_from", since = "1.34.0")] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const TryFrom<$source> for $target { type Error = TryFromIntError; @@ -271,7 +271,7 @@ macro_rules! impl_try_from_unbounded { macro_rules! impl_try_from_lower_bounded { ($source:ty => $($target:ty),+) => {$( #[stable(feature = "try_from", since = "1.34.0")] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const TryFrom<$source> for $target { type Error = TryFromIntError; @@ -294,7 +294,7 @@ macro_rules! impl_try_from_lower_bounded { macro_rules! impl_try_from_upper_bounded { ($source:ty => $($target:ty),+) => {$( #[stable(feature = "try_from", since = "1.34.0")] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const TryFrom<$source> for $target { type Error = TryFromIntError; @@ -317,7 +317,7 @@ macro_rules! impl_try_from_upper_bounded { macro_rules! impl_try_from_both_bounded { ($source:ty => $($target:ty),+) => {$( #[stable(feature = "try_from", since = "1.34.0")] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const TryFrom<$source> for $target { type Error = TryFromIntError; @@ -456,7 +456,7 @@ use crate::num::NonZero; macro_rules! impl_nonzero_int_from_nonzero_int { ($Small:ty => $Large:ty) => { #[stable(feature = "nz_int_conv", since = "1.41.0")] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From> for NonZero<$Large> { // Rustdocs on the impl block show a "[+] show undocumented items" toggle. // Rustdocs on functions do not. @@ -515,7 +515,8 @@ impl_nonzero_int_from_nonzero_int!(u64 => i128); macro_rules! impl_nonzero_int_try_from_int { ($Int:ty) => { #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] - impl TryFrom<$Int> for NonZero<$Int> { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + impl const TryFrom<$Int> for NonZero<$Int> { type Error = TryFromIntError; // Rustdocs on the impl block show a "[+] show undocumented items" toggle. @@ -547,7 +548,7 @@ impl_nonzero_int_try_from_int!(isize); macro_rules! impl_nonzero_int_try_from_nonzero_int { ($source:ty => $($target:ty),+) => {$( #[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const TryFrom> for NonZero<$target> { type Error = TryFromIntError; diff --git a/library/core/src/ffi/c_str.rs b/library/core/src/ffi/c_str.rs index c43f3834630f0..c1db569dad990 100644 --- a/library/core/src/ffi/c_str.rs +++ b/library/core/src/ffi/c_str.rs @@ -708,7 +708,8 @@ impl ops::Index> for CStr { } #[stable(feature = "cstring_asref", since = "1.7.0")] -impl AsRef for CStr { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for CStr { #[inline] fn as_ref(&self) -> &CStr { self diff --git a/library/core/src/mem/manually_drop.rs b/library/core/src/mem/manually_drop.rs index 02bb81792931e..8868f05f1b98f 100644 --- a/library/core/src/mem/manually_drop.rs +++ b/library/core/src/mem/manually_drop.rs @@ -258,7 +258,8 @@ impl ManuallyDrop { } #[stable(feature = "manually_drop", since = "1.20.0")] -impl Deref for ManuallyDrop { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Deref for ManuallyDrop { type Target = T; #[inline(always)] fn deref(&self) -> &T { @@ -267,7 +268,8 @@ impl Deref for ManuallyDrop { } #[stable(feature = "manually_drop", since = "1.20.0")] -impl DerefMut for ManuallyDrop { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const DerefMut for ManuallyDrop { #[inline(always)] fn deref_mut(&mut self) -> &mut T { &mut self.value diff --git a/library/core/src/net/ip_addr.rs b/library/core/src/net/ip_addr.rs index 6adeb2aa3fdad..9eb0e9b74b3ef 100644 --- a/library/core/src/net/ip_addr.rs +++ b/library/core/src/net/ip_addr.rs @@ -1088,7 +1088,7 @@ impl fmt::Debug for IpAddr { } #[stable(feature = "ip_from_ip", since = "1.16.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for IpAddr { /// Copies this address to a new `IpAddr::V4`. /// @@ -1111,7 +1111,7 @@ impl const From for IpAddr { } #[stable(feature = "ip_from_ip", since = "1.16.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for IpAddr { /// Copies this address to a new `IpAddr::V6`. /// @@ -1222,7 +1222,7 @@ impl Ord for Ipv4Addr { } #[stable(feature = "ip_u32", since = "1.1.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for u32 { /// Uses [`Ipv4Addr::to_bits`] to convert an IPv4 address to a host byte order `u32`. #[inline] @@ -1232,7 +1232,7 @@ impl const From for u32 { } #[stable(feature = "ip_u32", since = "1.1.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for Ipv4Addr { /// Uses [`Ipv4Addr::from_bits`] to convert a host byte order `u32` into an IPv4 address. #[inline] @@ -1242,7 +1242,7 @@ impl const From for Ipv4Addr { } #[stable(feature = "from_slice_v4", since = "1.9.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From<[u8; 4]> for Ipv4Addr { /// Creates an `Ipv4Addr` from a four element byte array. /// @@ -1261,7 +1261,7 @@ impl const From<[u8; 4]> for Ipv4Addr { } #[stable(feature = "ip_from_slice", since = "1.17.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From<[u8; 4]> for IpAddr { /// Creates an `IpAddr::V4` from a four element byte array. /// @@ -2216,7 +2216,7 @@ impl Ord for Ipv6Addr { } #[stable(feature = "i128", since = "1.26.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for u128 { /// Uses [`Ipv6Addr::to_bits`] to convert an IPv6 address to a host byte order `u128`. #[inline] @@ -2225,7 +2225,7 @@ impl const From for u128 { } } #[stable(feature = "i128", since = "1.26.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for Ipv6Addr { /// Uses [`Ipv6Addr::from_bits`] to convert a host byte order `u128` to an IPv6 address. #[inline] @@ -2235,7 +2235,7 @@ impl const From for Ipv6Addr { } #[stable(feature = "ipv6_from_octets", since = "1.9.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From<[u8; 16]> for Ipv6Addr { /// Creates an `Ipv6Addr` from a sixteen element byte array. /// @@ -2263,7 +2263,7 @@ impl const From<[u8; 16]> for Ipv6Addr { } #[stable(feature = "ipv6_from_segments", since = "1.16.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From<[u16; 8]> for Ipv6Addr { /// Creates an `Ipv6Addr` from an eight element 16-bit array. /// @@ -2292,7 +2292,7 @@ impl const From<[u16; 8]> for Ipv6Addr { } #[stable(feature = "ip_from_slice", since = "1.17.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From<[u8; 16]> for IpAddr { /// Creates an `IpAddr::V6` from a sixteen element byte array. /// @@ -2320,7 +2320,7 @@ impl const From<[u8; 16]> for IpAddr { } #[stable(feature = "ip_from_slice", since = "1.17.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From<[u16; 8]> for IpAddr { /// Creates an `IpAddr::V6` from an eight element 16-bit array. /// diff --git a/library/core/src/net/socket_addr.rs b/library/core/src/net/socket_addr.rs index 69924199f99f1..e0b7112250d28 100644 --- a/library/core/src/net/socket_addr.rs +++ b/library/core/src/net/socket_addr.rs @@ -592,7 +592,7 @@ impl SocketAddrV6 { } #[stable(feature = "ip_from_ip", since = "1.16.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for SocketAddr { /// Converts a [`SocketAddrV4`] into a [`SocketAddr::V4`]. #[inline] @@ -602,7 +602,7 @@ impl const From for SocketAddr { } #[stable(feature = "ip_from_ip", since = "1.16.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for SocketAddr { /// Converts a [`SocketAddrV6`] into a [`SocketAddr::V6`]. #[inline] @@ -612,7 +612,7 @@ impl const From for SocketAddr { } #[stable(feature = "addr_from_into_ip", since = "1.17.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl> const From<(I, u16)> for SocketAddr { /// Converts a tuple struct (Into<[`IpAddr`]>, `u16`) into a [`SocketAddr`]. /// diff --git a/library/core/src/num/error.rs b/library/core/src/num/error.rs index cfedd465cab0b..6ff477487a2a0 100644 --- a/library/core/src/num/error.rs +++ b/library/core/src/num/error.rs @@ -26,7 +26,7 @@ impl Error for TryFromIntError { } #[stable(feature = "try_from", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for TryFromIntError { fn from(x: Infallible) -> TryFromIntError { match x {} @@ -34,7 +34,7 @@ impl const From for TryFromIntError { } #[unstable(feature = "never_type", issue = "35121")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for TryFromIntError { #[inline] fn from(never: !) -> TryFromIntError { diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index acfe38b7a37b5..69c6a7dbd8976 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -1378,7 +1378,7 @@ const fn from_ascii_radix_panic(radix: u32) -> ! { macro_rules! from_str_int_impl { ($signedness:ident $($int_ty:ty)+) => {$( #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const FromStr for $int_ty { type Err = ParseIntError; diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index f793602de5087..9b1d5155bc15d 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -297,7 +297,7 @@ where } #[stable(feature = "from_nonzero", since = "1.31.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From> for T where T: ZeroablePrimitive, diff --git a/library/core/src/ops/deref.rs b/library/core/src/ops/deref.rs index c2dede9fa0889..05c70302a0e1c 100644 --- a/library/core/src/ops/deref.rs +++ b/library/core/src/ops/deref.rs @@ -136,7 +136,7 @@ use crate::marker::PointeeSized; #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "Deref"] #[const_trait] -#[rustc_const_unstable(feature = "const_deref", issue = "88955")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub trait Deref: PointeeSized { /// The resulting type after dereferencing. #[stable(feature = "rust1", since = "1.0.0")] @@ -152,7 +152,7 @@ pub trait Deref: PointeeSized { } #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_deref", issue = "88955")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const Deref for &T { type Target = T; @@ -166,7 +166,7 @@ impl const Deref for &T { impl !DerefMut for &T {} #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_deref", issue = "88955")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const Deref for &mut T { type Target = T; @@ -268,7 +268,7 @@ impl const Deref for &mut T { #[doc(alias = "*")] #[stable(feature = "rust1", since = "1.0.0")] #[const_trait] -#[rustc_const_unstable(feature = "const_deref", issue = "88955")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub trait DerefMut: ~const Deref + PointeeSized { /// Mutably dereferences the value. #[stable(feature = "rust1", since = "1.0.0")] @@ -277,7 +277,7 @@ pub trait DerefMut: ~const Deref + PointeeSized { } #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_deref", issue = "88955")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const DerefMut for &mut T { fn deref_mut(&mut self) -> &mut T { self diff --git a/library/core/src/option.rs b/library/core/src/option.rs index ed070fbd22746..4a6b1f1fa84c7 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -2144,7 +2144,7 @@ const fn expect_failed(msg: &str) -> ! { ///////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_clone", issue = "142757")] impl const Clone for Option where // FIXME(const_hack): the T: ~const Destruct should be inferred from the Self: ~const Destruct in clone_from. @@ -2233,7 +2233,7 @@ impl<'a, T> IntoIterator for &'a mut Option { } #[stable(since = "1.12.0", feature = "option_from")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for Option { /// Moves `val` into a new [`Some`]. /// @@ -2250,7 +2250,7 @@ impl const From for Option { } #[stable(feature = "option_ref_from_ref_option", since = "1.30.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl<'a, T> const From<&'a Option> for Option<&'a T> { /// Converts from `&Option` to `Option<&T>`. /// @@ -2278,7 +2278,7 @@ impl<'a, T> const From<&'a Option> for Option<&'a T> { } #[stable(feature = "option_ref_from_ref_option", since = "1.30.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl<'a, T> const From<&'a mut Option> for Option<&'a mut T> { /// Converts from `&mut Option` to `Option<&mut T>` /// diff --git a/library/core/src/panic/unwind_safe.rs b/library/core/src/panic/unwind_safe.rs index a60f0799c0eae..722af55103839 100644 --- a/library/core/src/panic/unwind_safe.rs +++ b/library/core/src/panic/unwind_safe.rs @@ -248,7 +248,8 @@ impl RefUnwindSafe for crate::sync::atomic::AtomicBool {} impl RefUnwindSafe for crate::sync::atomic::AtomicPtr {} #[stable(feature = "catch_unwind", since = "1.9.0")] -impl Deref for AssertUnwindSafe { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Deref for AssertUnwindSafe { type Target = T; fn deref(&self) -> &T { @@ -257,7 +258,8 @@ impl Deref for AssertUnwindSafe { } #[stable(feature = "catch_unwind", since = "1.9.0")] -impl DerefMut for AssertUnwindSafe { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const DerefMut for AssertUnwindSafe { fn deref_mut(&mut self) -> &mut T { &mut self.0 } diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 14bf7ba90150e..f39c82df15057 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -1359,7 +1359,11 @@ impl Pin { /// ruled out by the contract of `Pin::new_unchecked`. #[stable(feature = "pin", since = "1.33.0")] #[inline(always)] - pub fn as_ref(&self) -> Pin<&Ptr::Target> { + #[rustc_const_unstable(feature = "const_from", issue = "143773")] + pub const fn as_ref(&self) -> Pin<&Ptr::Target> + where + Ptr: ~const Deref, + { // SAFETY: see documentation on this function unsafe { Pin::new_unchecked(&*self.pointer) } } @@ -1403,7 +1407,11 @@ impl Pin { /// ``` #[stable(feature = "pin", since = "1.33.0")] #[inline(always)] - pub fn as_mut(&mut self) -> Pin<&mut Ptr::Target> { + #[rustc_const_unstable(feature = "const_from", issue = "143773")] + pub const fn as_mut(&mut self) -> Pin<&mut Ptr::Target> + where + Ptr: ~const DerefMut, + { // SAFETY: see documentation on this function unsafe { Pin::new_unchecked(&mut *self.pointer) } } @@ -1418,7 +1426,11 @@ impl Pin { #[stable(feature = "pin_deref_mut", since = "1.84.0")] #[must_use = "`self` will be dropped if the result is not used"] #[inline(always)] - pub fn as_deref_mut(self: Pin<&mut Self>) -> Pin<&mut Ptr::Target> { + #[rustc_const_unstable(feature = "const_from", issue = "143773")] + pub const fn as_deref_mut(self: Pin<&mut Self>) -> Pin<&mut Ptr::Target> + where + Ptr: ~const DerefMut, + { // SAFETY: What we're asserting here is that going from // // Pin<&mut Pin> @@ -1669,7 +1681,8 @@ impl Pin<&'static mut T> { } #[stable(feature = "pin", since = "1.33.0")] -impl Deref for Pin { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl const Deref for Pin { type Target = Ptr::Target; fn deref(&self) -> &Ptr::Target { Pin::get_ref(Pin::as_ref(self)) @@ -1677,7 +1690,8 @@ impl Deref for Pin { } #[stable(feature = "pin", since = "1.33.0")] -impl> DerefMut for Pin { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl> const DerefMut for Pin { fn deref_mut(&mut self) -> &mut Ptr::Target { Pin::get_mut(Pin::as_mut(self)) } diff --git a/library/core/src/pin/unsafe_pinned.rs b/library/core/src/pin/unsafe_pinned.rs index b18b5d7c9ec0d..6a4512c4c97f3 100644 --- a/library/core/src/pin/unsafe_pinned.rs +++ b/library/core/src/pin/unsafe_pinned.rs @@ -148,7 +148,8 @@ impl Default for UnsafePinned { } #[unstable(feature = "unsafe_pinned", issue = "125735")] -impl From for UnsafePinned { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for UnsafePinned { /// Creates a new `UnsafePinned` containing the given value. fn from(value: T) -> Self { UnsafePinned::new(value) diff --git a/library/core/src/ptr/alignment.rs b/library/core/src/ptr/alignment.rs index bd5b4e21baa0f..8eeaf396cdb11 100644 --- a/library/core/src/ptr/alignment.rs +++ b/library/core/src/ptr/alignment.rs @@ -169,7 +169,8 @@ impl fmt::Debug for Alignment { } #[unstable(feature = "ptr_alignment_type", issue = "102070")] -impl TryFrom> for Alignment { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const TryFrom> for Alignment { type Error = num::TryFromIntError; #[inline] @@ -179,7 +180,8 @@ impl TryFrom> for Alignment { } #[unstable(feature = "ptr_alignment_type", issue = "102070")] -impl TryFrom for Alignment { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const TryFrom for Alignment { type Error = num::TryFromIntError; #[inline] @@ -189,7 +191,7 @@ impl TryFrom for Alignment { } #[unstable(feature = "ptr_alignment_type", issue = "102070")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for NonZero { #[inline] fn from(align: Alignment) -> NonZero { @@ -198,7 +200,7 @@ impl const From for NonZero { } #[unstable(feature = "ptr_alignment_type", issue = "102070")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for usize { #[inline] fn from(align: Alignment) -> usize { diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index 62da6567cca75..4cd4c416bea37 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -1682,7 +1682,8 @@ impl hash::Hash for NonNull { } #[unstable(feature = "ptr_internals", issue = "none")] -impl From> for NonNull { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for NonNull { #[inline] fn from(unique: Unique) -> Self { unique.as_non_null_ptr() @@ -1690,7 +1691,8 @@ impl From> for NonNull { } #[stable(feature = "nonnull", since = "1.25.0")] -impl From<&mut T> for NonNull { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From<&mut T> for NonNull { /// Converts a `&mut T` to a `NonNull`. /// /// This conversion is safe and infallible since references cannot be null. @@ -1701,7 +1703,8 @@ impl From<&mut T> for NonNull { } #[stable(feature = "nonnull", since = "1.25.0")] -impl From<&T> for NonNull { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From<&T> for NonNull { /// Converts a `&T` to a `NonNull`. /// /// This conversion is safe and infallible since references cannot be null. diff --git a/library/core/src/ptr/unique.rs b/library/core/src/ptr/unique.rs index e9e13f9e97f84..9b149bd75f93c 100644 --- a/library/core/src/ptr/unique.rs +++ b/library/core/src/ptr/unique.rs @@ -189,7 +189,8 @@ impl fmt::Pointer for Unique { } #[unstable(feature = "ptr_internals", issue = "none")] -impl From<&mut T> for Unique { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From<&mut T> for Unique { /// Converts a `&mut T` to a `Unique`. /// /// This conversion is infallible since references cannot be null. @@ -200,7 +201,8 @@ impl From<&mut T> for Unique { } #[unstable(feature = "ptr_internals", issue = "none")] -impl From> for Unique { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Unique { /// Converts a `NonNull` to a `Unique`. /// /// This conversion is infallible since `NonNull` cannot be null. diff --git a/library/core/src/range.rs b/library/core/src/range.rs index 5cd7956291ca2..e8005fd63fe73 100644 --- a/library/core/src/range.rs +++ b/library/core/src/range.rs @@ -186,7 +186,7 @@ impl IntoBounds for Range { } #[unstable(feature = "new_range_api", issue = "125687")] -#[rustc_const_unstable(feature = "const_index", issue = "143775")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From> for legacy::Range { #[inline] fn from(value: Range) -> Self { @@ -195,7 +195,7 @@ impl const From> for legacy::Range { } #[unstable(feature = "new_range_api", issue = "125687")] -#[rustc_const_unstable(feature = "const_index", issue = "143775")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From> for Range { #[inline] fn from(value: legacy::Range) -> Self { @@ -365,7 +365,7 @@ impl IntoBounds for RangeInclusive { } #[unstable(feature = "new_range_api", issue = "125687")] -#[rustc_const_unstable(feature = "const_index", issue = "143775")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From> for legacy::RangeInclusive { #[inline] fn from(value: RangeInclusive) -> Self { @@ -373,7 +373,8 @@ impl const From> for legacy::RangeInclusive { } } #[unstable(feature = "new_range_api", issue = "125687")] -impl From> for RangeInclusive { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for RangeInclusive { #[inline] fn from(value: legacy::RangeInclusive) -> Self { assert!( diff --git a/library/core/src/result.rs b/library/core/src/result.rs index f65257ff59b92..8ce80dd8283b4 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1289,7 +1289,7 @@ impl Result { #[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")] #[inline] #[rustc_allow_const_fn_unstable(const_precise_live_drops)] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub const fn into_ok(self) -> T where E: ~const Into, @@ -1326,7 +1326,7 @@ impl Result { #[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")] #[inline] #[rustc_allow_const_fn_unstable(const_precise_live_drops)] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub const fn into_err(self) -> E where T: ~const Into, diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index 33132dcc7148d..70eed5d63f8ab 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -425,7 +425,8 @@ impl<'a, T: 'a, P: FnMut(&T) -> bool> Split<'a, T, P> { /// assert_eq!(split.as_slice(), &[3,4,5]); /// ``` #[unstable(feature = "split_as_slice", issue = "96137")] - pub fn as_slice(&self) -> &'a [T] { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_slice(&self) -> &'a [T] { if self.finished { &[] } else { &self.v } } } diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 029abf1753913..40dcc4449b141 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -3066,7 +3066,8 @@ impl str { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef<[u8]> for str { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef<[u8]> for str { #[inline] fn as_ref(&self) -> &[u8] { self.as_bytes() diff --git a/library/core/src/str/traits.rs b/library/core/src/str/traits.rs index 1597d1c1fa868..bbfacabfa6986 100644 --- a/library/core/src/str/traits.rs +++ b/library/core/src/str/traits.rs @@ -826,7 +826,7 @@ unsafe impl const SliceIndex for ops::RangeToInclusive { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[const_trait] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub trait FromStr: Sized { /// The associated error which can be returned from parsing. #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/core/src/sync/atomic.rs b/library/core/src/sync/atomic.rs index 546f3d91a8099..cd7f525853115 100644 --- a/library/core/src/sync/atomic.rs +++ b/library/core/src/sync/atomic.rs @@ -2518,7 +2518,7 @@ impl AtomicPtr { #[cfg(target_has_atomic_load_store = "8")] #[stable(feature = "atomic_bool_from", since = "1.24.0")] -#[rustc_const_unstable(feature = "const_try", issue = "74935")] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From for AtomicBool { /// Converts a `bool` into an `AtomicBool`. /// @@ -2537,7 +2537,8 @@ impl const From for AtomicBool { #[cfg(target_has_atomic_load_store = "ptr")] #[stable(feature = "atomic_from", since = "1.23.0")] -impl From<*mut T> for AtomicPtr { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From<*mut T> for AtomicPtr { /// Converts a `*mut T` into an `AtomicPtr`. #[inline] fn from(p: *mut T) -> Self { @@ -2616,7 +2617,7 @@ macro_rules! atomic_int { } #[$stable_from] - #[rustc_const_unstable(feature = "const_try", issue = "74935")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] impl const From<$int_type> for $atomic_type { #[doc = concat!("Converts an `", stringify!($int_type), "` into an `", stringify!($atomic_type), "`.")] #[inline] diff --git a/library/core/src/sync/exclusive.rs b/library/core/src/sync/exclusive.rs index 340b0b79e40a3..cf086bf4f5080 100644 --- a/library/core/src/sync/exclusive.rs +++ b/library/core/src/sync/exclusive.rs @@ -163,7 +163,8 @@ impl Exclusive { } #[unstable(feature = "exclusive_wrapper", issue = "98407")] -impl From for Exclusive { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for Exclusive { #[inline] fn from(t: T) -> Self { Self::new(t) diff --git a/library/core/src/task/poll.rs b/library/core/src/task/poll.rs index ca668361ef63b..90011ba4938a3 100644 --- a/library/core/src/task/poll.rs +++ b/library/core/src/task/poll.rs @@ -215,7 +215,8 @@ impl Poll>> { } #[stable(feature = "futures_api", since = "1.36.0")] -impl From for Poll { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for Poll { /// Moves the value into a [`Poll::Ready`] to make a `Poll`. /// /// # Example diff --git a/library/core/src/task/wake.rs b/library/core/src/task/wake.rs index bb7efe582f7a3..97eb9ec7dc5b0 100644 --- a/library/core/src/task/wake.rs +++ b/library/core/src/task/wake.rs @@ -901,7 +901,8 @@ impl Clone for LocalWaker { } #[unstable(feature = "local_waker", issue = "118959")] -impl AsRef for Waker { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for Waker { fn as_ref(&self) -> &LocalWaker { // SAFETY: LocalWaker is just Waker without thread safety unsafe { transmute(self) } diff --git a/library/core/src/tuple.rs b/library/core/src/tuple.rs index 23a0a6877df77..2cdee1803a9a2 100644 --- a/library/core/src/tuple.rs +++ b/library/core/src/tuple.rs @@ -133,6 +133,7 @@ macro_rules! tuple_impls { maybe_tuple_doc! { $($T)+ @ #[stable(feature = "array_tuple_conv", since = "1.71.0")] + // can't do const From due to https://github.com/rust-lang/rust/issues/144280 impl From<[T; ${count($T)}]> for ($(${ignore($T)} T,)+) { #[inline] #[allow(non_snake_case)] @@ -146,6 +147,7 @@ macro_rules! tuple_impls { maybe_tuple_doc! { $($T)+ @ #[stable(feature = "array_tuple_conv", since = "1.71.0")] + // can't do const From due to https://github.com/rust-lang/rust/issues/144280 impl From<($(${ignore($T)} T,)+)> for [T; ${count($T)}] { #[inline] #[allow(non_snake_case)] diff --git a/library/coretests/tests/lib.rs b/library/coretests/tests/lib.rs index 4cfac9ecc2ab7..64e4cb745fdc5 100644 --- a/library/coretests/tests/lib.rs +++ b/library/coretests/tests/lib.rs @@ -16,7 +16,7 @@ #![feature(cfg_target_has_reliable_f16_f128)] #![feature(char_max_len)] #![feature(clone_to_uninit)] -#![feature(const_deref)] +#![feature(const_convert)] #![feature(const_destruct)] #![feature(const_eval_select)] #![feature(const_ops)] diff --git a/library/std/src/error.rs b/library/std/src/error.rs index def5f984c88e4..36bfc0e8ce54d 100644 --- a/library/std/src/error.rs +++ b/library/std/src/error.rs @@ -507,7 +507,8 @@ where } #[unstable(feature = "error_reporter", issue = "90172")] -impl From for Report +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for Report where E: Error, { diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index 8d7edc732aff3..7db096c650129 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -200,8 +200,17 @@ impl OsString { #[stable(feature = "rust1", since = "1.0.0")] #[must_use] #[inline] - pub fn as_os_str(&self) -> &OsStr { - self + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_os_str(&self) -> &OsStr { + OsStr::from_inner(self.inner.as_slice()) + } + + /// Converts to a mutable [`OsStr`] slice. + #[must_use] + #[inline] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const fn as_mut_os_str(&mut self) -> &mut OsStr { + OsStr::from_inner_mut(self.inner.as_mut_slice()) } /// Converts the `OsString` into a byte vector. To convert the byte vector back into an @@ -551,8 +560,8 @@ impl OsString { #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "into_boxed_os_str", since = "1.20.0")] pub fn into_boxed_os_str(self) -> Box { - let rw = Box::into_raw(self.inner.into_box()) as *mut OsStr; - unsafe { Box::from_raw(rw) } + let (rw, alloc) = Box::into_raw_with_allocator(self.inner.into_box()); + unsafe { Box::from_raw_in(rw as *mut OsStr, alloc) } } /// Consumes and leaks the `OsString`, returning a mutable reference to the contents, @@ -606,7 +615,8 @@ impl OsString { } #[stable(feature = "rust1", since = "1.0.0")] -impl From for OsString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for OsString { /// Converts a [`String`] into an [`OsString`]. /// /// This conversion does not allocate or copy memory. @@ -654,7 +664,7 @@ impl ops::Index for OsString { #[inline] fn index(&self, _index: ops::RangeFull) -> &OsStr { - OsStr::from_inner(self.inner.as_slice()) + &**self } } @@ -662,25 +672,27 @@ impl ops::Index for OsString { impl ops::IndexMut for OsString { #[inline] fn index_mut(&mut self, _index: ops::RangeFull) -> &mut OsStr { - OsStr::from_inner_mut(self.inner.as_mut_slice()) + &mut **self } } #[stable(feature = "rust1", since = "1.0.0")] -impl ops::Deref for OsString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const ops::Deref for OsString { type Target = OsStr; #[inline] fn deref(&self) -> &OsStr { - &self[..] + self.as_os_str() } } #[stable(feature = "mut_osstr", since = "1.44.0")] -impl ops::DerefMut for OsString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const ops::DerefMut for OsString { #[inline] fn deref_mut(&mut self) -> &mut OsStr { - &mut self[..] + self.as_mut_os_str() } } @@ -828,7 +840,8 @@ impl OsStr { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn new + ?Sized>(s: &S) -> &OsStr { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn new + ?Sized>(s: &S) -> &OsStr { s.as_ref() } @@ -876,14 +889,16 @@ impl OsStr { } #[inline] - fn from_inner(inner: &Slice) -> &OsStr { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const fn from_inner(inner: &Slice) -> &OsStr { // SAFETY: OsStr is just a wrapper of Slice, // therefore converting &Slice to &OsStr is safe. unsafe { &*(inner as *const Slice as *const OsStr) } } #[inline] - fn from_inner_mut(inner: &mut Slice) -> &mut OsStr { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const fn from_inner_mut(inner: &mut Slice) -> &mut OsStr { // SAFETY: OsStr is just a wrapper of Slice, // therefore converting &mut Slice to &mut OsStr is safe. // Any method that mutates OsStr must be careful not to @@ -1041,8 +1056,10 @@ impl OsStr { /// Converts a [Box]<[OsStr]> into an [`OsString`] without copying or allocating. #[stable(feature = "into_boxed_os_str", since = "1.20.0")] #[must_use = "`self` will be dropped if the result is not used"] - pub fn into_os_string(self: Box) -> OsString { - let boxed = unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) }; + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_os_string(self: Box) -> OsString { + let (raw, alloc) = Box::into_raw_with_allocator(self); + let boxed = unsafe { Box::from_raw_in(raw as *mut Slice, alloc) }; OsString { inner: Buf::from_box(boxed) } } @@ -1308,7 +1325,8 @@ impl From> for Box { } #[stable(feature = "os_string_from_box", since = "1.18.0")] -impl From> for OsString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for OsString { /// Converts a [Box]<[OsStr]> into an [`OsString`] without copying or /// allocating. #[inline] @@ -1405,7 +1423,8 @@ impl From<&mut OsStr> for Rc { } #[stable(feature = "cow_from_osstr", since = "1.28.0")] -impl<'a> From for Cow<'a, OsStr> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From for Cow<'a, OsStr> { /// Moves the string into a [`Cow::Owned`]. #[inline] fn from(s: OsString) -> Cow<'a, OsStr> { @@ -1414,7 +1433,8 @@ impl<'a> From for Cow<'a, OsStr> { } #[stable(feature = "cow_from_osstr", since = "1.28.0")] -impl<'a> From<&'a OsStr> for Cow<'a, OsStr> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From<&'a OsStr> for Cow<'a, OsStr> { /// Converts the string reference into a [`Cow::Borrowed`]. #[inline] fn from(s: &'a OsStr) -> Cow<'a, OsStr> { @@ -1423,7 +1443,8 @@ impl<'a> From<&'a OsStr> for Cow<'a, OsStr> { } #[stable(feature = "cow_from_osstr", since = "1.28.0")] -impl<'a> From<&'a OsString> for Cow<'a, OsStr> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From<&'a OsString> for Cow<'a, OsStr> { /// Converts the string reference into a [`Cow::Borrowed`]. #[inline] fn from(s: &'a OsString) -> Cow<'a, OsStr> { @@ -1442,7 +1463,8 @@ impl<'a> From> for OsString { } #[stable(feature = "str_tryfrom_osstr_impl", since = "1.72.0")] -impl<'a> TryFrom<&'a OsStr> for &'a str { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const TryFrom<&'a OsStr> for &'a str { type Error = crate::str::Utf8Error; /// Tries to convert an `&OsStr` to a `&str`. @@ -1660,10 +1682,11 @@ impl> alloc::slice::Join<&OsStr> for [S] { } #[stable(feature = "rust1", since = "1.0.0")] -impl Borrow for OsString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow for OsString { #[inline] fn borrow(&self) -> &OsStr { - &self[..] + &**self } } @@ -1681,7 +1704,8 @@ impl ToOwned for OsStr { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for OsStr { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for OsStr { #[inline] fn as_ref(&self) -> &OsStr { self @@ -1689,7 +1713,8 @@ impl AsRef for OsStr { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for OsString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for OsString { #[inline] fn as_ref(&self) -> &OsStr { self @@ -1697,7 +1722,8 @@ impl AsRef for OsString { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for str { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for str { #[inline] fn as_ref(&self) -> &OsStr { OsStr::from_inner(Slice::from_str(self)) @@ -1705,7 +1731,8 @@ impl AsRef for str { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for String { #[inline] fn as_ref(&self) -> &OsStr { (&**self).as_ref() diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 72ad7c244eeba..f063fbe82921e 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -412,7 +412,8 @@ impl fmt::Display for TryLockError { } #[stable(feature = "file_lock", since = "1.89.0")] -impl From for io::Error { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for io::Error { fn from(err: TryLockError) -> io::Error { match err { TryLockError::Error(err) => err, diff --git a/library/std/src/io/buffered/mod.rs b/library/std/src/io/buffered/mod.rs index 475d877528f7f..0a752024e3119 100644 --- a/library/std/src/io/buffered/mod.rs +++ b/library/std/src/io/buffered/mod.rs @@ -15,7 +15,8 @@ use linewritershim::LineWriterShim; #[stable(feature = "rust1", since = "1.0.0")] pub use self::{bufreader::BufReader, bufwriter::BufWriter, linewriter::LineWriter}; use crate::io::Error; -use crate::{error, fmt}; +use crate::marker::Destruct; +use crate::{error, fmt, mem, ptr}; /// An error returned by [`BufWriter::into_inner`] which combines an error that /// happened while writing out the buffer, and the buffered writer object @@ -172,9 +173,17 @@ impl IntoInnerError { } #[stable(feature = "rust1", since = "1.0.0")] -impl From> for Error { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for Error { fn from(iie: IntoInnerError) -> Error { - iie.1 + // FIXME: rustc_allow_const_fn_unstable doesn't work for const traits, + // need const_precise_live_drops + let iie = mem::ManuallyDrop::new(iie); + + // drop `W` + unsafe { ptr::read(&iie.0) }; + + unsafe { ptr::read(&iie.1) } } } diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs index 562fdbf4ff76d..2f01542ddfa96 100644 --- a/library/std/src/io/error.rs +++ b/library/std/src/io/error.rs @@ -106,7 +106,8 @@ impl From for Error { } #[stable(feature = "io_error_from_try_reserve", since = "1.78.0")] -impl From for Error { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for Error { /// Converts `TryReserveError` to an error with [`ErrorKind::OutOfMemory`]. /// /// `TryReserveError` won't be available as the error `source()`, @@ -217,7 +218,8 @@ struct Custom { /// tests more robust. In particular, if you want to verify that your code does /// produce an unrecognized error kind, the robust solution is to check for all /// the recognized error kinds and fail in those cases. -#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialOrd)] +#[derive_const(PartialEq)] #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "io_errorkind")] #[allow(deprecated)] @@ -516,7 +518,8 @@ impl fmt::Display for ErrorKind { /// Intended for use for errors not exposed to the user, where allocating onto /// the heap (for normal construction via Error::new) is too costly. #[stable(feature = "io_error_from_errorkind", since = "1.14.0")] -impl From for Error { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for Error { /// Converts an [`ErrorKind`] into an [`Error`]. /// /// This conversion creates a new error with a simple representation of error kind. diff --git a/library/std/src/io/error/repr_bitpacked.rs b/library/std/src/io/error/repr_bitpacked.rs index 716da37168d01..c4370bee18789 100644 --- a/library/std/src/io/error/repr_bitpacked.rs +++ b/library/std/src/io/error/repr_bitpacked.rs @@ -190,20 +190,13 @@ impl Repr { } #[inline] - pub(super) fn new_simple(kind: ErrorKind) -> Self { + pub(super) const fn new_simple(kind: ErrorKind) -> Self { let utagged = ((kind as usize) << 32) | TAG_SIMPLE; // Safety: `TAG_SIMPLE` is not zero, so the result of the `|` is not 0. let res = Self( NonNull::without_provenance(unsafe { NonZeroUsize::new_unchecked(utagged) }), PhantomData, ); - // quickly smoke-check we encoded the right thing (This generally will - // only run in std's tests, unless the user uses -Zbuild-std) - debug_assert!( - matches!(res.data(), ErrorData::Simple(k) if k == kind), - "repr(simple) encoding failed {:?}", - kind, - ); res } diff --git a/library/std/src/io/error/repr_unpacked.rs b/library/std/src/io/error/repr_unpacked.rs index dc8a95577c959..5bec9dfc062ba 100644 --- a/library/std/src/io/error/repr_unpacked.rs +++ b/library/std/src/io/error/repr_unpacked.rs @@ -21,7 +21,7 @@ impl Repr { Self(Inner::Os(code)) } #[inline] - pub(super) fn new_simple(kind: ErrorKind) -> Self { + pub(super) const fn new_simple(kind: ErrorKind) -> Self { Self(Inner::Simple(kind)) } #[inline] diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index d351ee5e739d3..0be8416f7cac6 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1449,7 +1449,8 @@ impl<'a> IoSliceMut<'a> { } #[stable(feature = "iovec", since = "1.36.0")] -impl<'a> Deref for IoSliceMut<'a> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const Deref for IoSliceMut<'a> { type Target = [u8]; #[inline] @@ -1459,7 +1460,8 @@ impl<'a> Deref for IoSliceMut<'a> { } #[stable(feature = "iovec", since = "1.36.0")] -impl<'a> DerefMut for IoSliceMut<'a> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const DerefMut for IoSliceMut<'a> { #[inline] fn deref_mut(&mut self) -> &mut [u8] { self.0.as_mut_slice() @@ -1611,13 +1613,15 @@ impl<'a> IoSlice<'a> { /// assert_eq!(io_slice.as_slice(), b"def"); /// ``` #[unstable(feature = "io_slice_as_bytes", issue = "132818")] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub const fn as_slice(self) -> &'a [u8] { self.0.as_slice() } } #[stable(feature = "iovec", since = "1.36.0")] -impl<'a> Deref for IoSlice<'a> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const Deref for IoSlice<'a> { type Target = [u8]; #[inline] diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 323742a75b055..cd88703c8e47c 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -281,9 +281,12 @@ #![feature(cfg_target_thread_local)] #![feature(cfi_encoding)] #![feature(char_max_len)] +#![feature(const_precise_live_drops)] +#![feature(const_trait_impl)] #![feature(core_float_math)] #![feature(decl_macro)] #![feature(deprecated_suggestion)] +#![feature(derive_const)] #![feature(doc_cfg)] #![feature(doc_cfg_hide)] #![feature(doc_masked)] @@ -329,6 +332,11 @@ #![feature(bstr_internals)] #![feature(char_internals)] #![feature(clone_to_uninit)] +#![feature(const_clone)] +#![feature(const_cmp)] +#![feature(const_convert)] +#![feature(const_convert_methods)] +#![feature(const_destruct)] #![feature(core_intrinsics)] #![feature(core_io_borrowed_buf)] #![feature(duration_constants)] diff --git a/library/std/src/path.rs b/library/std/src/path.rs index d9c34d4fa0451..c0c24cc93fa36 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -75,12 +75,11 @@ use crate::error::Error; use crate::ffi::{OsStr, OsString, os_str}; use crate::hash::{Hash, Hasher}; use crate::iter::FusedIterator; -use crate::ops::{self, Deref}; use crate::rc::Rc; use crate::str::FromStr; use crate::sync::Arc; use crate::sys::path::{MAIN_SEP_STR, is_sep_byte, is_verbatim_sep, parse_prefix}; -use crate::{cmp, fmt, fs, io, sys}; +use crate::{cmp, fmt, fs, io, ops, sys}; //////////////////////////////////////////////////////////////////////////////// // GENERAL NOTES @@ -439,7 +438,8 @@ impl<'a> PrefixComponent<'a> { #[stable(feature = "rust1", since = "1.0.0")] #[must_use] #[inline] - pub fn as_os_str(&self) -> &'a OsStr { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn as_os_str(&self) -> &'a OsStr { self.raw } } @@ -545,7 +545,8 @@ impl<'a> Component<'a> { /// ``` #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "rust1", since = "1.0.0")] - pub fn as_os_str(self) -> &'a OsStr { + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn as_os_str(self) -> &'a OsStr { match self { Component::Prefix(p) => p.as_os_str(), Component::RootDir => OsStr::new(MAIN_SEP_STR), @@ -557,7 +558,8 @@ impl<'a> Component<'a> { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for Component<'_> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for Component<'_> { #[inline] fn as_ref(&self) -> &OsStr { self.as_os_str() @@ -565,7 +567,8 @@ impl AsRef for Component<'_> { } #[stable(feature = "path_component_asref", since = "1.25.0")] -impl AsRef for Component<'_> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for Component<'_> { #[inline] fn as_ref(&self) -> &Path { self.as_os_str().as_ref() @@ -1110,7 +1113,7 @@ impl FusedIterator for Ancestors<'_> {} /// An owned, mutable path (akin to [`String`]). /// /// This type provides methods like [`push`] and [`set_extension`] that mutate -/// the path in place. It also implements [`Deref`] to [`Path`], meaning that +/// the path in place. It also implements [`Deref`][ops::Deref] to [`Path`], meaning that /// all methods on [`Path`] slices are available on `PathBuf` values as well. /// /// [`push`]: PathBuf::push @@ -1235,8 +1238,17 @@ impl PathBuf { #[stable(feature = "rust1", since = "1.0.0")] #[must_use] #[inline] - pub fn as_path(&self) -> &Path { - self + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn as_path(&self) -> &Path { + Path::new(&self.inner) + } + + /// Coerces to a mutable [`Path`] slice. + #[must_use] + #[inline] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const fn as_mut_path(&mut self) -> &mut Path { + Path::from_inner_mut(&mut self.inner) } /// Consumes and leaks the `PathBuf`, returning a mutable reference to the contents, @@ -1673,8 +1685,8 @@ impl PathBuf { #[must_use = "`self` will be dropped if the result is not used"] #[inline] pub fn into_boxed_path(self) -> Box { - let rw = Box::into_raw(self.inner.into_boxed_os_str()) as *mut Path; - unsafe { Box::from_raw(rw) } + let (raw, alloc) = Box::into_raw_with_allocator(self.inner.into_boxed_os_str()); + unsafe { Box::from_raw_in(raw as *mut Path, alloc) } } /// Invokes [`capacity`] on the underlying instance of [`OsString`]. @@ -1805,7 +1817,8 @@ impl From> for Box { } #[stable(feature = "path_buf_from_box", since = "1.18.0")] -impl From> for PathBuf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for PathBuf { /// Converts a [Box]<[Path]> into a [`PathBuf`]. /// /// This conversion does not allocate or copy memory. @@ -1847,7 +1860,8 @@ impl> From<&T> for PathBuf { } #[stable(feature = "rust1", since = "1.0.0")] -impl From for PathBuf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for PathBuf { /// Converts an [`OsString`] into a [`PathBuf`]. /// /// This conversion does not allocate or copy memory. @@ -1858,7 +1872,8 @@ impl From for PathBuf { } #[stable(feature = "from_path_buf_for_os_string", since = "1.14.0")] -impl From for OsString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for OsString { /// Converts a [`PathBuf`] into an [`OsString`] /// /// This conversion does not allocate or copy memory. @@ -1869,7 +1884,8 @@ impl From for OsString { } #[stable(feature = "rust1", since = "1.0.0")] -impl From for PathBuf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for PathBuf { /// Converts a [`String`] into a [`PathBuf`] /// /// This conversion does not allocate or copy memory. @@ -1945,27 +1961,30 @@ impl fmt::Debug for PathBuf { } #[stable(feature = "rust1", since = "1.0.0")] -impl ops::Deref for PathBuf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const ops::Deref for PathBuf { type Target = Path; #[inline] fn deref(&self) -> &Path { - Path::new(&self.inner) + self.as_path() } } #[stable(feature = "path_buf_deref_mut", since = "1.68.0")] -impl ops::DerefMut for PathBuf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const ops::DerefMut for PathBuf { #[inline] fn deref_mut(&mut self) -> &mut Path { - Path::from_inner_mut(&mut self.inner) + self.as_mut_path() } } #[stable(feature = "rust1", since = "1.0.0")] -impl Borrow for PathBuf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Borrow for PathBuf { #[inline] fn borrow(&self) -> &Path { - self.deref() + self.as_path() } } @@ -1978,7 +1997,8 @@ impl Default for PathBuf { } #[stable(feature = "cow_from_path", since = "1.6.0")] -impl<'a> From<&'a Path> for Cow<'a, Path> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From<&'a Path> for Cow<'a, Path> { /// Creates a clone-on-write pointer from a reference to /// [`Path`]. /// @@ -1990,7 +2010,8 @@ impl<'a> From<&'a Path> for Cow<'a, Path> { } #[stable(feature = "cow_from_path", since = "1.6.0")] -impl<'a> From for Cow<'a, Path> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From for Cow<'a, Path> { /// Creates a clone-on-write pointer from an owned /// instance of [`PathBuf`]. /// @@ -2002,7 +2023,8 @@ impl<'a> From for Cow<'a, Path> { } #[stable(feature = "cow_from_pathbuf_ref", since = "1.28.0")] -impl<'a> From<&'a PathBuf> for Cow<'a, Path> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl<'a> const From<&'a PathBuf> for Cow<'a, Path> { /// Creates a clone-on-write pointer from a reference to /// [`PathBuf`]. /// @@ -2132,10 +2154,11 @@ impl Ord for PathBuf { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for PathBuf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for PathBuf { #[inline] fn as_ref(&self) -> &OsStr { - &self.inner[..] + &self.inner } } @@ -2232,11 +2255,13 @@ impl Path { /// assert_eq!(from_string, from_path); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn new + ?Sized>(s: &S) -> &Path { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn new + ?Sized>(s: &S) -> &Path { unsafe { &*(s.as_ref() as *const OsStr as *const Path) } } - fn from_inner_mut(inner: &mut OsStr) -> &mut Path { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const fn from_inner_mut(inner: &mut OsStr) -> &mut Path { // SAFETY: Path is just a wrapper around OsStr, // therefore converting &mut OsStr to &mut Path is safe. unsafe { &mut *(inner as *mut OsStr as *mut Path) } @@ -3285,9 +3310,10 @@ impl Path { /// allocating. #[stable(feature = "into_boxed_path", since = "1.20.0")] #[must_use = "`self` will be dropped if the result is not used"] - pub fn into_path_buf(self: Box) -> PathBuf { - let rw = Box::into_raw(self) as *mut OsStr; - let inner = unsafe { Box::from_raw(rw) }; + #[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")] + pub const fn into_path_buf(self: Box) -> PathBuf { + let (raw, alloc) = Box::into_raw_with_allocator(self); + let inner = unsafe { Box::from_raw_in(raw as *mut OsStr, alloc) }; PathBuf { inner: OsString::from(inner) } } } @@ -3303,7 +3329,8 @@ unsafe impl CloneToUninit for Path { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for Path { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for Path { #[inline] fn as_ref(&self) -> &OsStr { &self.inner @@ -3440,7 +3467,8 @@ impl Ord for Path { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for Path { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for Path { #[inline] fn as_ref(&self) -> &Path { self @@ -3448,7 +3476,8 @@ impl AsRef for Path { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for OsStr { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for OsStr { #[inline] fn as_ref(&self) -> &Path { Path::new(self) @@ -3456,7 +3485,8 @@ impl AsRef for OsStr { } #[stable(feature = "cow_os_str_as_ref_path", since = "1.8.0")] -impl AsRef for Cow<'_, OsStr> { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for Cow<'_, OsStr> { #[inline] fn as_ref(&self) -> &Path { Path::new(self) @@ -3464,7 +3494,8 @@ impl AsRef for Cow<'_, OsStr> { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for OsString { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for OsString { #[inline] fn as_ref(&self) -> &Path { Path::new(self) @@ -3472,7 +3503,8 @@ impl AsRef for OsString { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for str { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for str { #[inline] fn as_ref(&self) -> &Path { Path::new(self) @@ -3480,7 +3512,8 @@ impl AsRef for str { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for String { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for String { #[inline] fn as_ref(&self) -> &Path { Path::new(self) @@ -3488,7 +3521,8 @@ impl AsRef for String { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef for PathBuf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsRef for PathBuf { #[inline] fn as_ref(&self) -> &Path { self diff --git a/library/std/src/process.rs b/library/std/src/process.rs index 373584d0117ce..eb0e4143313ce 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -1996,7 +1996,8 @@ impl ExitStatusError { } #[unstable(feature = "exit_status_error", issue = "84908")] -impl From for ExitStatus { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for ExitStatus { fn from(error: ExitStatusError) -> Self { Self(error.0.into()) } @@ -2161,7 +2162,8 @@ impl Default for ExitCode { } #[stable(feature = "process_exitcode", since = "1.61.0")] -impl From for ExitCode { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for ExitCode { /// Constructs an `ExitCode` from an arbitrary u8 value. fn from(code: u8) -> Self { ExitCode(imp::ExitCode::from(code)) diff --git a/library/std/src/sync/mpmc/error.rs b/library/std/src/sync/mpmc/error.rs index e34b56d08312b..52e8889aa2445 100644 --- a/library/std/src/sync/mpmc/error.rs +++ b/library/std/src/sync/mpmc/error.rs @@ -40,7 +40,8 @@ impl fmt::Display for SendTimeoutError { impl error::Error for SendTimeoutError {} #[unstable(feature = "mpmc_channel", issue = "126840")] -impl From> for SendTimeoutError { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for SendTimeoutError { fn from(err: SendError) -> SendTimeoutError { match err { SendError(e) => SendTimeoutError::Disconnected(e), diff --git a/library/std/src/sync/mpsc.rs b/library/std/src/sync/mpsc.rs index 41d1dd3ce674b..4f8228939f9dd 100644 --- a/library/std/src/sync/mpsc.rs +++ b/library/std/src/sync/mpsc.rs @@ -1143,7 +1143,8 @@ impl error::Error for TrySendError { } #[stable(feature = "mpsc_error_conversions", since = "1.24.0")] -impl From> for TrySendError { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for TrySendError { /// Converts a `SendError` into a `TrySendError`. /// /// This conversion always returns a `TrySendError::Disconnected` containing the data in the `SendError`. @@ -1193,7 +1194,8 @@ impl error::Error for TryRecvError { } #[stable(feature = "mpsc_error_conversions", since = "1.24.0")] -impl From for TryRecvError { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for TryRecvError { /// Converts a `RecvError` into a `TryRecvError`. /// /// This conversion always returns `TryRecvError::Disconnected`. @@ -1228,7 +1230,8 @@ impl error::Error for RecvTimeoutError { } #[stable(feature = "mpsc_error_conversions", since = "1.24.0")] -impl From for RecvTimeoutError { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for RecvTimeoutError { /// Converts a `RecvError` into a `RecvTimeoutError`. /// /// This conversion always returns `RecvTimeoutError::Disconnected`. diff --git a/library/std/src/sync/poison.rs b/library/std/src/sync/poison.rs index 0c05f152ef84c..f48eb67bb724a 100644 --- a/library/std/src/sync/poison.rs +++ b/library/std/src/sync/poison.rs @@ -339,7 +339,8 @@ impl PoisonError { } #[stable(feature = "rust1", since = "1.0.0")] -impl From> for TryLockError { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From> for TryLockError { fn from(err: PoisonError) -> TryLockError { TryLockError::Poisoned(err) } diff --git a/library/std/src/sync/poison/mutex.rs b/library/std/src/sync/poison/mutex.rs index 30325be685c32..dbc1d0ace631c 100644 --- a/library/std/src/sync/poison/mutex.rs +++ b/library/std/src/sync/poison/mutex.rs @@ -622,7 +622,8 @@ impl Mutex { } #[stable(feature = "mutex_from", since = "1.24.0")] -impl From for Mutex { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for Mutex { /// Creates a new mutex in an unlocked state ready for use. /// This is equivalent to [`Mutex::new`]. fn from(t: T) -> Self { diff --git a/library/std/src/sync/poison/rwlock.rs b/library/std/src/sync/poison/rwlock.rs index 934a173425a81..11570ccc9c67e 100644 --- a/library/std/src/sync/poison/rwlock.rs +++ b/library/std/src/sync/poison/rwlock.rs @@ -676,7 +676,8 @@ impl Default for RwLock { } #[stable(feature = "rw_lock_from", since = "1.24.0")] -impl From for RwLock { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for RwLock { /// Creates a new instance of an `RwLock` which is unlocked. /// This is equivalent to [`RwLock::new`]. fn from(t: T) -> Self { diff --git a/library/std/src/sync/reentrant_lock.rs b/library/std/src/sync/reentrant_lock.rs index 727252f03a24e..56a88e977388c 100644 --- a/library/std/src/sync/reentrant_lock.rs +++ b/library/std/src/sync/reentrant_lock.rs @@ -388,7 +388,8 @@ impl Default for ReentrantLock { } #[unstable(feature = "reentrant_lock", issue = "121440")] -impl From for ReentrantLock { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for ReentrantLock { fn from(t: T) -> Self { Self::new(t) } diff --git a/library/std/src/sys/io/io_slice/iovec.rs b/library/std/src/sys/io/io_slice/iovec.rs index df56358969a39..9623b7743f562 100644 --- a/library/std/src/sys/io/io_slice/iovec.rs +++ b/library/std/src/sys/io/io_slice/iovec.rs @@ -38,6 +38,7 @@ impl<'a> IoSlice<'a> { } #[inline] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub const fn as_slice(&self) -> &'a [u8] { unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) } } @@ -71,7 +72,8 @@ impl<'a> IoSliceMut<'a> { } #[inline] - pub fn as_slice(&self) -> &[u8] { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_slice(&self) -> &[u8] { unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) } } @@ -81,7 +83,8 @@ impl<'a> IoSliceMut<'a> { } #[inline] - pub fn as_mut_slice(&mut self) -> &mut [u8] { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_mut_slice(&mut self) -> &mut [u8] { unsafe { slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len) } } } diff --git a/library/std/src/sys/io/io_slice/unsupported.rs b/library/std/src/sys/io/io_slice/unsupported.rs index 1572cac6cd771..f65bb4e0260f3 100644 --- a/library/std/src/sys/io/io_slice/unsupported.rs +++ b/library/std/src/sys/io/io_slice/unsupported.rs @@ -15,6 +15,7 @@ impl<'a> IoSlice<'a> { } #[inline] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub const fn as_slice(&self) -> &'a [u8] { self.0 } @@ -36,7 +37,8 @@ impl<'a> IoSliceMut<'a> { } #[inline] - pub fn as_slice(&self) -> &[u8] { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_slice(&self) -> &[u8] { self.0 } @@ -46,7 +48,8 @@ impl<'a> IoSliceMut<'a> { } #[inline] - pub fn as_mut_slice(&mut self) -> &mut [u8] { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_mut_slice(&mut self) -> &mut [u8] { self.0 } } diff --git a/library/std/src/sys/io/io_slice/wasi.rs b/library/std/src/sys/io/io_slice/wasi.rs index 87acbbd924e56..3a9a43871edf9 100644 --- a/library/std/src/sys/io/io_slice/wasi.rs +++ b/library/std/src/sys/io/io_slice/wasi.rs @@ -27,6 +27,7 @@ impl<'a> IoSlice<'a> { } #[inline] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub const fn as_slice(&self) -> &'a [u8] { unsafe { slice::from_raw_parts(self.vec.buf as *const u8, self.vec.buf_len) } } @@ -60,7 +61,8 @@ impl<'a> IoSliceMut<'a> { } #[inline] - pub fn as_slice(&self) -> &[u8] { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_slice(&self) -> &[u8] { unsafe { slice::from_raw_parts(self.vec.buf as *const u8, self.vec.buf_len) } } @@ -70,7 +72,8 @@ impl<'a> IoSliceMut<'a> { } #[inline] - pub fn as_mut_slice(&mut self) -> &mut [u8] { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_mut_slice(&mut self) -> &mut [u8] { unsafe { slice::from_raw_parts_mut(self.vec.buf as *mut u8, self.vec.buf_len) } } } diff --git a/library/std/src/sys/io/io_slice/windows.rs b/library/std/src/sys/io/io_slice/windows.rs index c3d8ec87c19e3..d40c522eaf6ac 100644 --- a/library/std/src/sys/io/io_slice/windows.rs +++ b/library/std/src/sys/io/io_slice/windows.rs @@ -32,6 +32,7 @@ impl<'a> IoSlice<'a> { } #[inline] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub const fn as_slice(&self) -> &'a [u8] { unsafe { slice::from_raw_parts(self.vec.buf, self.vec.len as usize) } } @@ -66,7 +67,8 @@ impl<'a> IoSliceMut<'a> { } #[inline] - pub fn as_slice(&self) -> &[u8] { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_slice(&self) -> &[u8] { unsafe { slice::from_raw_parts(self.vec.buf, self.vec.len as usize) } } @@ -76,7 +78,8 @@ impl<'a> IoSliceMut<'a> { } #[inline] - pub fn as_mut_slice(&mut self) -> &mut [u8] { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_mut_slice(&mut self) -> &mut [u8] { unsafe { slice::from_raw_parts_mut(self.vec.buf, self.vec.len as usize) } } } diff --git a/library/std/src/sys/os_str/bytes.rs b/library/std/src/sys/os_str/bytes.rs index f8ab4543a3a52..49271e578dcbd 100644 --- a/library/std/src/sys/os_str/bytes.rs +++ b/library/std/src/sys/os_str/bytes.rs @@ -31,13 +31,15 @@ impl IntoInner> for Buf { } } -impl FromInner> for Buf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const FromInner> for Buf { fn from_inner(inner: Vec) -> Self { Buf { inner } } } -impl AsInner<[u8]> for Buf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsInner<[u8]> for Buf { #[inline] fn as_inner(&self) -> &[u8] { &self.inner @@ -115,6 +117,7 @@ impl Buf { } #[inline] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub const fn from_string(s: String) -> Buf { Buf { inner: s.into_bytes() } } @@ -175,7 +178,8 @@ impl Buf { } #[inline] - pub fn as_slice(&self) -> &Slice { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_slice(&self) -> &Slice { // SAFETY: Slice just wraps [u8], // and &*self.inner is &[u8], therefore // transmuting &[u8] to &Slice is safe. @@ -183,7 +187,8 @@ impl Buf { } #[inline] - pub fn as_mut_slice(&mut self) -> &mut Slice { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_mut_slice(&mut self) -> &mut Slice { // SAFETY: Slice just wraps [u8], // and &mut *self.inner is &mut [u8], therefore // transmuting &mut [u8] to &mut Slice is safe. @@ -201,7 +206,8 @@ impl Buf { } #[inline] - pub fn from_box(boxed: Box) -> Buf { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn from_box(boxed: Box) -> Buf { let inner: Box<[u8]> = unsafe { mem::transmute(boxed) }; Buf { inner: inner.into_vec() } } @@ -247,7 +253,8 @@ impl Slice { } #[inline] - pub unsafe fn from_encoded_bytes_unchecked(s: &[u8]) -> &Slice { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const unsafe fn from_encoded_bytes_unchecked(s: &[u8]) -> &Slice { unsafe { mem::transmute(s) } } @@ -295,12 +302,14 @@ impl Slice { } #[inline] - pub fn from_str(s: &str) -> &Slice { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn from_str(s: &str) -> &Slice { unsafe { Slice::from_encoded_bytes_unchecked(s.as_bytes()) } } #[inline] - pub fn to_str(&self) -> Result<&str, crate::str::Utf8Error> { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn to_str(&self) -> Result<&str, crate::str::Utf8Error> { str::from_utf8(&self.inner) } diff --git a/library/std/src/sys/os_str/wtf8.rs b/library/std/src/sys/os_str/wtf8.rs index bbc704ebf8697..d02416e70f7fc 100644 --- a/library/std/src/sys/os_str/wtf8.rs +++ b/library/std/src/sys/os_str/wtf8.rs @@ -8,7 +8,7 @@ use crate::rc::Rc; use crate::sync::Arc; use crate::sys_common::wtf8::{Wtf8, Wtf8Buf, check_utf8_boundary}; use crate::sys_common::{AsInner, FromInner, IntoInner}; -use crate::{fmt, mem}; +use crate::{fmt, mem, ptr}; #[derive(Hash)] pub struct Buf { @@ -20,19 +20,24 @@ pub struct Slice { pub inner: Wtf8, } -impl IntoInner for Buf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const IntoInner for Buf { fn into_inner(self) -> Wtf8Buf { - self.inner + // FIXME: const Destruct + let buf = mem::ManuallyDrop::new(self); + unsafe { ptr::read(&buf.inner) } } } -impl FromInner for Buf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const FromInner for Buf { fn from_inner(inner: Wtf8Buf) -> Self { Buf { inner } } } -impl AsInner for Buf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const AsInner for Buf { #[inline] fn as_inner(&self) -> &Wtf8 { &self.inner @@ -92,6 +97,7 @@ impl Buf { } #[inline] + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub const fn from_string(s: String) -> Buf { Buf { inner: Wtf8Buf::from_string(s) } } @@ -152,7 +158,8 @@ impl Buf { } #[inline] - pub fn as_slice(&self) -> &Slice { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_slice(&self) -> &Slice { // SAFETY: Slice is just a wrapper for Wtf8, // and self.inner.as_slice() returns &Wtf8. // Therefore, transmuting &Wtf8 to &Slice is safe. @@ -160,7 +167,8 @@ impl Buf { } #[inline] - pub fn as_mut_slice(&mut self) -> &mut Slice { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_mut_slice(&mut self) -> &mut Slice { // SAFETY: Slice is just a wrapper for Wtf8, // and self.inner.as_mut_slice() returns &mut Wtf8. // Therefore, transmuting &mut Wtf8 to &mut Slice is safe. @@ -180,7 +188,8 @@ impl Buf { } #[inline] - pub fn from_box(boxed: Box) -> Buf { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn from_box(boxed: Box) -> Buf { let inner: Box = unsafe { mem::transmute(boxed) }; Buf { inner: Wtf8Buf::from_box(inner) } } @@ -231,7 +240,8 @@ impl Slice { } #[inline] - pub unsafe fn from_encoded_bytes_unchecked(s: &[u8]) -> &Slice { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const unsafe fn from_encoded_bytes_unchecked(s: &[u8]) -> &Slice { unsafe { mem::transmute(Wtf8::from_bytes_unchecked(s)) } } @@ -242,12 +252,14 @@ impl Slice { } #[inline] - pub fn from_str(s: &str) -> &Slice { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn from_str(s: &str) -> &Slice { unsafe { mem::transmute(Wtf8::from_str(s)) } } #[inline] - pub fn to_str(&self) -> Result<&str, crate::str::Utf8Error> { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn to_str(&self) -> Result<&str, crate::str::Utf8Error> { self.inner.as_str() } diff --git a/library/std/src/sys/process/uefi.rs b/library/std/src/sys/process/uefi.rs index 4864c58698817..724c2557d74a9 100644 --- a/library/std/src/sys/process/uefi.rs +++ b/library/std/src/sys/process/uefi.rs @@ -274,7 +274,8 @@ impl fmt::Debug for ExitStatusError { } } -impl Into for ExitStatusError { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Into for ExitStatusError { fn into(self) -> ExitStatus { ExitStatus(self.0) } @@ -298,7 +299,8 @@ impl ExitCode { } } -impl From for ExitCode { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for ExitCode { fn from(code: u8) -> Self { match code { 0 => Self::SUCCESS, diff --git a/library/std/src/sys/process/unix/common.rs b/library/std/src/sys/process/unix/common.rs index 6219be60caf2c..e804653ec7e8e 100644 --- a/library/std/src/sys/process/unix/common.rs +++ b/library/std/src/sys/process/unix/common.rs @@ -595,7 +595,8 @@ impl ExitCode { } } -impl From for ExitCode { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for ExitCode { fn from(code: u8) -> Self { Self(code) } diff --git a/library/std/src/sys/process/unix/fuchsia.rs b/library/std/src/sys/process/unix/fuchsia.rs index d71be510b6afe..609f105aebc50 100644 --- a/library/std/src/sys/process/unix/fuchsia.rs +++ b/library/std/src/sys/process/unix/fuchsia.rs @@ -304,7 +304,8 @@ impl fmt::Display for ExitStatus { #[derive(PartialEq, Eq, Clone, Copy, Debug)] pub struct ExitStatusError(NonZero); -impl Into for ExitStatusError { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Into for ExitStatusError { fn into(self) -> ExitStatus { ExitStatus(self.0.into()) } diff --git a/library/std/src/sys/process/unix/unix.rs b/library/std/src/sys/process/unix/unix.rs index 5d13d6da18582..0457231f23fca 100644 --- a/library/std/src/sys/process/unix/unix.rs +++ b/library/std/src/sys/process/unix/unix.rs @@ -1238,7 +1238,8 @@ impl fmt::Display for ExitStatus { #[derive(PartialEq, Eq, Clone, Copy)] pub struct ExitStatusError(NonZero); -impl Into for ExitStatusError { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Into for ExitStatusError { fn into(self) -> ExitStatus { ExitStatus(self.0.into()) } diff --git a/library/std/src/sys/process/unix/unsupported.rs b/library/std/src/sys/process/unix/unsupported.rs index 87403cd50f822..e02f3beb118c6 100644 --- a/library/std/src/sys/process/unix/unsupported.rs +++ b/library/std/src/sys/process/unix/unsupported.rs @@ -63,7 +63,8 @@ pub use wait_status::ExitStatus; #[derive(PartialEq, Eq, Clone, Copy, Debug)] pub struct ExitStatusError(NonZero); -impl Into for ExitStatusError { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Into for ExitStatusError { fn into(self) -> ExitStatus { ExitStatus::from(c_int::from(self.0)) } diff --git a/library/std/src/sys/process/unix/vxworks.rs b/library/std/src/sys/process/unix/vxworks.rs index 2275cbb946a9c..91aa1390238b4 100644 --- a/library/std/src/sys/process/unix/vxworks.rs +++ b/library/std/src/sys/process/unix/vxworks.rs @@ -260,7 +260,8 @@ impl fmt::Display for ExitStatus { #[derive(PartialEq, Eq, Clone, Copy, Debug)] pub struct ExitStatusError(NonZero); -impl Into for ExitStatusError { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Into for ExitStatusError { fn into(self) -> ExitStatus { ExitStatus(self.0.into()) } diff --git a/library/std/src/sys/process/unsupported.rs b/library/std/src/sys/process/unsupported.rs index 469922c78aca2..e627db5ca5b9f 100644 --- a/library/std/src/sys/process/unsupported.rs +++ b/library/std/src/sys/process/unsupported.rs @@ -242,7 +242,8 @@ impl fmt::Debug for ExitStatusError { } } -impl Into for ExitStatusError { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Into for ExitStatusError { fn into(self) -> ExitStatus { self.0 } @@ -266,7 +267,8 @@ impl ExitCode { } } -impl From for ExitCode { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for ExitCode { fn from(code: u8) -> Self { Self(code) } diff --git a/library/std/src/sys/process/windows.rs b/library/std/src/sys/process/windows.rs index 1ee3fbd285f52..fe048b17b6815 100644 --- a/library/std/src/sys/process/windows.rs +++ b/library/std/src/sys/process/windows.rs @@ -782,7 +782,8 @@ impl fmt::Display for ExitStatus { #[derive(PartialEq, Eq, Clone, Copy, Debug)] pub struct ExitStatusError(NonZero); -impl Into for ExitStatusError { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const Into for ExitStatusError { fn into(self) -> ExitStatus { ExitStatus(self.0.into()) } @@ -807,13 +808,15 @@ impl ExitCode { } } -impl From for ExitCode { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for ExitCode { fn from(code: u8) -> Self { ExitCode(u32::from(code)) } } -impl From for ExitCode { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const From for ExitCode { fn from(code: u32) -> Self { ExitCode(u32::from(code)) } diff --git a/library/std/src/sys_common/mod.rs b/library/std/src/sys_common/mod.rs index cce88d936b71b..d1059fb4ba78e 100644 --- a/library/std/src/sys_common/mod.rs +++ b/library/std/src/sys_common/mod.rs @@ -27,6 +27,8 @@ pub mod wtf8; /// A trait for viewing representations from std types #[doc(hidden)] +#[const_trait] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] #[allow(dead_code)] // not used on all platforms pub trait AsInner { fn as_inner(&self) -> &Inner; @@ -34,6 +36,8 @@ pub trait AsInner { /// A trait for viewing representations from std types #[doc(hidden)] +#[const_trait] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] #[allow(dead_code)] // not used on all platforms pub trait AsInnerMut { fn as_inner_mut(&mut self) -> &mut Inner; @@ -41,12 +45,16 @@ pub trait AsInnerMut { /// A trait for extracting representations from std types #[doc(hidden)] +#[const_trait] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub trait IntoInner { fn into_inner(self) -> Inner; } /// A trait for creating std types from internal representations #[doc(hidden)] +#[const_trait] +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] pub trait FromInner { fn from_inner(inner: Inner) -> Self; } diff --git a/library/std/src/sys_common/wtf8.rs b/library/std/src/sys_common/wtf8.rs index 50bde88b5a4c3..2385bc5521c93 100644 --- a/library/std/src/sys_common/wtf8.rs +++ b/library/std/src/sys_common/wtf8.rs @@ -142,7 +142,8 @@ pub struct Wtf8Buf { is_known_utf8: bool, } -impl ops::Deref for Wtf8Buf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const ops::Deref for Wtf8Buf { type Target = Wtf8; fn deref(&self) -> &Wtf8 { @@ -150,7 +151,8 @@ impl ops::Deref for Wtf8Buf { } } -impl ops::DerefMut for Wtf8Buf { +#[rustc_const_unstable(feature = "const_convert", issue = "143773")] +impl const ops::DerefMut for Wtf8Buf { fn deref_mut(&mut self) -> &mut Wtf8 { self.as_mut_slice() } @@ -199,7 +201,8 @@ impl Wtf8Buf { /// Since the byte vec is not checked for valid WTF-8, this function is /// marked unsafe. #[inline] - pub unsafe fn from_bytes_unchecked(value: Vec) -> Wtf8Buf { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const unsafe fn from_bytes_unchecked(value: Vec) -> Wtf8Buf { Wtf8Buf { bytes: value, is_known_utf8: false } } @@ -262,12 +265,14 @@ impl Wtf8Buf { } #[inline] - pub fn as_slice(&self) -> &Wtf8 { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_slice(&self) -> &Wtf8 { unsafe { Wtf8::from_bytes_unchecked(&self.bytes) } } #[inline] - pub fn as_mut_slice(&mut self) -> &mut Wtf8 { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_mut_slice(&mut self) -> &mut Wtf8 { // Safety: `Wtf8` doesn't expose any way to mutate the bytes that would // cause them to change from well-formed UTF-8 to ill-formed UTF-8, // which would break the assumptions of the `is_known_utf8` field. @@ -489,7 +494,8 @@ impl Wtf8Buf { } /// Converts a `Box` into a `Wtf8Buf`. - pub fn from_box(boxed: Box) -> Wtf8Buf { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn from_box(boxed: Box) -> Wtf8Buf { let bytes: Box<[u8]> = unsafe { mem::transmute(boxed) }; Wtf8Buf { bytes: bytes.into_vec(), is_known_utf8: false } } @@ -614,7 +620,8 @@ impl Wtf8 { /// /// Since WTF-8 is a superset of UTF-8, this always succeeds. #[inline] - pub fn from_str(value: &str) -> &Wtf8 { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn from_str(value: &str) -> &Wtf8 { unsafe { Wtf8::from_bytes_unchecked(value.as_bytes()) } } @@ -623,7 +630,8 @@ impl Wtf8 { /// Since the byte slice is not checked for valid WTF-8, this functions is /// marked unsafe. #[inline] - pub unsafe fn from_bytes_unchecked(value: &[u8]) -> &Wtf8 { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const unsafe fn from_bytes_unchecked(value: &[u8]) -> &Wtf8 { // SAFETY: start with &[u8], end with fancy &[u8] unsafe { &*(value as *const [u8] as *const Wtf8) } } @@ -633,7 +641,8 @@ impl Wtf8 { /// Since the byte slice is not checked for valid WTF-8, this functions is /// marked unsafe. #[inline] - unsafe fn from_mut_bytes_unchecked(value: &mut [u8]) -> &mut Wtf8 { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + const unsafe fn from_mut_bytes_unchecked(value: &mut [u8]) -> &mut Wtf8 { // SAFETY: start with &mut [u8], end with fancy &mut [u8] unsafe { &mut *(value as *mut [u8] as *mut Wtf8) } } @@ -681,7 +690,8 @@ impl Wtf8 { /// /// This does not copy the data. #[inline] - pub fn as_str(&self) -> Result<&str, str::Utf8Error> { + #[rustc_const_unstable(feature = "const_convert", issue = "143773")] + pub const fn as_str(&self) -> Result<&str, str::Utf8Error> { str::from_utf8(&self.bytes) } diff --git a/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.rs b/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.rs index 6a6b0e666e1c3..629f109dddb54 100644 --- a/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.rs +++ b/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.rs @@ -1,7 +1,7 @@ const fn foo(a: i32) -> Vec { vec![1, 2, 3] //~^ ERROR allocations are not allowed - //~| ERROR cannot call non-const method + //~| ERROR not yet stable as a const fn } fn main() {} diff --git a/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr b/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr index 8e52a7aa35e1e..80fedc37d2a86 100644 --- a/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr +++ b/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr @@ -6,16 +6,18 @@ LL | vec![1, 2, 3] | = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0015]: cannot call non-const method `slice::::into_vec::` in constant functions +error: `slice::::into_vec` is not yet stable as a const fn --> $DIR/bad_const_fn_body_ice.rs:2:5 | LL | vec![1, 2, 3] | ^^^^^^^^^^^^^ | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) +help: add `#![feature(const_convert_methods)]` to the crate attributes to enable + | +LL + #![feature(const_convert_methods)] + | error: aborting due to 2 previous errors -Some errors have detailed explanations: E0010, E0015. -For more information about an error, try `rustc --explain E0010`. +For more information about this error, try `rustc --explain E0010`. diff --git a/tests/ui/error-codes/E0010-teach.rs b/tests/ui/error-codes/E0010-teach.rs index 0eef24783873c..a307f14df046d 100644 --- a/tests/ui/error-codes/E0010-teach.rs +++ b/tests/ui/error-codes/E0010-teach.rs @@ -3,5 +3,5 @@ #![allow(warnings)] const CON: Vec = vec![1, 2, 3]; //~ ERROR E0010 -//~| ERROR cannot call non-const method +//~| ERROR is not yet stable as a const fn fn main() {} diff --git a/tests/ui/error-codes/E0010-teach.stderr b/tests/ui/error-codes/E0010-teach.stderr index 82bbe01aef792..853c7b23a3d4a 100644 --- a/tests/ui/error-codes/E0010-teach.stderr +++ b/tests/ui/error-codes/E0010-teach.stderr @@ -7,16 +7,18 @@ LL | const CON: Vec = vec![1, 2, 3]; = note: The runtime heap is not yet available at compile-time, so no runtime heap allocations can be created. = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0015]: cannot call non-const method `slice::::into_vec::` in constants +error: `slice::::into_vec` is not yet stable as a const fn --> $DIR/E0010-teach.rs:5:23 | LL | const CON: Vec = vec![1, 2, 3]; | ^^^^^^^^^^^^^ | - = note: calls in constants are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) +help: add `#![feature(const_convert_methods)]` to the crate attributes to enable + | +LL + #![feature(const_convert_methods)] + | error: aborting due to 2 previous errors -Some errors have detailed explanations: E0010, E0015. -For more information about an error, try `rustc --explain E0010`. +For more information about this error, try `rustc --explain E0010`. diff --git a/tests/ui/error-codes/E0010.rs b/tests/ui/error-codes/E0010.rs index edb96714dd320..769b904da1a7b 100644 --- a/tests/ui/error-codes/E0010.rs +++ b/tests/ui/error-codes/E0010.rs @@ -1,5 +1,5 @@ #![allow(warnings)] const CON: Vec = vec![1, 2, 3]; //~ ERROR E0010 -//~| ERROR cannot call non-const method +//~| ERROR is not yet stable as a const fn fn main() {} diff --git a/tests/ui/error-codes/E0010.stderr b/tests/ui/error-codes/E0010.stderr index 87b722b5f6566..270f6ee11284a 100644 --- a/tests/ui/error-codes/E0010.stderr +++ b/tests/ui/error-codes/E0010.stderr @@ -6,16 +6,18 @@ LL | const CON: Vec = vec![1, 2, 3]; | = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0015]: cannot call non-const method `slice::::into_vec::` in constants +error: `slice::::into_vec` is not yet stable as a const fn --> $DIR/E0010.rs:3:23 | LL | const CON: Vec = vec![1, 2, 3]; | ^^^^^^^^^^^^^ | - = note: calls in constants are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) +help: add `#![feature(const_convert_methods)]` to the crate attributes to enable + | +LL + #![feature(const_convert_methods)] + | error: aborting due to 2 previous errors -Some errors have detailed explanations: E0010, E0015. -For more information about an error, try `rustc --explain E0010`. +For more information about this error, try `rustc --explain E0010`. diff --git a/tests/ui/statics/check-values-constraints.rs b/tests/ui/statics/check-values-constraints.rs index 9df76b5c1497f..b589af08223ef 100644 --- a/tests/ui/statics/check-values-constraints.rs +++ b/tests/ui/statics/check-values-constraints.rs @@ -80,7 +80,7 @@ struct MyOwned; static STATIC11: Vec = vec![MyOwned]; //~^ ERROR allocations are not allowed in statics -//~^^ ERROR cannot call non-const +//~^^ ERROR is not yet stable as a const fn static mut STATIC12: UnsafeStruct = UnsafeStruct; @@ -94,28 +94,28 @@ static mut STATIC14: SafeStruct = SafeStruct { static STATIC15: &'static [Vec] = &[ vec![MyOwned], //~ ERROR allocations are not allowed in statics - //~^ ERROR cannot call non-const + //~^ ERROR is not yet stable as a const fn vec![MyOwned], //~ ERROR allocations are not allowed in statics - //~^ ERROR cannot call non-const + //~^ ERROR is not yet stable as a const fn ]; static STATIC16: (&'static Vec, &'static Vec) = ( &vec![MyOwned], //~ ERROR allocations are not allowed in statics - //~^ ERROR cannot call non-const + //~^ ERROR is not yet stable as a const fn &vec![MyOwned], //~ ERROR allocations are not allowed in statics - //~^ ERROR cannot call non-const + //~^ ERROR is not yet stable as a const fn ); static mut STATIC17: SafeEnum = SafeEnum::Variant1; static STATIC19: Vec = vec![3]; //~^ ERROR allocations are not allowed in statics -//~^^ ERROR cannot call non-const +//~^^ ERROR is not yet stable as a const fn pub fn main() { let y = { static x: Vec = vec![3]; //~ ERROR allocations are not allowed in statics - //~^ ERROR cannot call non-const + //~^ ERROR is not yet stable as a const fn x //~^ ERROR cannot move out of static }; diff --git a/tests/ui/statics/check-values-constraints.stderr b/tests/ui/statics/check-values-constraints.stderr index eb2d37d297e9f..d4f6274bc42e6 100644 --- a/tests/ui/statics/check-values-constraints.stderr +++ b/tests/ui/statics/check-values-constraints.stderr @@ -19,15 +19,17 @@ LL | static STATIC11: Vec = vec![MyOwned]; | = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0015]: cannot call non-const method `slice::::into_vec::` in statics +error: `slice::::into_vec` is not yet stable as a const fn --> $DIR/check-values-constraints.rs:81:33 | LL | static STATIC11: Vec = vec![MyOwned]; | ^^^^^^^^^^^^^ | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants - = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) +help: add `#![feature(const_convert_methods)]` to the crate attributes to enable + | +LL + #![feature(const_convert_methods)] + | error[E0015]: cannot call non-const method `::to_string` in statics --> $DIR/check-values-constraints.rs:92:38 @@ -46,15 +48,17 @@ LL | vec![MyOwned], | = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0015]: cannot call non-const method `slice::::into_vec::` in statics +error: `slice::::into_vec` is not yet stable as a const fn --> $DIR/check-values-constraints.rs:96:5 | LL | vec![MyOwned], | ^^^^^^^^^^^^^ | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants - = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) +help: add `#![feature(const_convert_methods)]` to the crate attributes to enable + | +LL + #![feature(const_convert_methods)] + | error[E0010]: allocations are not allowed in statics --> $DIR/check-values-constraints.rs:98:5 @@ -64,15 +68,17 @@ LL | vec![MyOwned], | = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0015]: cannot call non-const method `slice::::into_vec::` in statics +error: `slice::::into_vec` is not yet stable as a const fn --> $DIR/check-values-constraints.rs:98:5 | LL | vec![MyOwned], | ^^^^^^^^^^^^^ | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants - = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) +help: add `#![feature(const_convert_methods)]` to the crate attributes to enable + | +LL + #![feature(const_convert_methods)] + | error[E0010]: allocations are not allowed in statics --> $DIR/check-values-constraints.rs:103:6 @@ -82,15 +88,17 @@ LL | &vec![MyOwned], | = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0015]: cannot call non-const method `slice::::into_vec::` in statics +error: `slice::::into_vec` is not yet stable as a const fn --> $DIR/check-values-constraints.rs:103:6 | LL | &vec![MyOwned], | ^^^^^^^^^^^^^ | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants - = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) +help: add `#![feature(const_convert_methods)]` to the crate attributes to enable + | +LL + #![feature(const_convert_methods)] + | error[E0010]: allocations are not allowed in statics --> $DIR/check-values-constraints.rs:105:6 @@ -100,15 +108,17 @@ LL | &vec![MyOwned], | = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0015]: cannot call non-const method `slice::::into_vec::` in statics +error: `slice::::into_vec` is not yet stable as a const fn --> $DIR/check-values-constraints.rs:105:6 | LL | &vec![MyOwned], | ^^^^^^^^^^^^^ | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants - = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) +help: add `#![feature(const_convert_methods)]` to the crate attributes to enable + | +LL + #![feature(const_convert_methods)] + | error[E0010]: allocations are not allowed in statics --> $DIR/check-values-constraints.rs:111:31 @@ -118,15 +128,17 @@ LL | static STATIC19: Vec = vec![3]; | = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0015]: cannot call non-const method `slice::::into_vec::` in statics +error: `slice::::into_vec` is not yet stable as a const fn --> $DIR/check-values-constraints.rs:111:31 | LL | static STATIC19: Vec = vec![3]; | ^^^^^^^ | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants - = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) +help: add `#![feature(const_convert_methods)]` to the crate attributes to enable + | +LL + #![feature(const_convert_methods)] + | error[E0010]: allocations are not allowed in statics --> $DIR/check-values-constraints.rs:117:32 @@ -136,15 +148,17 @@ LL | static x: Vec = vec![3]; | = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0015]: cannot call non-const method `slice::::into_vec::` in statics +error: `slice::::into_vec` is not yet stable as a const fn --> $DIR/check-values-constraints.rs:117:32 | LL | static x: Vec = vec![3]; | ^^^^^^^ | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants - = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) +help: add `#![feature(const_convert_methods)]` to the crate attributes to enable + | +LL + #![feature(const_convert_methods)] + | error[E0507]: cannot move out of static item `x` --> $DIR/check-values-constraints.rs:119:9 diff --git a/tests/ui/traits/const-traits/enforce-deref-on-adjust.rs b/tests/ui/traits/const-traits/enforce-deref-on-adjust.rs index d5240b7e18ddb..cba207f095309 100644 --- a/tests/ui/traits/const-traits/enforce-deref-on-adjust.rs +++ b/tests/ui/traits/const-traits/enforce-deref-on-adjust.rs @@ -1,6 +1,6 @@ //@ check-pass -#![feature(const_deref)] +#![feature(const_convert)] #![feature(const_trait_impl)] use std::ops::Deref; diff --git a/tests/ui/traits/const-traits/non-const-op-in-closure-in-const.rs b/tests/ui/traits/const-traits/non-const-op-in-closure-in-const.rs index d5f80acc15b54..50c985096d127 100644 --- a/tests/ui/traits/const-traits/non-const-op-in-closure-in-const.rs +++ b/tests/ui/traits/const-traits/non-const-op-in-closure-in-const.rs @@ -1,4 +1,4 @@ -#![feature(const_trait_impl, const_from)] +#![feature(const_trait_impl, const_convert)] //@ check-pass