Skip to content

Commit c6007fd

Browse files
committed
Auto merge of rust-lang#86853 - usbalbin:const_try, r=oli-obk
Constify ?-operator for Result and Option Try to make `?`-operator usable in `const fn` with `Result` and `Option`, see rust-lang#74935 . Note that the try-operator itself was constified in rust-lang#87237. TODO * [x] Add tests for const T -> T conversions * [x] cleanup commits * [x] Remove `#![allow(incomplete_features)]` * [?] Await decision in rust-lang#86808 - I'm not sure * [x] Await support for parsing `~const` in bootstrapping compiler * [x] Tracking issue(s)? - rust-lang#88674
2 parents 69c1c6a + 29029c0 commit c6007fd

File tree

7 files changed

+58
-8
lines changed

7 files changed

+58
-8
lines changed

library/core/src/convert/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,10 @@ where
532532

533533
// From implies Into
534534
#[stable(feature = "rust1", since = "1.0.0")]
535-
impl<T, U> Into<U> for T
535+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
536+
impl<T, U> const Into<U> for T
536537
where
537-
U: From<T>,
538+
U: ~const From<T>,
538539
{
539540
fn into(self) -> U {
540541
U::from(self)
@@ -543,7 +544,8 @@ where
543544

544545
// From (and thus Into) is reflexive
545546
#[stable(feature = "rust1", since = "1.0.0")]
546-
impl<T> From<T> for T {
547+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
548+
impl<T> const From<T> for T {
547549
fn from(t: T) -> T {
548550
t
549551
}

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#![feature(const_float_bits_conv)]
8383
#![feature(const_float_classify)]
8484
#![feature(const_heap)]
85+
#![feature(const_convert)]
8586
#![feature(const_inherent_unchecked_arith)]
8687
#![feature(const_int_unchecked_arith)]
8788
#![feature(const_intrinsic_copy)]

library/core/src/option.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2019,7 +2019,8 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
20192019
}
20202020

20212021
#[unstable(feature = "try_trait_v2", issue = "84277")]
2022-
impl<T> ops::Try for Option<T> {
2022+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
2023+
impl<T> const ops::Try for Option<T> {
20232024
type Output = T;
20242025
type Residual = Option<convert::Infallible>;
20252026

@@ -2038,7 +2039,8 @@ impl<T> ops::Try for Option<T> {
20382039
}
20392040

20402041
#[unstable(feature = "try_trait_v2", issue = "84277")]
2041-
impl<T> ops::FromResidual for Option<T> {
2042+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
2043+
impl<T> const ops::FromResidual for Option<T> {
20422044
#[inline]
20432045
fn from_residual(residual: Option<convert::Infallible>) -> Self {
20442046
match residual {

library/core/src/result.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1889,7 +1889,8 @@ impl<A, E, V: FromIterator<A>> FromIterator<Result<A, E>> for Result<V, E> {
18891889
}
18901890

18911891
#[unstable(feature = "try_trait_v2", issue = "84277")]
1892-
impl<T, E> ops::Try for Result<T, E> {
1892+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
1893+
impl<T, E> const ops::Try for Result<T, E> {
18931894
type Output = T;
18941895
type Residual = Result<convert::Infallible, E>;
18951896

@@ -1908,7 +1909,10 @@ impl<T, E> ops::Try for Result<T, E> {
19081909
}
19091910

19101911
#[unstable(feature = "try_trait_v2", issue = "84277")]
1911-
impl<T, E, F: From<E>> ops::FromResidual<Result<convert::Infallible, E>> for Result<T, F> {
1912+
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
1913+
impl<T, E, F: ~const From<E>> const ops::FromResidual<Result<convert::Infallible, E>>
1914+
for Result<T, F>
1915+
{
19121916
#[inline]
19131917
fn from_residual(residual: Result<convert::Infallible, E>) -> Self {
19141918
match residual {

library/core/tests/convert.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#[test]
2+
fn convert() {
3+
const fn from(x: i32) -> i32 {
4+
i32::from(x)
5+
}
6+
7+
const FOO: i32 = from(42);
8+
assert_eq!(FOO, 42);
9+
10+
const fn into(x: Vec<String>) -> Vec<String> {
11+
x.into()
12+
}
13+
14+
const BAR: Vec<String> = into(Vec::new());
15+
assert_eq!(BAR, Vec::<String>::new());
16+
}

library/core/tests/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
#![feature(cfg_target_has_atomic)]
1010
#![feature(const_assume)]
1111
#![feature(const_cell_into_inner)]
12+
#![feature(const_convert)]
1213
#![feature(const_maybe_uninit_assume_init)]
14+
#![feature(const_num_from_num)]
1315
#![feature(const_ptr_read)]
1416
#![feature(const_ptr_write)]
1517
#![feature(const_ptr_offset)]
1618
#![feature(const_trait_impl)]
17-
#![feature(const_num_from_num)]
1819
#![feature(core_intrinsics)]
1920
#![feature(core_private_bignum)]
2021
#![feature(core_private_diy_float)]
@@ -83,6 +84,7 @@ mod char;
8384
mod clone;
8485
mod cmp;
8586
mod const_ptr;
87+
mod convert;
8688
mod fmt;
8789
mod hash;
8890
mod intrinsics;

src/test/ui/consts/try-operator.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// run-pass
2+
3+
#![feature(try_trait_v2)]
4+
#![feature(const_trait_impl)]
5+
#![feature(const_try)]
6+
#![feature(const_convert)]
7+
8+
fn main() {
9+
const fn result() -> Result<bool, ()> {
10+
Err(())?;
11+
Ok(true)
12+
}
13+
14+
const FOO: Result<bool, ()> = result();
15+
assert_eq!(Err(()), FOO);
16+
17+
const fn option() -> Option<()> {
18+
None?;
19+
Some(())
20+
}
21+
const BAR: Option<()> = option();
22+
assert_eq!(None, BAR);
23+
}

0 commit comments

Comments
 (0)