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: <https://github.com/rust-lang/rust/issues/67441>
This commit is contained in:
Trevor Gross 2024-10-01 17:11:48 -04:00
parent 6b9676b454
commit 19f6c17df4
14 changed files with 37 additions and 29 deletions

View File

@ -37,7 +37,6 @@
#![feature(box_as_ptr)] #![feature(box_as_ptr)]
#![feature(box_patterns)] #![feature(box_patterns)]
#![feature(closure_track_caller)] #![feature(closure_track_caller)]
#![feature(const_option)]
#![feature(const_type_name)] #![feature(const_type_name)]
#![feature(core_intrinsics)] #![feature(core_intrinsics)]
#![feature(coroutines)] #![feature(coroutines)]

View File

@ -10,7 +10,6 @@
test(attr(allow(unused_variables), deny(warnings))) test(attr(allow(unused_variables), deny(warnings)))
)] )]
#![doc(rust_logo)] #![doc(rust_logo)]
#![feature(const_option)]
#![feature(core_intrinsics)] #![feature(core_intrinsics)]
#![feature(min_specialization)] #![feature(min_specialization)]
#![feature(never_type)] #![feature(never_type)]

View File

@ -112,7 +112,6 @@
#![feature(const_eval_select)] #![feature(const_eval_select)]
#![feature(const_heap)] #![feature(const_heap)]
#![feature(const_maybe_uninit_write)] #![feature(const_maybe_uninit_write)]
#![feature(const_option)]
#![feature(const_pin)] #![feature(const_pin)]
#![feature(const_size_of_val)] #![feature(const_size_of_val)]
#![feature(const_vec_string_slice)] #![feature(const_vec_string_slice)]

View File

