Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2d85a1

Browse files
committedOct 12, 2024
Stabilize const_option
This makes the following API stable in const contexts: impl<T> Option<T> { pub const fn as_mut(&mut self) -> Option<&mut T>; pub const fn expect(self, msg: &str) -> T; pub const fn unwrap(self) -> T; pub const unsafe fn unwrap_unchecked(self) -> T; pub const fn take(&mut self) -> Option<T>; pub const fn replace(&mut self, value: T) -> Option<T>; } impl<T> Option<&T> { pub const fn copied(self) -> Option<T> where T: Copy; } impl<T> Option<&mut T> { pub const fn copied(self) -> Option<T> where T: Copy; } impl<T, E> Option<Result<T, E>> { pub const fn transpose(self) -> Result<Option<T>, E> } impl<T> Option<Option<T>> { pub const fn flatten(self) -> Option<T>; } The following functions make use of the unstable `const_precise_live_drops` feature: - `expect` - `unwrap` - `unwrap_unchecked` - `transpose` - `flatten` Fixes: <rust-lang#67441>
1 parent 7420c36 commit a2d85a1

File tree

7 files changed

+19
-18
lines changed

7 files changed

+19
-18
lines changed
 

‎alloc/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@
112112
#![feature(const_eval_select)]
113113
#![feature(const_heap)]
114114
#![feature(const_maybe_uninit_write)]
115-
#![feature(const_option)]
116115
#![feature(const_pin)]
117116
#![feature(const_size_of_val)]
118117
#![feature(const_vec_string_slice)]

