rust/tests/ui/consts/const-unwrap.rs
Trevor Gross 19f6c17df4 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>
2024-10-12 17:07:13 -04:00

18 lines
467 B
Rust

//@ check-fail
// Verify that panicking `const_option` methods do the correct thing
const FOO: i32 = Some(42i32).unwrap();
const BAR: i32 = Option::<i32>::None.unwrap();
//~^ 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() {
println!("{}", FOO);
println!("{}", BAR);
}