From db109c662e4bc4a8d81aaa69e6bd37c43d191259 Mon Sep 17 00:00:00 2001 From: CDirkx Date: Mon, 31 Aug 2020 02:11:48 +0200 Subject: [PATCH 1/2] Stabilize some Option methods as const Stabilize the following methods of `Option` as const: - `is_some` - `is_none` - `as_ref` Possible because of stabilization of #49146 (Allow if and match in constants). --- library/core/src/option.rs | 6 +++--- src/test/ui/consts/const-option.rs | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index dd7556758be7d..fa0f917c79dca 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -175,7 +175,7 @@ impl Option { /// ``` #[must_use = "if you intended to assert that this has a value, consider `.unwrap()` instead"] #[inline] - #[rustc_const_unstable(feature = "const_option", issue = "67441")] + #[rustc_const_stable(feature = "const_option", since = "1.48.0")] #[stable(feature = "rust1", since = "1.0.0")] pub const fn is_some(&self) -> bool { matches!(*self, Some(_)) @@ -195,7 +195,7 @@ impl Option { #[must_use = "if you intended to assert that this doesn't have a value, consider \ `.and_then(|| panic!(\"`Option` had a value when expected `None`\"))` instead"] #[inline] - #[rustc_const_unstable(feature = "const_option", issue = "67441")] + #[rustc_const_stable(feature = "const_option", since = "1.48.0")] #[stable(feature = "rust1", since = "1.0.0")] pub const fn is_none(&self) -> bool { !self.is_some() @@ -254,7 +254,7 @@ impl Option { /// println!("still can print text: {:?}", text); /// ``` #[inline] - #[rustc_const_unstable(feature = "const_option", issue = "67441")] + #[rustc_const_stable(feature = "const_option", since = "1.48.0")] #[stable(feature = "rust1", since = "1.0.0")] pub const fn as_ref(&self) -> Option<&T> { match *self { diff --git a/src/test/ui/consts/const-option.rs b/src/test/ui/consts/const-option.rs index fbf20b9db6741..793f78c8d20fa 100644 --- a/src/test/ui/consts/const-option.rs +++ b/src/test/ui/consts/const-option.rs @@ -1,7 +1,5 @@ // run-pass -#![feature(const_option)] - const X: Option = Some(32); const Y: Option<&i32> = X.as_ref(); From 04e4a391985994f7ccb579eae530b52d704eb887 Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Fri, 4 Sep 2020 00:13:25 +0200 Subject: [PATCH 2/2] Move const tests for `Option` to `library\core` Part of #76268 --- library/core/tests/option.rs | 16 ++++++++++++++++ src/test/ui/consts/const-option.rs | 12 ------------ 2 files changed, 16 insertions(+), 12 deletions(-) delete mode 100644 src/test/ui/consts/const-option.rs diff --git a/library/core/tests/option.rs b/library/core/tests/option.rs index fa308160fc228..5049ba954c7a2 100644 --- a/library/core/tests/option.rs +++ b/library/core/tests/option.rs @@ -357,3 +357,19 @@ fn test_replace() { assert_eq!(x, Some(3)); assert_eq!(old, None); } + +#[test] +fn option_const() { + // test that the methods of `Option` are usable in a const context + + const OPTION: Option = Some(32); + + const REF: Option<&usize> = OPTION.as_ref(); + assert_eq!(REF, Some(&32)); + + const IS_SOME: bool = OPTION.is_some(); + assert!(IS_SOME); + + const IS_NONE: bool = OPTION.is_none(); + assert!(!IS_NONE); +} diff --git a/src/test/ui/consts/const-option.rs b/src/test/ui/consts/const-option.rs deleted file mode 100644 index 793f78c8d20fa..0000000000000 --- a/src/test/ui/consts/const-option.rs +++ /dev/null @@ -1,12 +0,0 @@ -// run-pass - -const X: Option = Some(32); -const Y: Option<&i32> = X.as_ref(); - -const IS_SOME: bool = X.is_some(); -const IS_NONE: bool = Y.is_none(); - -fn main() { - assert!(IS_SOME); - assert!(!IS_NONE) -}