‎core/src/array/ascii.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ impl<const N: usize> [u8; N] {
99
///
1010
/// ```
1111
/// #![feature(ascii_char)]
12-
/// #![feature(const_option)]
1312
///
1413
/// const HEX_DIGITS: [std::ascii::Char; 16] =
1514
/// *b"0123456789abcdef".as_ascii().unwrap();

‎core/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@
131131
#![feature(const_maybe_uninit_assume_init)]
132132
#![feature(const_nonnull_new)]
133133
#![feature(const_num_midpoint)]
134-
#![feature(const_option)]
135134
#![feature(const_option_ext)]
136135
#![feature(const_pin)]
137136
#![feature(const_pointer_is_aligned)]

‎core/src/option.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,8 @@ impl<T> Option<T> {
723723
/// ```
724724
#[inline]
725725
#[stable(feature = "rust1", since = "1.0.0")]
726-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
726+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
727+
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
727728
pub const fn as_mut(&mut self) -> Option<&mut T> {
728729
match *self {
729730
Some(ref mut x) => Some(x),
@@ -924,7 +925,8 @@ impl<T> Option<T> {
924925
#[track_caller]
925926
#[stable(feature = "rust1", since = "1.0.0")]
926927
#[cfg_attr(not(test), rustc_diagnostic_item = "option_expect")]
927-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
928+
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
929+
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
928930
pub const fn expect(self, msg: &str) -> T {
929931
match self {
930932
Some(val) => val,
@@ -962,7 +964,8 @@ impl<T> Option<T> {
962964
#[track_caller]
963965
#[stable(feature = "rust1", since = "1.0.0")]
964966
#[cfg_attr(not(test), rustc_diagnostic_item = "option_unwrap")]
965-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
967+
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
968+
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
966969
pub const fn unwrap(self) -> T {
967970
match self {
968971
Some(val) => val,
@@ -1069,7 +1072,8 @@ impl<T> Option<T> {
10691072
#[inline]
10701073
#[track_caller]
10711074
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
1072-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
1075+
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
1076+
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
10731077
pub const unsafe fn unwrap_unchecked(self) -> T {
10741078
match self {
10751079
Some(val) => val,
@@ -1712,7 +1716,8 @@ impl<T> Option<T> {
17121716
/// ```
17131717
#[inline]
17141718
#[stable(feature = "rust1", since = "1.0.0")]
1715-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
1719+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
1720+
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
17161721
pub const fn take(&mut self) -> Option<T> {
17171722
// FIXME(const-hack) replace `mem::replace` by `mem::take` when the latter is const ready
17181723
mem::replace(self, None)
@@ -1769,8 +1774,9 @@ impl<T> Option<T> {
17691774
/// assert_eq!(old, None);
17701775
/// ```
17711776
#[inline]
1772-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
17731777
#[stable(feature = "option_replace", since = "1.31.0")]
1778+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
1779+
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
17741780
pub const fn replace(&mut self, value: T) -> Option<T> {
17751781
mem::replace(self, Some(value))
17761782
}
@@ -1878,7 +1884,7 @@ impl<T> Option<&T> {
18781884
/// ```
18791885
#[must_use = "`self` will be dropped if the result is not used"]
18801886
#[stable(feature = "copied", since = "1.35.0")]
1881-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
1887+
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
18821888
pub const fn copied(self) -> Option<T>
18831889
where
18841890
T: Copy,
@@ -1931,7 +1937,8 @@ impl<T> Option<&mut T> {
19311937
/// ```
19321938
#[must_use = "`self` will be dropped if the result is not used"]
19331939
#[stable(feature = "copied", since = "1.35.0")]
1934-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
1940+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
1941+
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
19351942
pub const fn copied(self) -> Option<T>
19361943
where
19371944
T: Copy,
@@ -1986,7 +1993,8 @@ impl<T, E> Option<Result<T, E>> {
19861993
/// ```
19871994
#[inline]
19881995
#[stable(feature = "transpose_result", since = "1.33.0")]
1989-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
1996+
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
1997+
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
19901998
pub const fn transpose(self) -> Result<Option<T>, E> {
19911999
match self {
19922000
Some(Ok(x)) => Ok(Some(x)),
@@ -2009,7 +2017,6 @@ const fn unwrap_failed() -> ! {
20092017
#[cfg_attr(feature = "panic_immediate_abort", inline)]
20102018
#[cold]
20112019
#[track_caller]
2012-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
20132020
const fn expect_failed(msg: &str) -> ! {
20142021
panic_display(&msg)
20152022
}
@@ -2534,7 +2541,8 @@ impl<T> Option<Option<T>> {
25342541
/// ```
25352542
#[inline]
25362543
#[stable(feature = "option_flattening", since = "1.40.0")]
2537-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
2544+
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
2545+
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
25382546
pub const fn flatten(self) -> Option<T> {
25392547
// FIXME(const-hack): could be written with `and_then`
25402548
match self {

‎core/src/ptr/non_null.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,6 @@ impl<T: ?Sized> NonNull<T> {
12111211
///
12121212
/// ```
12131213
/// #![feature(const_nonnull_new)]
1214-
/// #![feature(const_option)]
12151214
/// #![feature(const_pointer_is_aligned)]
12161215
/// use std::ptr::NonNull;
12171216
///
@@ -1264,7 +1263,6 @@ impl<T: ?Sized> NonNull<T> {
12641263
///
12651264
/// ```
12661265
/// #![feature(const_pointer_is_aligned)]
1267-
/// #![feature(const_option)]
12681266
/// #![feature(const_nonnull_new)]
12691267
/// use std::ptr::NonNull;
12701268
///

‎core/src/time.rs

-1
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,6 @@ impl Duration {
626626
/// ```
627627
#[stable(feature = "duration_abs_diff", since = "1.81.0")]
628628
#[rustc_const_stable(feature = "duration_abs_diff", since = "1.81.0")]
629-
#[rustc_allow_const_fn_unstable(const_option)]
630629
#[must_use = "this returns the result of the operation, \
631630
without modifying the original"]
632631
#[inline]

‎core/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#![feature(const_ipv6)]
2525
#![feature(const_likely)]
2626
#![feature(const_nonnull_new)]
27-
#![feature(const_option)]
2827
#![feature(const_option_ext)]
2928
#![feature(const_pin)]
3029
#![feature(const_pointer_is_aligned)]

0 commit comments

Comments
 (0)
Failed to load comments.