Skip to content

Commit 9f2ad0a

Browse files
authored
Rollup merge of #90009 - woppopo:const_from_more, r=dtolnay
Make more `From` impls `const` (libcore) Adding `const` to `From` implementations in the core. `rustc_const_unstable` attribute is not added to unstable implementations. Tracking issue: #88674 <details> <summary>Done</summary><div> - `T` from `T` - `T` from `!` - `Option<T>` from `T` - `Option<&T>` from `&Option<T>` - `Option<&mut T>` from `&mut Option<T>` - `Cell<T>` from `T` - `RefCell<T>` from `T` - `UnsafeCell<T>` from `T` - `OnceCell<T>` from `T` - `Poll<T>` from `T` - `u32` from `char` - `u64` from `char` - `u128` from `char` - `char` from `u8` - `AtomicBool` from `bool` - `AtomicPtr<T>` from `*mut T` - `AtomicI(bits)` from `i(bits)` - `AtomicU(bits)` from `u(bits)` - `i(bits)` from `NonZeroI(bits)` - `u(bits)` from `NonZeroU(bits)` - `NonNull<T>` from `Unique<T>` - `NonNull<T>` from `&T` - `NonNull<T>` from `&mut T` - `Unique<T>` from `&mut T` - `Infallible` from `!` - `TryIntError` from `!` - `TryIntError` from `Infallible` - `TryFromSliceError` from `Infallible` - `FromResidual for Option<T>` </div></details> <details> <summary>Remaining</summary><dev> - `NonZero` from `NonZero` These can't be made const at this time because these use Into::into. https://github.com/rust-lang/rust/blob/master/library/core/src/convert/num.rs#L393 - `std`, `alloc` There may still be many implementations that can be made `const`. </div></details>
2 parents f702499 + 7936ecf commit 9f2ad0a

File tree

19 files changed

+103
-27
lines changed

19 files changed

+103
-27
lines changed

library/core/src/array/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ impl TryFromSliceError {
125125
}
126126

