From b82aaf49132793d490bb21bada9b02fa3cfd49ac Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Sun, 4 Jul 2021 00:13:27 +0200 Subject: [PATCH 01/10] Constify identify conversions --- library/core/src/convert/mod.rs | 6 ++++-- library/core/src/lib.rs | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/library/core/src/convert/mod.rs b/library/core/src/convert/mod.rs index 1e512af48051e..78d37ebac2d0c 100644 --- a/library/core/src/convert/mod.rs +++ b/library/core/src/convert/mod.rs @@ -532,7 +532,8 @@ where // From implies Into #[stable(feature = "rust1", since = "1.0.0")] -impl Into for T +#[rustc_const_unstable(feature = "const_convert", issue = "none")] +impl const Into for T where U: From, { @@ -543,7 +544,8 @@ where // From (and thus Into) is reflexive #[stable(feature = "rust1", since = "1.0.0")] -impl From for T { +#[rustc_const_unstable(feature = "const_convert", issue = "none")] +impl const From for T { fn from(t: T) -> T { t } diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 265ba9f1bb91b..9c4429d320f1f 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -82,6 +82,7 @@ #![feature(const_float_bits_conv)] #![feature(const_float_classify)] #![feature(const_heap)] +#![feature(const_convert)] #![feature(const_inherent_unchecked_arith)] #![feature(const_int_unchecked_arith)] #![feature(const_intrinsic_copy)] From b85107ec711aa4e7de8ffe3deab6f88800e2a680 Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Mon, 2 Aug 2021 14:42:29 +0200 Subject: [PATCH 02/10] Add tests for feature(const_identity_convert) --- src/test/ui/consts/convert.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/test/ui/consts/convert.rs diff --git a/src/test/ui/consts/convert.rs b/src/test/ui/consts/convert.rs new file mode 100644 index 0000000000000..e10374070acf1 --- /dev/null +++ b/src/test/ui/consts/convert.rs @@ -0,0 +1,20 @@ +// run-pass + +#![feature(const_trait_impl)] +#![feature(const_identity_convert)] + +fn main() { + 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()); +} From 88258c02a9c70fb562e4c646386411f4e0a4a80a Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Sun, 4 Jul 2021 00:58:32 +0200 Subject: [PATCH 03/10] Constly impl TryV2 and FromResidual for Result --- library/core/src/result.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 092e6544342b7..d3587a1b2b8d2 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1889,7 +1889,8 @@ impl> FromIterator> for Result { } #[unstable(feature = "try_trait_v2", issue = "84277")] -impl ops::Try for Result { +#[rustc_const_unstable(feature = "const_convert", issue = "none")] +impl const ops::Try for Result { type Output = T; type Residual = Result; @@ -1908,7 +1909,8 @@ impl ops::Try for Result { } #[unstable(feature = "try_trait_v2", issue = "84277")] -impl> ops::FromResidual> for Result { +#[rustc_const_unstable(feature = "const_convert", issue = "none")] +impl> const ops::FromResidual> for Result { #[inline] fn from_residual(residual: Result) -> Self { match residual { From 991b375d87c1d171f3d36dd92d9543950eefcf47 Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Sun, 4 Jul 2021 14:22:48 +0200 Subject: [PATCH 04/10] Add test for try operator on Result --- src/test/ui/consts/try-operator.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/test/ui/consts/try-operator.rs diff --git a/src/test/ui/consts/try-operator.rs b/src/test/ui/consts/try-operator.rs new file mode 100644 index 0000000000000..4767e68d41e74 --- /dev/null +++ b/src/test/ui/consts/try-operator.rs @@ -0,0 +1,16 @@ +// run-pass + +#![feature(try_trait_v2)] +#![feature(const_trait_impl)] +#![feature(const_try)] +#![feature(const_convert)] + +fn main() { + const fn foo() -> Result { + Err(())?; + Ok(true) + } + + const FOO: Result = foo(); + assert_eq!(Err(()), FOO); +} From a042705a7dd3733428528ad7d31310763552e33b Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Sun, 4 Jul 2021 19:10:21 +0200 Subject: [PATCH 05/10] Constly impl TryV2 and Residual for Option --- library/core/src/option.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 907726f0c345c..b61ab393bf914 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -2010,7 +2010,8 @@ impl> FromIterator> for Option { } #[unstable(feature = "try_trait_v2", issue = "84277")] -impl ops::Try for Option { +#[rustc_const_unstable(feature = "const_convert", issue = "none")] +impl const ops::Try for Option { type Output = T; type Residual = Option; @@ -2029,7 +2030,8 @@ impl ops::Try for Option { } #[unstable(feature = "try_trait_v2", issue = "84277")] -impl ops::FromResidual for Option { +#[rustc_const_unstable(feature = "const_convert", issue = "none")] +impl const ops::FromResidual for Option { #[inline] fn from_residual(residual: Option) -> Self { match residual { From aaf902bf8ff406f1dd300ad86b4fb5eeccb4fbe9 Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Sun, 4 Jul 2021 19:11:54 +0200 Subject: [PATCH 06/10] Add test for try operator with Option --- src/test/ui/consts/try-operator.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/test/ui/consts/try-operator.rs b/src/test/ui/consts/try-operator.rs index 4767e68d41e74..fe43b132cbd7f 100644 --- a/src/test/ui/consts/try-operator.rs +++ b/src/test/ui/consts/try-operator.rs @@ -6,11 +6,18 @@ #![feature(const_convert)] fn main() { - const fn foo() -> Result { + const fn result() -> Result { Err(())?; Ok(true) } - const FOO: Result = foo(); + const FOO: Result = result(); assert_eq!(Err(()), FOO); + + const fn option() -> Option<()> { + None?; + Some(()) + } + const BAR: Option<()> = option(); + assert_eq!(None, BAR); } From 3051bb9c811cde27bcde4052b30dfa30c99a0bf0 Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Mon, 9 Aug 2021 22:44:01 +0200 Subject: [PATCH 07/10] Move tests to library/core/tests --- {src/test/ui/consts => library/core/tests}/convert.rs | 8 ++------ library/core/tests/lib.rs | 4 +++- 2 files changed, 5 insertions(+), 7 deletions(-) rename {src/test/ui/consts => library/core/tests}/convert.rs (75%) diff --git a/src/test/ui/consts/convert.rs b/library/core/tests/convert.rs similarity index 75% rename from src/test/ui/consts/convert.rs rename to library/core/tests/convert.rs index e10374070acf1..f1048f4cf09cb 100644 --- a/src/test/ui/consts/convert.rs +++ b/library/core/tests/convert.rs @@ -1,9 +1,5 @@ -// run-pass - -#![feature(const_trait_impl)] -#![feature(const_identity_convert)] - -fn main() { +#[test] +fn convert() { const fn from(x: i32) -> i32 { i32::from(x) } diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 19bcc45108dfd..cc9d70cfbdcd3 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -9,12 +9,13 @@ #![feature(cfg_target_has_atomic)] #![feature(const_assume)] #![feature(const_cell_into_inner)] +#![feature(const_convert)] #![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)] @@ -84,6 +85,7 @@ mod char; mod clone; mod cmp; mod const_ptr; +mod convert; mod fmt; mod hash; mod intrinsics; From 92b57c047615857cb7423a830579246245457a02 Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Sun, 5 Sep 2021 22:51:25 +0200 Subject: [PATCH 08/10] Updated for new const trait bounds syntax --- library/core/src/convert/mod.rs | 2 +- library/core/src/result.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/convert/mod.rs b/library/core/src/convert/mod.rs index 78d37ebac2d0c..daf9f4674f8c4 100644 --- a/library/core/src/convert/mod.rs +++ b/library/core/src/convert/mod.rs @@ -535,7 +535,7 @@ where #[rustc_const_unstable(feature = "const_convert", issue = "none")] impl const Into for T where - U: From, + U: ~const From, { fn into(self) -> U { U::from(self) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index d3587a1b2b8d2..f7bbf0192103f 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1910,7 +1910,7 @@ impl const ops::Try for Result { #[unstable(feature = "try_trait_v2", issue = "84277")] #[rustc_const_unstable(feature = "const_convert", issue = "none")] -impl> const ops::FromResidual> for Result { +impl> const ops::FromResidual> for Result { #[inline] fn from_residual(residual: Result) -> Self { match residual { From ff1ecc0ee9e082236b5d6d652f3419b2915600b2 Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Sun, 5 Sep 2021 22:56:42 +0200 Subject: [PATCH 09/10] Add tracking issue --- library/core/src/convert/mod.rs | 4 ++-- library/core/src/option.rs | 4 ++-- library/core/src/result.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/library/core/src/convert/mod.rs b/library/core/src/convert/mod.rs index daf9f4674f8c4..72dbe845c97a9 100644 --- a/library/core/src/convert/mod.rs +++ b/library/core/src/convert/mod.rs @@ -532,7 +532,7 @@ where // From implies Into #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_convert", issue = "none")] +#[rustc_const_unstable(feature = "const_convert", issue = "88674")] impl const Into for T where U: ~const From, @@ -544,7 +544,7 @@ where // From (and thus Into) is reflexive #[stable(feature = "rust1", since = "1.0.0")] -#[rustc_const_unstable(feature = "const_convert", issue = "none")] +#[rustc_const_unstable(feature = "const_convert", issue = "88674")] impl const From for T { fn from(t: T) -> T { t diff --git a/library/core/src/option.rs b/library/core/src/option.rs index b61ab393bf914..d74771ef89a1c 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -2010,7 +2010,7 @@ impl> FromIterator> for Option { } #[unstable(feature = "try_trait_v2", issue = "84277")] -#[rustc_const_unstable(feature = "const_convert", issue = "none")] +#[rustc_const_unstable(feature = "const_convert", issue = "88674")] impl const ops::Try for Option { type Output = T; type Residual = Option; @@ -2030,7 +2030,7 @@ impl const ops::Try for Option { } #[unstable(feature = "try_trait_v2", issue = "84277")] -#[rustc_const_unstable(feature = "const_convert", issue = "none")] +#[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 f7bbf0192103f..6ed980cf53fe0 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1889,7 +1889,7 @@ impl> FromIterator> for Result { } #[unstable(feature = "try_trait_v2", issue = "84277")] -#[rustc_const_unstable(feature = "const_convert", issue = "none")] +#[rustc_const_unstable(feature = "const_convert", issue = "88674")] impl const ops::Try for Result { type Output = T; type Residual = Result; @@ -1909,7 +1909,7 @@ impl const ops::Try for Result { } #[unstable(feature = "try_trait_v2", issue = "84277")] -#[rustc_const_unstable(feature = "const_convert", issue = "none")] +#[rustc_const_unstable(feature = "const_convert", issue = "88674")] impl> const ops::FromResidual> for Result { #[inline] fn from_residual(residual: Result) -> Self { From 29029c0bc2383441412f696e430aaa3536b04e37 Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Wed, 15 Sep 2021 18:08:48 +0200 Subject: [PATCH 10/10] Fix formatting --- library/core/src/result.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 6ed980cf53fe0..bfa84ee460825 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1910,7 +1910,9 @@ impl const ops::Try for Result { #[unstable(feature = "try_trait_v2", issue = "84277")] #[rustc_const_unstable(feature = "const_convert", issue = "88674")] -impl> const ops::FromResidual> for Result { +impl> const ops::FromResidual> + for Result +{ #[inline] fn from_residual(residual: Result) -> Self { match residual {