Skip to content

Commit af0594f

Browse files
authored
Rollup merge of rust-lang#76135 - CDirkx:const-option, r=dtolnay
Stabilize some Option methods as const Stabilize the following methods of `Option` as const: - `is_some` - `is_none` - `as_ref` These methods are currently const under the unstable feature `const_option` (tracking issue: rust-lang#67441). I believe these methods to be eligible for stabilization because of the stabilization of rust-lang#49146 (Allow if and match in constants) and the trivial implementations, see also: [PR#75463](rust-lang#75463). Related: rust-lang#76225
2 parents 47fc3ed + 04e4a39 commit af0594f

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

library/core/src/option.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl<T> Option<T> {
175175
/// ```
176176
#[must_use = "if you intended to assert that this has a value, consider `.unwrap()` instead"]
177177
#[inline]
178-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
178+
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
179179
#[stable(feature = "rust1", since = "1.0.0")]
180180
pub const fn is_some(&self) -> bool {
181181
matches!(*self, Some(_))
@@ -195,7 +195,7 @@ impl<T> Option<T> {
195195
#[must_use = "if you intended to assert that this doesn't have a value, consider \
196196
`.and_then(|| panic!(\"`Option` had a value when expected `None`\"))` instead"]
197197
#[inline]
198-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
198+
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
199199
#[stable(feature = "rust1", since = "1.0.0")]
200200
pub const fn is_none(&self) -> bool {
201201
!self.is_some()
@@ -254,7 +254,7 @@ impl<T> Option<T> {
254254
/// println!("still can print text: {:?}", text);
255255
/// ```
256256
#[inline]
257-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
257+
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
258258
#[stable(feature = "rust1", since = "1.0.0")]
259259
pub const fn as_ref(&self) -> Option<&T> {
260260
match *self {

library/core/tests/option.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,19 @@ fn test_replace() {
356356
assert_eq!(x, Some(3));
357357
assert_eq!(old, None);
358358
}
359+
360+
#[test]
361+
fn option_const() {
362+
// test that the methods of `Option` are usable in a const context
363+
364+
const OPTION: Option<usize> = Some(32);
365+
366+
const REF: Option<&usize> = OPTION.as_ref();
367+
assert_eq!(REF, Some(&32));
368+
369+
const IS_SOME: bool = OPTION.is_some();
370+
assert!(IS_SOME);
371+
372+
const IS_NONE: bool = OPTION.is_none();
373+
assert!(!IS_NONE);
374+
}

src/test/ui/consts/const-option.rs

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

0 commit comments

Comments
 (0)