diff --git a/library/core/src/convert/mod.rs b/library/core/src/convert/mod.rs index 5aa53deee34..1c2e673d604 100644 --- a/library/core/src/convert/mod.rs +++ b/library/core/src/convert/mod.rs @@ -534,9 +534,10 @@ where // From implies Into #[stable(feature = "rust1", since = "1.0.0")] -impl Into for T +#[rustc_const_unstable(feature = "const_convert", issue = "88674")] +impl const Into for T where - U: From, + U: ~const From, { fn into(self) -> U { U::from(self) diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index d9a40a9b2ec..04996368064 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -112,6 +112,7 @@ #![feature(const_float_classify)] #![feature(const_fmt_arguments_new)] #![feature(const_heap)] +#![feature(const_convert)] #![feature(const_inherent_unchecked_arith)] #![feature(const_int_unchecked_arith)] #![feature(const_intrinsic_copy)] diff --git a/library/core/src/option.rs b/library/core/src/option.rs index a6e7257448c..381e2d6eed3 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -2077,7 +2077,8 @@ impl> FromIterator> for Option { } #[unstable(feature = "try_trait_v2", issue = "84277")] -impl ops::Try for Option { +#[rustc_const_unstable(feature = "const_convert", issue = "88674")] +impl const ops::Try for Option { type Output = T; type Residual = Option; @@ -2096,6 +2097,7 @@ impl ops::Try for Option { } #[unstable(feature = "try_trait_v2", issue = "84277")] +#[rustc_const_unstable(feature = "const_convert", issue = "88674")] impl const ops::FromResidual for Option { #[inline] fn from_residual(residual: Option) -> Self { diff --git a/library/core/src/result.rs b/library/core/src/result.rs index ab067d57d08..8ef5ffb962b 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1945,7 +1945,8 @@ impl> FromIterator> for Result { } #[unstable(feature = "try_trait_v2", issue = "84277")] -impl ops::Try for Result { +#[rustc_const_unstable(feature = "const_convert", issue = "88674")] +impl const ops::Try for Result { type Output = T; type Residual = Result; @@ -1964,7 +1965,10 @@ impl ops::Try for Result { } #[unstable(feature = "try_trait_v2", issue = "84277")] -impl> ops::FromResidual> for Result { +#[rustc_const_unstable(feature = "const_convert", issue = "88674")] +impl> const ops::FromResidual> + for Result +{ #[inline] fn from_residual(residual: Result) -> Self { match residual { diff --git a/library/core/tests/convert.rs b/library/core/tests/convert.rs new file mode 100644 index 00000000000..f1048f4cf09 --- /dev/null +++ b/library/core/tests/convert.rs @@ -0,0 +1,16 @@ +#[test] +fn convert() { + const fn from(x: i32) -> i32 { + i32::from(x) + } + + const FOO: i32 = from(42); + assert_eq!(FOO, 42); + + const fn into(x: Vec) -> Vec { + x.into() + } + + const BAR: Vec = into(Vec::new()); + assert_eq!(BAR, Vec::::new()); +} diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 012e6e5b57a..b41d3e09df8 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -12,11 +12,11 @@ #![feature(const_convert)] #![feature(const_maybe_uninit_as_mut_ptr)] #![feature(const_maybe_uninit_assume_init)] +#![feature(const_num_from_num)] #![feature(const_ptr_read)] #![feature(const_ptr_write)] #![feature(const_ptr_offset)] #![feature(const_trait_impl)] -#![feature(const_num_from_num)] #![feature(core_intrinsics)] #![feature(core_private_bignum)] #![feature(core_private_diy_float)] @@ -96,6 +96,7 @@ mod char; mod clone; mod cmp; mod const_ptr; +mod convert; mod fmt; mod future; mod hash; diff --git a/src/test/ui/consts/not_const_clusure_in_const.rs b/src/test/ui/consts/not_const_clusure_in_const.rs deleted file mode 100644 index fd2cfa442d3..00000000000 --- a/src/test/ui/consts/not_const_clusure_in_const.rs +++ /dev/null @@ -1,9 +0,0 @@ -// run-pass - -const _FOO: fn() -> String = || "foo".into(); - -pub fn bar() -> fn() -> String { - || "bar".into() -} - -fn main(){} diff --git a/src/test/ui/consts/try-operator.rs b/src/test/ui/consts/try-operator.rs new file mode 100644 index 00000000000..fe43b132cbd --- /dev/null +++ b/src/test/ui/consts/try-operator.rs @@ -0,0 +1,23 @@ +// run-pass + +#![feature(try_trait_v2)] +#![feature(const_trait_impl)] +#![feature(const_try)] +#![feature(const_convert)] + +fn main() { + const fn result() -> Result { + Err(())?; + Ok(true) + } + + const FOO: Result = result(); + assert_eq!(Err(()), FOO); + + const fn option() -> Option<()> { + None?; + Some(()) + } + const BAR: Option<()> = option(); + assert_eq!(None, BAR); +}