127127
#[stable(feature = "try_from_slice_error", since = "1.36.0")]
128-
impl From<Infallible> for TryFromSliceError {
128+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
129+
impl const From<Infallible> for TryFromSliceError {
129130
fn from(x: Infallible) -> TryFromSliceError {
130131
match x {}
131132
}

library/core/src/cell.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ impl<T: Ord + Copy> Ord for Cell<T> {
308308
}
309309

310310
#[stable(feature = "cell_from", since = "1.12.0")]
311-
impl<T> From<T> for Cell<T> {
311+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
312+
impl<T> const From<T> for Cell<T> {
312313
fn from(t: T) -> Cell<T> {
313314
Cell::new(t)
314315
}
@@ -1236,7 +1237,8 @@ impl<T: ?Sized + Ord> Ord for RefCell<T> {
12361237
}
12371238

12381239
#[stable(feature = "cell_from", since = "1.12.0")]
1239-
impl<T> From<T> for RefCell<T> {
1240+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
1241+
impl<T> const From<T> for RefCell<T> {
12401242
fn from(t: T) -> RefCell<T> {
12411243
RefCell::new(t)
12421244
}
@@ -1976,7 +1978,8 @@ impl<T: Default> Default for UnsafeCell<T> {
19761978
}
19771979

19781980
#[stable(feature = "cell_from", since = "1.12.0")]
1979-
impl<T> From<T> for UnsafeCell<T> {
1981+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
1982+
impl<T> const From<T> for UnsafeCell<T> {
19801983
fn from(t: T) -> UnsafeCell<T> {
19811984
UnsafeCell::new(t)
19821985
}

library/core/src/char/convert.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ pub unsafe fn from_u32_unchecked(i: u32) -> char {
9797
}
9898

9999
#[stable(feature = "char_convert", since = "1.13.0")]
100-
impl From<char> for u32 {
100+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
101+
impl const From<char> for u32 {
101102
/// Converts a [`char`] into a [`u32`].
102103
///
103104
/// # Examples
@@ -116,7 +117,8 @@ impl From<char> for u32 {
116117
}
117118

118119
#[stable(feature = "more_char_conversions", since = "1.51.0")]
119-
impl From<char> for u64 {
120+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
121+
impl const From<char> for u64 {
120122
/// Converts a [`char`] into a [`u64`].
121123
///
122124
/// # Examples
@@ -137,7 +139,8 @@ impl From<char> for u64 {
137139
}
138140

139141
#[stable(feature = "more_char_conversions", since = "1.51.0")]
140-
impl From<char> for u128 {
142+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
143+
impl const From<char> for u128 {
141144
/// Converts a [`char`] into a [`u128`].
142145
///
143146
/// # Examples
@@ -176,7 +179,8 @@ impl From<char> for u128 {
176179
/// for a superset of Windows-1252 that fills the remaining blanks with corresponding
177180
/// C0 and C1 control codes.
178181
#[stable(feature = "char_convert", since = "1.13.0")]
179-
impl From<u8> for char {
182+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
183+
impl const From<u8> for char {
180184
/// Converts a [`u8`] into a [`char`].
181185
///
182186
/// # Examples

library/core/src/convert/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ where
545545

546546
// From (and thus Into) is reflexive
547547
#[stable(feature = "rust1", since = "1.0.0")]
548-
impl<T> From<T> for T {
548+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
549+
impl<T> const From<T> for T {
549550
fn from(t: T) -> T {
550551
t
551552
}
@@ -560,7 +561,8 @@ impl<T> From<T> for T {
560561
#[allow(unused_attributes)] // FIXME(#58633): do a principled fix instead.
561562
#[rustc_reservation_impl = "permitting this impl would forbid us from adding \
562563
`impl<T> From<!> for T` later; see rust-lang/rust#64715 for details"]
563-
impl<T> From<!> for T {
564+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
565+
impl<T> const From<!> for T {
564566
fn from(t: !) -> T {
565567
t
566568
}
@@ -726,7 +728,8 @@ impl Ord for Infallible {
726728
}
727729

728730
#[stable(feature = "convert_infallible", since = "1.34.0")]
729-
impl From<!> for Infallible {
731+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
732+
impl const From<!> for Infallible {
730733
fn from(x: !) -> Self {
731734
x
732735
}

library/core/src/lazy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<T: PartialEq> PartialEq for OnceCell<T> {
7474
impl<T: Eq> Eq for OnceCell<T> {}
7575

7676
#[unstable(feature = "once_cell", issue = "74465")]
77-
impl<T> From<T> for OnceCell<T> {
77+
impl<T> const From<T> for OnceCell<T> {
7878
fn from(value: T) -> Self {
7979
OnceCell { inner: UnsafeCell::new(Some(value)) }
8080
}

library/core/src/num/error.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ impl fmt::Display for TryFromIntError {
2929
}
3030

3131
#[stable(feature = "try_from", since = "1.34.0")]
32-
impl From<Infallible> for TryFromIntError {
32+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
33+
impl const From<Infallible> for TryFromIntError {
3334
fn from(x: Infallible) -> TryFromIntError {
3435
match x {}
3536
}
3637
}
3738

3839
#[unstable(feature = "never_type", issue = "35121")]
39-
impl From<!> for TryFromIntError {
40+
impl const From<!> for TryFromIntError {
4041
fn from(never: !) -> TryFromIntError {
4142
// Match rather than coerce to make sure that code like
4243
// `From<Infallible> for TryFromIntError` above will keep working

library/core/src/num/nonzero.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ macro_rules! nonzero_integers {
8282
}
8383

8484
#[stable(feature = "from_nonzero", since = "1.31.0")]
85-
impl From<$Ty> for $Int {
85+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
86+
impl const From<$Ty> for $Int {
8687
#[doc = concat!("Converts a `", stringify!($Ty), "` into an `", stringify!($Int), "`")]
8788
#[inline]
8889
fn from(nonzero: $Ty) -> Self {

library/core/src/option.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,8 @@ impl<'a, T> IntoIterator for &'a mut Option<T> {
17231723
}
17241724

17251725
#[stable(since = "1.12.0", feature = "option_from")]
1726-
impl<T> From<T> for Option<T> {
1726+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
1727+
impl<T> const From<T> for Option<T> {
17271728
/// Moves `val` into a new [`Some`].
17281729
///
17291730
/// # Examples
@@ -1739,7 +1740,8 @@ impl<T> From<T> for Option<T> {
17391740
}
17401741

17411742
#[stable(feature = "option_ref_from_ref_option", since = "1.30.0")]
1742-
impl<'a, T> From<&'a Option<T>> for Option<&'a T> {
1743+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
1744+
impl<'a, T> const From<&'a Option<T>> for Option<&'a T> {
17431745
/// Converts from `&Option<T>` to `Option<&T>`.
17441746
///
17451747
/// # Examples
@@ -1766,7 +1768,8 @@ impl<'a, T> From<&'a Option<T>> for Option<&'a T> {
17661768
}
17671769

17681770
#[stable(feature = "option_ref_from_ref_option", since = "1.30.0")]
1769-
impl<'a, T> From<&'a mut Option<T>> for Option<&'a mut T> {
1771+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
1772+
impl<'a, T> const From<&'a mut Option<T>> for Option<&'a mut T> {
17701773
/// Converts from `&mut Option<T>` to `Option<&mut T>`
17711774
///
17721775
/// # Examples
@@ -2052,7 +2055,7 @@ impl<T> ops::Try for Option<T> {
20522055
}
20532056

20542057
#[unstable(feature = "try_trait_v2", issue = "84277")]
2055-
impl<T> ops::FromResidual for Option<T> {
2058+
impl<T> const ops::FromResidual for Option<T> {
20562059
#[inline]
20572060
fn from_residual(residual: Option<convert::Infallible>) -> Self {
20582061
match residual {

library/core/src/ptr/non_null.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,8 @@ impl<T: ?Sized> hash::Hash for NonNull<T> {
698698
}
699699

700700
#[unstable(feature = "ptr_internals", issue = "none")]
701-
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
701+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
702+
impl<T: ?Sized> const From<Unique<T>> for NonNull<T> {
702703
#[inline]
703704
fn from(unique: Unique<T>) -> Self {
704705
// SAFETY: A Unique pointer cannot be null, so the conditions for
@@ -708,7 +709,8 @@ impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
708709
}
709710

710711
#[stable(feature = "nonnull", since = "1.25.0")]
711-
impl<T: ?Sized> From<&mut T> for NonNull<T> {
712+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
713+
impl<T: ?Sized> const From<&mut T> for NonNull<T> {
712714
#[inline]
713715
fn from(reference: &mut T) -> Self {
714716
// SAFETY: A mutable reference cannot be null.
@@ -717,7 +719,8 @@ impl<T: ?Sized> From<&mut T> for NonNull<T> {
717719
}
718720

719721
#[stable(feature = "nonnull", since = "1.25.0")]
720-
impl<T: ?Sized> From<&T> for NonNull<T> {
722+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
723+
impl<T: ?Sized> const From<&T> for NonNull<T> {
721724
#[inline]
722725
fn from(reference: &T) -> Self {
723726
// SAFETY: A reference cannot be null, so the conditions for

library/core/src/ptr/unique.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
176176
}
177177

178178
#[unstable(feature = "ptr_internals", issue = "none")]
179-
impl<T: ?Sized> From<&mut T> for Unique<T> {
179+
impl<T: ?Sized> const From<&mut T> for Unique<T> {
180180
#[inline]
181181
fn from(reference: &mut T) -> Self {
182182
// SAFETY: A mutable reference cannot be null

0 commit comments

Comments
 (0)