@ -9,7 +9,6 @@ impl<const N: usize> [u8; N] {
/// ///
/// ``` /// ```
/// #![feature(ascii_char)] /// #![feature(ascii_char)]
/// #![feature(const_option)]
/// ///
/// const HEX_DIGITS: [std::ascii::Char; 16] = /// const HEX_DIGITS: [std::ascii::Char; 16] =
/// *b"0123456789abcdef".as_ascii().unwrap(); /// *b"0123456789abcdef".as_ascii().unwrap();

View File

@ -131,7 +131,6 @@
#![feature(const_maybe_uninit_assume_init)] #![feature(const_maybe_uninit_assume_init)]
#![feature(const_nonnull_new)] #![feature(const_nonnull_new)]
#![feature(const_num_midpoint)] #![feature(const_num_midpoint)]
#![feature(const_option)]
#![feature(const_option_ext)] #![feature(const_option_ext)]
#![feature(const_pin)] #![feature(const_pin)]
#![feature(const_pointer_is_aligned)] #![feature(const_pointer_is_aligned)]

View File

@ -723,7 +723,8 @@ impl<T> Option<T> {
/// ``` /// ```
#[inline] #[inline]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")] #[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
pub const fn as_mut(&mut self) -> Option<&mut T> { pub const fn as_mut(&mut self) -> Option<&mut T> {
match *self { match *self {
Some(ref mut x) => Some(x), Some(ref mut x) => Some(x),
@ -924,7 +925,8 @@ impl<T> Option<T> {
#[track_caller] #[track_caller]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "option_expect")] #[cfg_attr(not(test), rustc_diagnostic_item = "option_expect")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")] #[rustc_allow_const_fn_unstable(const_precise_live_drops)]
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
pub const fn expect(self, msg: &str) -> T { pub const fn expect(self, msg: &str) -> T {
match self { match self {
Some(val) => val, Some(val) => val,
@ -962,7 +964,8 @@ impl<T> Option<T> {
#[track_caller] #[track_caller]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "option_unwrap")] #[cfg_attr(not(test), rustc_diagnostic_item = "option_unwrap")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")] #[rustc_allow_const_fn_unstable(const_precise_live_drops)]
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
pub const fn unwrap(self) -> T { pub const fn unwrap(self) -> T {
match self { match self {
Some(val) => val, Some(val) => val,
@ -1069,7 +1072,8 @@ impl<T> Option<T> {
#[inline] #[inline]
#[track_caller] #[track_caller]
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")] #[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")] #[rustc_allow_const_fn_unstable(const_precise_live_drops)]
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
pub const unsafe fn unwrap_unchecked(self) -> T { pub const unsafe fn unwrap_unchecked(self) -> T {
match self { match self {
Some(val) => val, Some(val) => val,
@ -1712,7 +1716,8 @@ impl<T> Option<T> {
/// ``` /// ```
#[inline] #[inline]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")] #[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
pub const fn take(&mut self) -> Option<T> { pub const fn take(&mut self) -> Option<T> {
// FIXME(const-hack) replace `mem::replace` by `mem::take` when the latter is const ready // FIXME(const-hack) replace `mem::replace` by `mem::take` when the latter is const ready
mem::replace(self, None) mem::replace(self, None)
@ -1769,8 +1774,9 @@ impl<T> Option<T> {
/// assert_eq!(old, None); /// assert_eq!(old, None);
/// ``` /// ```
#[inline] #[inline]
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
#[stable(feature = "option_replace", since = "1.31.0")] #[stable(feature = "option_replace", since = "1.31.0")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
pub const fn replace(&mut self, value: T) -> Option<T> { pub const fn replace(&mut self, value: T) -> Option<T> {
mem::replace(self, Some(value)) mem::replace(self, Some(value))
} }
@ -1878,7 +1884,7 @@ impl<T> Option<&T> {
/// ``` /// ```
#[must_use = "`self` will be dropped if the result is not used"] #[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "copied", since = "1.35.0")] #[stable(feature = "copied", since = "1.35.0")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")] #[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
pub const fn copied(self) -> Option<T> pub const fn copied(self) -> Option<T>
where where
T: Copy, T: Copy,
@ -1931,7 +1937,8 @@ impl<T> Option<&mut T> {
/// ``` /// ```
#[must_use = "`self` will be dropped if the result is not used"] #[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "copied", since = "1.35.0")] #[stable(feature = "copied", since = "1.35.0")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")] #[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
pub const fn copied(self) -> Option<T> pub const fn copied(self) -> Option<T>
where where
T: Copy, T: Copy,
@ -1986,7 +1993,8 @@ impl<T, E> Option<Result<T, E>> {
/// ``` /// ```
#[inline] #[inline]
#[stable(feature = "transpose_result", since = "1.33.0")] #[stable(feature = "transpose_result", since = "1.33.0")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")] #[rustc_allow_const_fn_unstable(const_precise_live_drops)]
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
pub const fn transpose(self) -> Result<Option<T>, E> { pub const fn transpose(self) -> Result<Option<T>, E> {
match self { match self {
Some(Ok(x)) => Ok(Some(x)), Some(Ok(x)) => Ok(Some(x)),
@ -2009,7 +2017,6 @@ const fn unwrap_failed() -> ! {
#[cfg_attr(feature = "panic_immediate_abort", inline)] #[cfg_attr(feature = "panic_immediate_abort", inline)]
#[cold] #[cold]
#[track_caller] #[track_caller]
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
const fn expect_failed(msg: &str) -> ! { const fn expect_failed(msg: &str) -> ! {
panic_display(&msg) panic_display(&msg)
} }
@ -2534,7 +2541,8 @@ impl<T> Option<Option<T>> {
/// ``` /// ```
#[inline] #[inline]
#[stable(feature = "option_flattening", since = "1.40.0")] #[stable(feature = "option_flattening", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")] #[rustc_allow_const_fn_unstable(const_precise_live_drops)]
#[rustc_const_stable(feature = "const_option", since = "CURRENT_RUSTC_VERSION")]
pub const fn flatten(self) -> Option<T> { pub const fn flatten(self) -> Option<T> {
// FIXME(const-hack): could be written with `and_then` // FIXME(const-hack): could be written with `and_then`
match self { match self {

View File

@ -1211,7 +1211,6 @@ impl<T: ?Sized> NonNull<T> {
/// ///
/// ``` /// ```
/// #![feature(const_nonnull_new)] /// #![feature(const_nonnull_new)]
/// #![feature(const_option)]
/// #![feature(const_pointer_is_aligned)] /// #![feature(const_pointer_is_aligned)]
/// use std::ptr::NonNull; /// use std::ptr::NonNull;
/// ///
@ -1264,7 +1263,6 @@ impl<T: ?Sized> NonNull<T> {
/// ///
/// ``` /// ```
/// #![feature(const_pointer_is_aligned)] /// #![feature(const_pointer_is_aligned)]
/// #![feature(const_option)]
/// #![feature(const_nonnull_new)] /// #![feature(const_nonnull_new)]
/// use std::ptr::NonNull; /// use std::ptr::NonNull;
/// ///

View File

@ -626,7 +626,6 @@ impl Duration {
/// ``` /// ```
#[stable(feature = "duration_abs_diff", since = "1.81.0")] #[stable(feature = "duration_abs_diff", since = "1.81.0")]
#[rustc_const_stable(feature = "duration_abs_diff", since = "1.81.0")] #[rustc_const_stable(feature = "duration_abs_diff", since = "1.81.0")]
#[rustc_allow_const_fn_unstable(const_option)]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[inline] #[inline]

View File

@ -24,7 +24,6 @@
#![feature(const_ipv6)] #![feature(const_ipv6)]
#![feature(const_likely)] #![feature(const_likely)]
#![feature(const_nonnull_new)] #![feature(const_nonnull_new)]
#![feature(const_option)]
#![feature(const_option_ext)] #![feature(const_option_ext)]
#![feature(const_pin)] #![feature(const_pin)]
#![feature(const_pointer_is_aligned)] #![feature(const_pointer_is_aligned)]

View File

@ -3,7 +3,7 @@
#![allow(dead_code, incomplete_features)] #![allow(dead_code, incomplete_features)]
#![warn(clippy::doc_markdown)] #![warn(clippy::doc_markdown)]
#![feature(custom_inner_attributes, generic_const_exprs, const_option)] #![feature(custom_inner_attributes, generic_const_exprs)]
#![rustfmt::skip] #![rustfmt::skip]
/// The `foo_bar` function does _nothing_. See also `foo::bar`. (note the dot there) /// The `foo_bar` function does _nothing_. See also `foo::bar`. (note the dot there)

View File

@ -3,7 +3,7 @@
#![allow(dead_code, incomplete_features)] #![allow(dead_code, incomplete_features)]
#![warn(clippy::doc_markdown)] #![warn(clippy::doc_markdown)]
#![feature(custom_inner_attributes, generic_const_exprs, const_option)] #![feature(custom_inner_attributes, generic_const_exprs)]
#![rustfmt::skip] #![rustfmt::skip]
/// The foo_bar function does _nothing_. See also foo::bar. (note the dot there) /// The foo_bar function does _nothing_. See also foo::bar. (note the dot there)

View File

@ -1,6 +1,5 @@
#![feature(rustc_private)] #![feature(rustc_private)]
#![feature(cell_update)] #![feature(cell_update)]
#![feature(const_option)]
#![feature(float_gamma)] #![feature(float_gamma)]
#![feature(map_try_insert)] #![feature(map_try_insert)]
#![feature(never_type)] #![feature(never_type)]

View File

@ -1,11 +1,15 @@
//@ check-fail //@ check-fail
// Verify that panicking `const_option` methods do the correct thing
#![feature(const_option)]
const FOO: i32 = Some(42i32).unwrap(); const FOO: i32 = Some(42i32).unwrap();
const BAR: i32 = Option::<i32>::None.unwrap(); const BAR: i32 = Option::<i32>::None.unwrap();
//~^ERROR: evaluation of constant value failed //~^ ERROR: evaluation of constant value failed
//~| NOTE: the evaluated program panicked
const BAZ: i32 = Option::<i32>::None.expect("absolutely not!");
//~^ ERROR: evaluation of constant value failed
//~| NOTE: absolutely not!
fn main() { fn main() {
println!("{}", FOO); println!("{}", FOO);

View File

@ -1,9 +1,15 @@
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/const-unwrap.rs:7:18 --> $DIR/const-unwrap.rs:6:18
| |
LL | const BAR: i32 = Option::<i32>::None.unwrap(); LL | const BAR: i32 = Option::<i32>::None.unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'called `Option::unwrap()` on a `None` value', $DIR/const-unwrap.rs:7:38 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'called `Option::unwrap()` on a `None` value', $DIR/const-unwrap.rs:6:38
error: aborting due to 1 previous error error[E0080]: evaluation of constant value failed
--> $DIR/const-unwrap.rs:10:18
|
LL | const BAZ: i32 = Option::<i32>::None.expect("absolutely not!");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'absolutely not!', $DIR/const-unwrap.rs:10:38
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0080`. For more information about this error, try `rustc --explain E0080`.