Skip to content

Commit c374b03

Browse files
authored
Rollup merge of rust-lang#143774 - oli-obk:const_from, r=fee1-dead
constify `From` and `Into` tracking issue rust-lang#143773 r? ``@fee1-dead`` I did not mark any impls elsewhere as `const`, those can happen on their own timeframe and don't need to be part of this MVP. But if there are some core ones you think should be in there I'll happily add them, just couldn't think of any
2 parents 4c36374 + e681d1a commit c374b03

File tree

5 files changed

+38
-43
lines changed

5 files changed

+38
-43
lines changed

library/core/src/convert/mod.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ pub trait AsMut<T: PointeeSized>: PointeeSized {
445445
#[rustc_diagnostic_item = "Into"]
446446
#[stable(feature = "rust1", since = "1.0.0")]
447447
#[doc(search_unbox)]
448+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
449+
#[const_trait]
448450
pub trait Into<T>: Sized {
449451
/// Converts this type into the (usually inferred) input type.
450452
#[must_use]
@@ -580,6 +582,8 @@ pub trait Into<T>: Sized {
580582
note = "to coerce a `{T}` into a `{Self}`, use `&*` as a prefix",
581583
))]
582584
#[doc(search_unbox)]
585+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
586+
#[const_trait]
583587
pub trait From<T>: Sized {
584588
/// Converts to this type from the input type.
585589
#[rustc_diagnostic_item = "from_fn"]
@@ -607,6 +611,8 @@ pub trait From<T>: Sized {
607611
/// [`Into`], see there for details.
608612
#[rustc_diagnostic_item = "TryInto"]
609613
#[stable(feature = "try_from", since = "1.34.0")]
614+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
615+
#[const_trait]
610616
pub trait TryInto<T>: Sized {
611617
/// The type returned in the event of a conversion error.
612618
#[stable(feature = "try_from", since = "1.34.0")]
@@ -685,6 +691,8 @@ pub trait TryInto<T>: Sized {
685691
/// [`try_from`]: TryFrom::try_from
686692
#[rustc_diagnostic_item = "TryFrom"]
687693
#[stable(feature = "try_from", since = "1.34.0")]
694+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
695+
#[const_trait]
688696
pub trait TryFrom<T>: Sized {
689697
/// The type returned in the event of a conversion error.
690698
#[stable(feature = "try_from", since = "1.34.0")]
@@ -754,9 +762,10 @@ where
754762

755763
// From implies Into
756764
#[stable(feature = "rust1", since = "1.0.0")]
757-
impl<T, U> Into<U> for T
765+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
766+
impl<T, U> const Into<U> for T
758767
where
759-
U: From<T>,
768+
U: ~const From<T>,
760769
{
761770
/// Calls `U::from(self)`.
762771
///
@@ -771,7 +780,8 @@ where
771780

772781
// From (and thus Into) is reflexive
773782
#[stable(feature = "rust1", since = "1.0.0")]
774-
impl<T> From<T> for T {
783+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
784+
impl<T> const From<T> for T {
775785
/// Returns the argument unchanged.
776786
#[inline(always)]
777787
fn from(t: T) -> T {
@@ -787,17 +797,19 @@ impl<T> From<T> for T {
787797
#[stable(feature = "convert_infallible", since = "1.34.0")]
788798
#[rustc_reservation_impl = "permitting this impl would forbid us from adding \
789799
`impl<T> From<!> for T` later; see rust-lang/rust#64715 for details"]
790-
impl<T> From<!> for T {
800+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
801+
impl<T> const From<!> for T {
791802
fn from(t: !) -> T {
792803
t
793804
}
794805
}
795806

796807
// TryFrom implies TryInto
797808
#[stable(feature = "try_from", since = "1.34.0")]
798-
impl<T, U> TryInto<U> for T
809+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
810+
impl<T, U> const TryInto<U> for T
799811
where
800-
U: TryFrom<T>,
812+
U: ~const TryFrom<T>,
801813
{
802814
type Error = U::Error;
803815

@@ -810,9 +822,10 @@ where
810822
// Infallible conversions are semantically equivalent to fallible conversions
811823
// with an uninhabited error type.
812824
#[stable(feature = "try_from", since = "1.34.0")]
813-
impl<T, U> TryFrom<U> for T
825+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
826+
impl<T, U> const TryFrom<U> for T
814827
where
815-
U: Into<T>,
828+
U: ~const Into<T>,
816829
{
817830
type Error = Infallible;
818831

tests/ui/specialization/issue-111232.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
#![feature(min_specialization)]
2+
#![feature(const_trait_impl)]
3+
4+
trait From<T> {
5+
fn from(t: T) -> Self;
6+
}
7+
8+
impl<T> From<T> for T {
9+
fn from(t: T) -> T { t }
10+
}
211

312
struct S;
413

tests/ui/specialization/issue-111232.stderr

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
error[E0520]: `from` specializes an item from a parent `impl`, but that item is not marked `default`
2-
--> $DIR/issue-111232.rs:6:5
2+
--> $DIR/issue-111232.rs:15:5
33
|
4+
LL | impl<T> From<T> for T {
5+
| --------------------- parent `impl` is here
6+
...
47
LL | fn from(s: S) -> S {
5-
| ^^^^^^^^^^^^^^^^^^
8+
| ^^^^^^^^^^^^^^^^^^ cannot specialize default item `from`
69
|
7-
= note: parent implementation is in crate `core`
10+
= note: to specialize, `from` in the parent `impl` must be marked `default`
811

912
error: aborting due to 1 previous error
1013

tests/ui/traits/const-traits/non-const-op-in-closure-in-const.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//@ known-bug: #110395
1+
#![feature(const_trait_impl, const_from)]
22

3-
#![feature(const_trait_impl)]
3+
//@ check-pass
44

55
#[const_trait]
66
trait Convert<T> {

tests/ui/traits/const-traits/non-const-op-in-closure-in-const.stderr

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)