Constify Try, From, TryFrom

This commit is contained in:
Evgenii Zheltonozhskii
2025-07-21 12:49:45 +03:00
parent 12865ffd0d
commit 3c76e3d002
32 changed files with 204 additions and 299 deletions
+2 -1
View File
@@ -198,7 +198,8 @@ fn description(&self) -> &str {
}
#[stable(feature = "try_from_slice_error", since = "1.36.0")]
impl From<Infallible> for TryFromSliceError {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<Infallible> for TryFromSliceError {
fn from(x: Infallible) -> TryFromSliceError {
match x {}
}
+2 -1
View File
@@ -546,7 +546,8 @@ macro_rules! into_int_impl {
($($ty:ty)*) => {
$(
#[unstable(feature = "ascii_char", issue = "110998")]
impl From<AsciiChar> for $ty {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<AsciiChar> for $ty {
#[inline]
fn from(chr: AsciiChar) -> $ty {
chr as u8 as $ty
+10 -5
View File
@@ -36,7 +36,8 @@ pub(super) const fn from_u32(i: u32) -> Option<char> {
}
#[stable(feature = "char_convert", since = "1.13.0")]
impl From<char> for u32 {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<char> for u32 {
/// Converts a [`char`] into a [`u32`].
///
/// # Examples
@@ -53,7 +54,8 @@ fn from(c: char) -> Self {
}
#[stable(feature = "more_char_conversions", since = "1.51.0")]
impl From<char> for u64 {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<char> for u64 {
/// Converts a [`char`] into a [`u64`].
///
/// # Examples
@@ -72,7 +74,8 @@ fn from(c: char) -> Self {
}
#[stable(feature = "more_char_conversions", since = "1.51.0")]
impl From<char> for u128 {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<char> for u128 {
/// Converts a [`char`] into a [`u128`].
///
/// # Examples
@@ -157,7 +160,8 @@ fn try_from(c: char) -> Result<u16, Self::Error> {
/// 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")]
impl From<u8> for char {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<u8> for char {
/// Converts a [`u8`] into a [`char`].
///
/// # Examples
@@ -247,7 +251,8 @@ const fn char_try_from_u32(i: u32) -> Result<char, CharTryFromError> {
}
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<u32> for char {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const TryFrom<u32> for char {
type Error = CharTryFromError;
#[inline]
+27 -13
View File
@@ -216,6 +216,8 @@ pub const fn identity<T>(x: T) -> T {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "AsRef"]
#[const_trait]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
pub trait AsRef<T: PointeeSized>: PointeeSized {
/// Converts this type into a shared reference of the (usually inferred) input type.
#[stable(feature = "rust1", since = "1.0.0")]
@@ -367,6 +369,8 @@ pub trait AsRef<T: PointeeSized>: PointeeSized {
/// `&mut Vec<u8>`, for example, is the better choice (callers need to pass the correct type then).
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "AsMut"]
#[const_trait]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
pub trait AsMut<T: PointeeSized>: PointeeSized {
/// Converts this type into a mutable reference of the (usually inferred) input type.
#[stable(feature = "rust1", since = "1.0.0")]
@@ -710,9 +714,10 @@ pub trait TryFrom<T>: Sized {
// As lifts over &
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: PointeeSized, U: PointeeSized> AsRef<U> for &T
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T: PointeeSized, U: PointeeSized> const AsRef<U> for &T
where
T: AsRef<U>,
T: ~const AsRef<U>,
{
#[inline]
fn as_ref(&self) -> &U {
@@ -722,9 +727,10 @@ fn as_ref(&self) -> &U {
// As lifts over &mut
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: PointeeSized, U: PointeeSized> AsRef<U> for &mut T
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T: PointeeSized, U: PointeeSized> const AsRef<U> for &mut T
where
T: AsRef<U>,
T: ~const AsRef<U>,
{
#[inline]
fn as_ref(&self) -> &U {
@@ -742,9 +748,10 @@ fn as_ref(&self) -> &U {
// AsMut lifts over &mut
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: PointeeSized, U: PointeeSized> AsMut<U> for &mut T
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T: PointeeSized, U: PointeeSized> const AsMut<U> for &mut T
where
T: AsMut<U>,
T: ~const AsMut<U>,
{
#[inline]
fn as_mut(&mut self) -> &mut U {
@@ -840,7 +847,8 @@ fn try_from(value: U) -> Result<Self, Self::Error> {
////////////////////////////////////////////////////////////////////////////////
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsRef<[T]> for [T] {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T> const AsRef<[T]> for [T] {
#[inline(always)]
fn as_ref(&self) -> &[T] {
self
@@ -848,7 +856,8 @@ fn as_ref(&self) -> &[T] {
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsMut<[T]> for [T] {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T> const AsMut<[T]> for [T] {
#[inline(always)]
fn as_mut(&mut self) -> &mut [T] {
self
@@ -856,7 +865,8 @@ fn as_mut(&mut self) -> &mut [T] {
}
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<str> for str {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const AsRef<str> for str {
#[inline(always)]
fn as_ref(&self) -> &str {
self
@@ -864,7 +874,8 @@ fn as_ref(&self) -> &str {
}
#[stable(feature = "as_mut_str_for_str", since = "1.51.0")]
impl AsMut<str> for str {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const AsMut<str> for str {
#[inline(always)]
fn as_mut(&mut self) -> &mut str {
self
@@ -925,7 +936,8 @@ fn as_mut(&mut self) -> &mut str {
pub enum Infallible {}
#[stable(feature = "convert_infallible", since = "1.34.0")]
impl Clone for Infallible {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const Clone for Infallible {
fn clone(&self) -> Infallible {
match *self {}
}
@@ -953,7 +965,8 @@ fn description(&self) -> &str {
}
#[stable(feature = "convert_infallible", since = "1.34.0")]
impl PartialEq for Infallible {
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
impl const PartialEq for Infallible {
fn eq(&self, _: &Infallible) -> bool {
match *self {}
}
@@ -977,7 +990,8 @@ fn cmp(&self, _other: &Self) -> crate::cmp::Ordering {
}
#[stable(feature = "convert_infallible", since = "1.34.0")]
impl From<!> for Infallible {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<!> for Infallible {
#[inline]
fn from(x: !) -> Self {
x
+16 -8
View File
@@ -69,7 +69,8 @@ macro_rules! impl_from {
};
($Small:ty => $Large:ty, #[$attr:meta], $doc:expr $(,)?) => {
#[$attr]
impl From<$Small> for $Large {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<$Small> for $Large {
// Rustdocs on the impl block show a "[+] show undocumented items" toggle.
// Rustdocs on functions do not.
#[doc = $doc]
@@ -200,7 +201,8 @@ macro_rules! impl_float_from_bool {
)?
) => {
#[stable(feature = "float_from_bool", since = "1.68.0")]
impl From<bool> for $float {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<bool> 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.
///
@@ -250,7 +252,8 @@ fn from(small: bool) -> Self {
macro_rules! impl_try_from_unbounded {
($source:ty => $($target:ty),+) => {$(
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const TryFrom<$source> for $target {
type Error = TryFromIntError;
/// Tries to create the target number type from a source
@@ -268,7 +271,8 @@ fn try_from(value: $source) -> Result<Self, Self::Error> {
macro_rules! impl_try_from_lower_bounded {
($source:ty => $($target:ty),+) => {$(
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const TryFrom<$source> for $target {
type Error = TryFromIntError;
/// Tries to create the target number type from a source
@@ -290,7 +294,8 @@ fn try_from(u: $source) -> Result<Self, Self::Error> {
macro_rules! impl_try_from_upper_bounded {
($source:ty => $($target:ty),+) => {$(
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const TryFrom<$source> for $target {
type Error = TryFromIntError;
/// Tries to create the target number type from a source
@@ -312,7 +317,8 @@ fn try_from(u: $source) -> Result<Self, Self::Error> {
macro_rules! impl_try_from_both_bounded {
($source:ty => $($target:ty),+) => {$(
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const TryFrom<$source> for $target {
type Error = TryFromIntError;
/// Tries to create the target number type from a source
@@ -450,7 +456,8 @@ mod ptr_try_from_impls {
macro_rules! impl_nonzero_int_from_nonzero_int {
($Small:ty => $Large:ty) => {
#[stable(feature = "nz_int_conv", since = "1.41.0")]
impl From<NonZero<$Small>> for NonZero<$Large> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<NonZero<$Small>> for NonZero<$Large> {
// Rustdocs on the impl block show a "[+] show undocumented items" toggle.
// Rustdocs on functions do not.
#[doc = concat!("Converts <code>[NonZero]\\<[", stringify!($Small), "]></code> ")]
@@ -540,7 +547,8 @@ fn try_from(value: $Int) -> Result<Self, Self::Error> {
macro_rules! impl_nonzero_int_try_from_nonzero_int {
($source:ty => $($target:ty),+) => {$(
#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
impl TryFrom<NonZero<$source>> for NonZero<$target> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const TryFrom<NonZero<$source>> for NonZero<$target> {
type Error = TryFromIntError;
// Rustdocs on the impl block show a "[+] show undocumented items" toggle.
+24 -12
View File
@@ -1089,7 +1089,8 @@ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
}
#[stable(feature = "ip_from_ip", since = "1.16.0")]
impl From<Ipv4Addr> for IpAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<Ipv4Addr> for IpAddr {
/// Copies this address to a new `IpAddr::V4`.
///
/// # Examples
@@ -1111,7 +1112,8 @@ fn from(ipv4: Ipv4Addr) -> IpAddr {
}
#[stable(feature = "ip_from_ip", since = "1.16.0")]
impl From<Ipv6Addr> for IpAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<Ipv6Addr> for IpAddr {
/// Copies this address to a new `IpAddr::V6`.
///
/// # Examples
@@ -1221,7 +1223,8 @@ fn cmp(&self, other: &Ipv4Addr) -> Ordering {
}
#[stable(feature = "ip_u32", since = "1.1.0")]
impl From<Ipv4Addr> for u32 {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<Ipv4Addr> for u32 {
/// Uses [`Ipv4Addr::to_bits`] to convert an IPv4 address to a host byte order `u32`.
#[inline]
fn from(ip: Ipv4Addr) -> u32 {
@@ -1230,7 +1233,8 @@ fn from(ip: Ipv4Addr) -> u32 {
}
#[stable(feature = "ip_u32", since = "1.1.0")]
impl From<u32> for Ipv4Addr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<u32> for Ipv4Addr {
/// Uses [`Ipv4Addr::from_bits`] to convert a host byte order `u32` into an IPv4 address.
#[inline]
fn from(ip: u32) -> Ipv4Addr {
@@ -1239,7 +1243,8 @@ fn from(ip: u32) -> Ipv4Addr {
}
#[stable(feature = "from_slice_v4", since = "1.9.0")]
impl From<[u8; 4]> for Ipv4Addr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<[u8; 4]> for Ipv4Addr {
/// Creates an `Ipv4Addr` from a four element byte array.
///
/// # Examples
@@ -1257,7 +1262,8 @@ fn from(ip: u32) -> Ipv4Addr {
}
#[stable(feature = "ip_from_slice", since = "1.17.0")]
impl From<[u8; 4]> for IpAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<[u8; 4]> for IpAddr {
/// Creates an `IpAddr::V4` from a four element byte array.
///
/// # Examples
@@ -2211,7 +2217,8 @@ fn cmp(&self, other: &Ipv6Addr) -> Ordering {
}
#[stable(feature = "i128", since = "1.26.0")]
impl From<Ipv6Addr> for u128 {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<Ipv6Addr> for u128 {
/// Uses [`Ipv6Addr::to_bits`] to convert an IPv6 address to a host byte order `u128`.
#[inline]
fn from(ip: Ipv6Addr) -> u128 {
@@ -2219,7 +2226,8 @@ fn from(ip: Ipv6Addr) -> u128 {
}
}
#[stable(feature = "i128", since = "1.26.0")]
impl From<u128> for Ipv6Addr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<u128> for Ipv6Addr {
/// Uses [`Ipv6Addr::from_bits`] to convert a host byte order `u128` to an IPv6 address.
#[inline]
fn from(ip: u128) -> Ipv6Addr {
@@ -2228,7 +2236,8 @@ fn from(ip: u128) -> Ipv6Addr {
}
#[stable(feature = "ipv6_from_octets", since = "1.9.0")]
impl From<[u8; 16]> for Ipv6Addr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<[u8; 16]> for Ipv6Addr {
/// Creates an `Ipv6Addr` from a sixteen element byte array.
///
/// # Examples
@@ -2255,7 +2264,8 @@ fn from(ip: u128) -> Ipv6Addr {
}
#[stable(feature = "ipv6_from_segments", since = "1.16.0")]
impl From<[u16; 8]> for Ipv6Addr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<[u16; 8]> for Ipv6Addr {
/// Creates an `Ipv6Addr` from an eight element 16-bit array.
///
/// # Examples
@@ -2283,7 +2293,8 @@ fn from(ip: u128) -> Ipv6Addr {
}
#[stable(feature = "ip_from_slice", since = "1.17.0")]
impl From<[u8; 16]> for IpAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<[u8; 16]> for IpAddr {
/// Creates an `IpAddr::V6` from a sixteen element byte array.
///
/// # Examples
@@ -2310,7 +2321,8 @@ fn from(ip: u128) -> Ipv6Addr {
}
#[stable(feature = "ip_from_slice", since = "1.17.0")]
impl From<[u16; 8]> for IpAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<[u16; 8]> for IpAddr {
/// Creates an `IpAddr::V6` from an eight element 16-bit array.
///
/// # Examples
+6 -3
View File
@@ -592,7 +592,8 @@ pub const fn set_scope_id(&mut self, new_scope_id: u32) {
}
#[stable(feature = "ip_from_ip", since = "1.16.0")]
impl From<SocketAddrV4> for SocketAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<SocketAddrV4> for SocketAddr {
/// Converts a [`SocketAddrV4`] into a [`SocketAddr::V4`].
#[inline]
fn from(sock4: SocketAddrV4) -> SocketAddr {
@@ -601,7 +602,8 @@ fn from(sock4: SocketAddrV4) -> SocketAddr {
}
#[stable(feature = "ip_from_ip", since = "1.16.0")]
impl From<SocketAddrV6> for SocketAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<SocketAddrV6> for SocketAddr {
/// Converts a [`SocketAddrV6`] into a [`SocketAddr::V6`].
#[inline]
fn from(sock6: SocketAddrV6) -> SocketAddr {
@@ -610,7 +612,8 @@ fn from(sock6: SocketAddrV6) -> SocketAddr {
}
#[stable(feature = "addr_from_into_ip", since = "1.17.0")]
impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<I: ~const Into<IpAddr>> const From<(I, u16)> for SocketAddr {
/// Converts a tuple struct (Into<[`IpAddr`]>, `u16`) into a [`SocketAddr`].
///
/// This conversion creates a [`SocketAddr::V4`] for an [`IpAddr::V4`]
+4 -2
View File
@@ -26,14 +26,16 @@ fn description(&self) -> &str {
}
#[stable(feature = "try_from", since = "1.34.0")]
impl From<Infallible> for TryFromIntError {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<Infallible> for TryFromIntError {
fn from(x: Infallible) -> TryFromIntError {
match x {}
}
}
#[unstable(feature = "never_type", issue = "35121")]
impl From<!> for TryFromIntError {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<!> for TryFromIntError {
#[inline]
fn from(never: !) -> TryFromIntError {
// Match rather than coerce to make sure that code like
+2 -1
View File
@@ -1378,7 +1378,8 @@ 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")]
impl FromStr for $int_ty {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const FromStr for $int_ty {
type Err = ParseIntError;
/// Parses an integer from a string slice with decimal digits.
+2 -1
View File
@@ -297,7 +297,8 @@ fn hash<H>(&self, state: &mut H)
}
#[stable(feature = "from_nonzero", since = "1.31.0")]
impl<T> From<NonZero<T>> for T
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T> const From<NonZero<T>> for T
where
T: ZeroablePrimitive,
{
+7 -1
View File
@@ -128,7 +128,9 @@
)]
#[doc(alias = "?")]
#[lang = "Try"]
pub trait Try: FromResidual {
#[const_trait]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
pub trait Try: ~const FromResidual {
/// The type of the value produced by `?` when *not* short-circuiting.
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
type Output;
@@ -304,6 +306,8 @@ pub trait Try: FromResidual {
)]
#[rustc_diagnostic_item = "FromResidual"]
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
#[const_trait]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
pub trait FromResidual<R = <Self as Try>::Residual> {
/// Constructs the type from a compatible `Residual` type.
///
@@ -357,6 +361,8 @@ pub fn from_yeet<T, Y>(yeeted: Y) -> T
/// and in the other direction,
/// `<Result<Infallible, E> as Residual<T>>::TryType = Result<T, E>`.
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
#[const_trait]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
pub trait Residual<O> {
/// The "return" type of this meta-function.
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
+19 -9
View File
@@ -2144,9 +2144,12 @@ const fn expect_failed(msg: &str) -> ! {
/////////////////////////////////////////////////////////////////////////////
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Clone for Option<T>
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T> const Clone for Option<T>
where
T: Clone,
// FIXME(const_hack): the T: ~const Destruct should be inferred from the Self: ~const Destruct in clone_from.
// See https://github.com/rust-lang/rust/issues/144207
T: ~const Clone + ~const Destruct,
{
#[inline]
fn clone(&self) -> Self {
@@ -2230,7 +2233,8 @@ fn into_iter(self) -> IterMut<'a, T> {
}
#[stable(since = "1.12.0", feature = "option_from")]
impl<T> From<T> for Option<T> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T> const From<T> for Option<T> {
/// Moves `val` into a new [`Some`].
///
/// # Examples
@@ -2246,7 +2250,8 @@ fn from(val: T) -> Option<T> {
}
#[stable(feature = "option_ref_from_ref_option", since = "1.30.0")]
impl<'a, T> From<&'a Option<T>> for Option<&'a T> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<'a, T> const From<&'a Option<T>> for Option<&'a T> {
/// Converts from `&Option<T>` to `Option<&T>`.
///
/// # Examples
@@ -2273,7 +2278,8 @@ fn from(o: &'a Option<T>) -> Option<&'a T> {
}
#[stable(feature = "option_ref_from_ref_option", since = "1.30.0")]
impl<'a, T> From<&'a mut Option<T>> for Option<&'a mut T> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<'a, T> const From<&'a mut Option<T>> for Option<&'a mut T> {
/// Converts from `&mut Option<T>` to `Option<&mut T>`
///
/// # Examples
@@ -2593,7 +2599,8 @@ fn from_iter<I: IntoIterator<Item = Option<A>>>(iter: I) -> Option<V> {
}
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
impl<T> ops::Try for Option<T> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T> const ops::Try for Option<T> {
type Output = T;
type Residual = Option<convert::Infallible>;
@@ -2612,9 +2619,10 @@ fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
}
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
// Note: manually specifying the residual type instead of using the default to work around
// https://github.com/rust-lang/rust/issues/99940
impl<T> ops::FromResidual<Option<convert::Infallible>> for Option<T> {
impl<T> const ops::FromResidual<Option<convert::Infallible>> for Option<T> {
#[inline]
fn from_residual(residual: Option<convert::Infallible>) -> Self {
match residual {
@@ -2625,7 +2633,8 @@ fn from_residual(residual: Option<convert::Infallible>) -> Self {
#[diagnostic::do_not_recommend]
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
impl<T> ops::FromResidual<ops::Yeet<()>> for Option<T> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T> const ops::FromResidual<ops::Yeet<()>> for Option<T> {
#[inline]
fn from_residual(ops::Yeet(()): ops::Yeet<()>) -> Self {
None
@@ -2633,7 +2642,8 @@ fn from_residual(ops::Yeet(()): ops::Yeet<()>) -> Self {
}
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
impl<T> ops::Residual<T> for Option<convert::Infallible> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T> const ops::Residual<T> for Option<convert::Infallible> {
type TryType = Option<T>;
}
+4 -2
View File
@@ -189,7 +189,8 @@ fn try_from(align: usize) -> Result<Alignment, Self::Error> {
}
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl From<Alignment> for NonZero<usize> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<Alignment> for NonZero<usize> {
#[inline]
fn from(align: Alignment) -> NonZero<usize> {
align.as_nonzero()
@@ -197,7 +198,8 @@ fn from(align: Alignment) -> NonZero<usize> {
}
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl From<Alignment> for usize {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<Alignment> for usize {
#[inline]
fn from(align: Alignment) -> usize {
align.as_usize()
+18 -8
View File
@@ -1288,9 +1288,11 @@ pub fn unwrap_err(self) -> E
/// ```
#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
#[inline]
pub fn into_ok(self) -> T
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
pub const fn into_ok(self) -> T
where
E: Into<!>,
E: ~const Into<!>,
{
match self {
Ok(x) => x,
@@ -1323,9 +1325,11 @@ pub fn into_ok(self) -> T
/// ```
#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
#[inline]
pub fn into_err(self) -> E
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
pub const fn into_err(self) -> E
where
T: Into<!>,
T: ~const Into<!>,
{
match self {
Ok(x) => x.into(),
@@ -2052,7 +2056,8 @@ fn from_iter<I: IntoIterator<Item = Result<A, E>>>(iter: I) -> Result<V, E> {
}
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
impl<T, E> ops::Try for Result<T, E> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T, E> const ops::Try for Result<T, E> {
type Output = T;
type Residual = Result<convert::Infallible, E>;
@@ -2071,7 +2076,10 @@ fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
}
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
impl<T, E, F: From<E>> ops::FromResidual<Result<convert::Infallible, E>> for Result<T, F> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T, E, F: ~const From<E>> const ops::FromResidual<Result<convert::Infallible, E>>
for Result<T, F>
{
#[inline]
#[track_caller]
fn from_residual(residual: Result<convert::Infallible, E>) -> Self {
@@ -2082,7 +2090,8 @@ fn from_residual(residual: Result<convert::Infallible, E>) -> Self {
}
#[diagnostic::do_not_recommend]
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
impl<T, E, F: From<E>> ops::FromResidual<ops::Yeet<E>> for Result<T, F> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T, E, F: ~const From<E>> const ops::FromResidual<ops::Yeet<E>> for Result<T, F> {
#[inline]
fn from_residual(ops::Yeet(e): ops::Yeet<E>) -> Self {
Err(From::from(e))
@@ -2090,6 +2099,7 @@ fn from_residual(ops::Yeet(e): ops::Yeet<E>) -> Self {
}
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
impl<T, E> ops::Residual<T> for Result<convert::Infallible, E> {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T, E> const ops::Residual<T> for Result<convert::Infallible, E> {
type TryType = Result<T, E>;
}
+2
View File
@@ -825,6 +825,8 @@ fn index_mut(self, slice: &mut str) -> &mut Self::Output {
/// assert!(Point::from_str("(1 2)").is_err());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[const_trait]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
pub trait FromStr: Sized {
/// The associated error which can be returned from parsing.
#[stable(feature = "rust1", since = "1.0.0")]
+4 -2
View File
@@ -2518,7 +2518,8 @@ pub const fn as_ptr(&self) -> *mut *mut T {
#[cfg(target_has_atomic_load_store = "8")]
#[stable(feature = "atomic_bool_from", since = "1.24.0")]
impl From<bool> for AtomicBool {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<bool> for AtomicBool {
/// Converts a `bool` into an `AtomicBool`.
///
/// # Examples
@@ -2615,7 +2616,8 @@ fn default() -> Self {
}
#[$stable_from]
impl From<$int_type> for $atomic_type {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<$int_type> for $atomic_type {
#[doc = concat!("Converts an `", stringify!($int_type), "` into an `", stringify!($atomic_type), "`.")]
#[inline]
fn from(v: $int_type) -> Self { Self::new(v) }
@@ -4,6 +4,8 @@ const fn t() -> Option<()> {
Some(())?;
//~^ ERROR `?` is not allowed
//~| ERROR `?` is not allowed
//~| ERROR `Try` is not yet stable as a const trait
//~| ERROR `FromResidual` is not yet stable as a const trait
None
}
+32 -4
View File
@@ -1,19 +1,47 @@
error[E0015]: `?` is not allowed on `Option<()>` in constant functions
error[E0658]: `?` is not allowed on `Option<()>` in constant functions
--> $DIR/const-try-feature-gate.rs:4:5
|
LL | Some(())?;
| ^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0015]: `?` is not allowed on `Option<()>` in constant functions
error: `Try` is not yet stable as a const trait
--> $DIR/const-try-feature-gate.rs:4:5
|
LL | Some(())?;
| ^^^^^^^^^
|
help: add `#![feature(const_try)]` to the crate attributes to enable
|
LL + #![feature(const_try)]
|
error[E0658]: `?` is not allowed on `Option<()>` in constant functions
--> $DIR/const-try-feature-gate.rs:4:5
|
LL | Some(())?;
| ^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 2 previous errors
error: `FromResidual` is not yet stable as a const trait
--> $DIR/const-try-feature-gate.rs:4:5
|
LL | Some(())?;
| ^^^^^^^^^
|
help: add `#![feature(const_try)]` to the crate attributes to enable
|
LL + #![feature(const_try)]
|
For more information about this error, try `rustc --explain E0015`.
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0658`.
+3 -5
View File
@@ -1,4 +1,6 @@
//@ compile-flags: -Znext-solver
//@ check-pass
//@ revisions: current next
//@[next] compile-flags: -Znext-solver
// Demonstrates what's needed to make use of `?` in const contexts.
@@ -13,14 +15,12 @@
struct Error;
impl const FromResidual<Error> for TryMe {
//~^ ERROR const `impl` for trait `FromResidual` which is not `const`
fn from_residual(residual: Error) -> Self {
TryMe
}
}
impl const Try for TryMe {
//~^ ERROR const `impl` for trait `Try` which is not `const`
type Output = ();
type Residual = Error;
fn from_output(output: Self::Output) -> Self {
@@ -33,8 +33,6 @@ fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
const fn t() -> TryMe {
TryMe?;
//~^ ERROR `?` is not allowed on
//~| ERROR `?` is not allowed on
TryMe
}
-37
View File
@@ -1,37 +0,0 @@
error: const `impl` for trait `FromResidual` which is not `const`
--> $DIR/const-try.rs:15:12
|
LL | impl const FromResidual<Error> for TryMe {
| ^^^^^^^^^^^^^^^^^^^ this trait is not `const`
|
= note: marking a trait with `const` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change
error: const `impl` for trait `Try` which is not `const`
--> $DIR/const-try.rs:22:12
|
LL | impl const Try for TryMe {
| ^^^ this trait is not `const`
|
= note: marking a trait with `const` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change
error[E0015]: `?` is not allowed on `TryMe` in constant functions
--> $DIR/const-try.rs:35:5
|
LL | TryMe?;
| ^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error[E0015]: `?` is not allowed on `TryMe` in constant functions
--> $DIR/const-try.rs:35:5
|
LL | TryMe?;
| ^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0015`.
+5 -4
View File
@@ -1,11 +1,12 @@
// The `?` operator is still not const-evaluatable because it calls `From::from` on the error
// variant.
//@ check-pass
#![allow(dead_code)]
#![feature(const_trait_impl)]
#![feature(const_try)]
const fn opt() -> Option<i32> {
let x = Some(2);
x?;
//~^ ERROR: `?` is not allowed
//~| ERROR: `?` is not allowed
None
}
-19
View File
@@ -1,19 +0,0 @@
error[E0015]: `?` is not allowed on `Option<i32>` in constant functions
--> $DIR/try.rs:6:5
|
LL | x?;
| ^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error[E0015]: `?` is not allowed on `Option<i32>` in constant functions
--> $DIR/try.rs:6:5
|
LL | x?;
| ^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0015`.
+1 -2
View File
@@ -1,9 +1,8 @@
//@ known-bug: #110395
//@ run-pass
#![feature(try_trait_v2)]
#![feature(const_trait_impl)]
#![feature(const_try)]
#![feature(const_convert)]
fn main() {
const fn result() -> Result<bool, ()> {
-42
View File
@@ -1,42 +0,0 @@
error[E0635]: unknown feature `const_convert`
--> $DIR/try-operator.rs:6:12
|
LL | #![feature(const_convert)]
| ^^^^^^^^^^^^^
error[E0015]: `?` is not allowed on `Result<(), ()>` in constant functions
--> $DIR/try-operator.rs:10:9
|
LL | Err(())?;
| ^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error[E0015]: `?` is not allowed on `Result<bool, ()>` in constant functions
--> $DIR/try-operator.rs:10:9
|
LL | Err(())?;
| ^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error[E0015]: `?` is not allowed on `Option<()>` in constant functions
--> $DIR/try-operator.rs:18:9
|
LL | None?;
| ^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error[E0015]: `?` is not allowed on `Option<()>` in constant functions
--> $DIR/try-operator.rs:18:9
|
LL | None?;
| ^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0015, E0635.
For more information about an error, try `rustc --explain E0015`.
@@ -1,8 +1,10 @@
//@ check-pass
//@ compile-flags: -Znext-solver
// Regression test for #69615.
#![feature(const_trait_impl)]
#![feature(const_try)]
#[const_trait]
pub trait MyTrait {
@@ -12,8 +14,6 @@ pub trait MyTrait {
impl const MyTrait for () {
fn method(&self) -> Option<()> {
Some(())?;
//~^ ERROR `?` is not allowed on
//~| ERROR `?` is not allowed on
None
}
}
@@ -1,19 +0,0 @@
error[E0015]: `?` is not allowed on `Option<()>` in constant functions
--> $DIR/hir-const-check.rs:14:9
|
LL | Some(())?;
| ^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error[E0015]: `?` is not allowed on `Option<()>` in constant functions
--> $DIR/hir-const-check.rs:14:9
|
LL | Some(())?;
| ^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0015`.
@@ -1,11 +1,10 @@
#![allow(incomplete_features)]
#![feature(const_trait_impl, try_trait_v2)]
#![feature(const_trait_impl, const_try, try_trait_v2)]
use std::ops::FromResidual;
impl<T> const FromResidual for T {
//~^ ERROR const `impl` for trait `FromResidual` which is not `const`
//~| ERROR type parameter `T` must be used as the type parameter for some local type
//~^ ERROR type parameter `T` must be used as the type parameter for some local type
fn from_residual(t: T) -> _ {
//~^ ERROR the placeholder `_` is not allowed
t
@@ -1,12 +1,3 @@
error: const `impl` for trait `FromResidual` which is not `const`
--> $DIR/ice-119717-constant-lifetime.rs:6:15
|
LL | impl<T> const FromResidual for T {
| ^^^^^^^^^^^^ this trait is not `const`
|
= note: marking a trait with `const` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
--> $DIR/ice-119717-constant-lifetime.rs:6:6
|
@@ -17,7 +8,7 @@ LL | impl<T> const FromResidual for T {
= note: only traits defined in the current crate can be implemented for a type parameter
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated functions
--> $DIR/ice-119717-constant-lifetime.rs:9:31
--> $DIR/ice-119717-constant-lifetime.rs:8:31
|
LL | fn from_residual(t: T) -> _ {
| ^ not allowed in type signatures
@@ -28,7 +19,7 @@ LL - fn from_residual(t: T) -> _ {
LL + fn from_residual(t: T) -> T {
|
error: aborting due to 3 previous errors
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0121, E0210.
For more information about an error, try `rustc --explain E0121`.
@@ -6,20 +6,16 @@
struct Error;
impl const FromResidual<Error> for TryMe {}
//~^ ERROR const `impl` for trait `FromResidual` which is not `const`
//~| ERROR not all trait items implemented
//~^ ERROR not all trait items implemented
impl const Try for TryMe {
//~^ ERROR const `impl` for trait `Try` which is not `const`
//~| ERROR not all trait items implemented
//~^ ERROR not all trait items implemented
type Output = ();
type Residual = Error;
}
const fn t() -> TryMe {
TryMe?;
//~^ ERROR `?` is not allowed on
//~| ERROR `?` is not allowed on
TryMe
}
@@ -1,12 +1,3 @@
error: const `impl` for trait `FromResidual` which is not `const`
--> $DIR/ice-126148-failed-to-normalize.rs:8:12
|
LL | impl const FromResidual<Error> for TryMe {}
| ^^^^^^^^^^^^^^^^^^^ this trait is not `const`
|
= note: marking a trait with `const` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change
error[E0046]: not all trait items implemented, missing: `from_residual`
--> $DIR/ice-126148-failed-to-normalize.rs:8:1
|
@@ -15,17 +6,8 @@ LL | impl const FromResidual<Error> for TryMe {}
|
= help: implement the missing item: `fn from_residual(_: Error) -> Self { todo!() }`
error: const `impl` for trait `Try` which is not `const`
--> $DIR/ice-126148-failed-to-normalize.rs:12:12
|
LL | impl const Try for TryMe {
| ^^^ this trait is not `const`
|
= note: marking a trait with `const` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change
error[E0046]: not all trait items implemented, missing: `from_output`, `branch`
--> $DIR/ice-126148-failed-to-normalize.rs:12:1
--> $DIR/ice-126148-failed-to-normalize.rs:11:1
|
LL | impl const Try for TryMe {
| ^^^^^^^^^^^^^^^^^^^^^^^^ missing `from_output`, `branch` in implementation
@@ -33,23 +15,6 @@ LL | impl const Try for TryMe {
= help: implement the missing item: `fn from_output(_: <Self as Try>::Output) -> Self { todo!() }`
= help: implement the missing item: `fn branch(self) -> ControlFlow<<Self as Try>::Residual, <Self as Try>::Output> { todo!() }`
error[E0015]: `?` is not allowed on `TryMe` in constant functions
--> $DIR/ice-126148-failed-to-normalize.rs:20:5
|
LL | TryMe?;
| ^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error: aborting due to 2 previous errors
error[E0015]: `?` is not allowed on `TryMe` in constant functions
--> $DIR/ice-126148-failed-to-normalize.rs:20:5
|
LL | TryMe?;
| ^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error: aborting due to 6 previous errors
Some errors have detailed explanations: E0015, E0046.
For more information about an error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0046`.
@@ -1,4 +1,4 @@
//@ known-bug: #110395
//@ check-pass
//@ compile-flags: -Znext-solver
#![allow(incomplete_features)]
#![feature(staged_api)]
@@ -1,37 +0,0 @@
error: const `impl` for trait `Try` which is not `const`
--> $DIR/trait-default-body-stability.rs:19:12
|
LL | impl const Try for T {
| ^^^ this trait is not `const`
|
= note: marking a trait with `const` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change
error: const `impl` for trait `FromResidual` which is not `const`
--> $DIR/trait-default-body-stability.rs:34:12
|
LL | impl const FromResidual for T {
| ^^^^^^^^^^^^ this trait is not `const`
|
= note: marking a trait with `const` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change
error[E0015]: `?` is not allowed on `T` in constant functions
--> $DIR/trait-default-body-stability.rs:46:9
|
LL | T?
| ^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error[E0015]: `?` is not allowed on `T` in constant functions
--> $DIR/trait-default-body-stability.rs:46:9
|
LL | T?
| ^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0015`.