Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize const_option #131120

Merged
merged 1 commit into from
Oct 13, 2024
Merged

Stabilize const_option #131120

merged 1 commit into from
Oct 13, 2024

Conversation

tgross35
Copy link
Contributor

@tgross35 tgross35 commented Oct 1, 2024

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: #67441

Cc @rust-lang/wg-const-eval

@rustbot
Copy link
Collaborator

rustbot commented Oct 1, 2024

r? @scottmcm

rustbot has assigned @scottmcm.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 1, 2024
@rustbot
Copy link
Collaborator

rustbot commented Oct 1, 2024

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

The Miri subtree was changed

cc @rust-lang/miri

@tgross35 tgross35 added the S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. label Oct 1, 2024
@tgross35
Copy link
Contributor Author

tgross35 commented Oct 1, 2024

FCP started today so should complete on the 11th. The beta branch is the same day, so if this gets approved before then, this API will make 1.83.

take and replace need const_replace, which is proposed to be stabilized in #130954. However, there is a chance that const_replace takes longer to stabilize #130954 (comment). If it winds up getting delayed to the following version, we can move take and replace under a new const_option_replace, to be stabilized whenever const_replace merges. (I left todos in the diff related to this).

expect calls panic_display. This seems to be okay because it is only formatting for a single string, similar to const { panic!("foo") }.

@RalfJung
Copy link
Member

RalfJung commented Oct 2, 2024

The following functions make use of the unstable
const_precise_live_drops feature:

That's fine from my side.

@@ -1712,7 +1716,9 @@ impl<T> Option<T> {
/// ```
#[inline]
#[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_allow_const_fn_unstable(const_replace)] // todo: drop if we don't get stable const_replace
Copy link
Member

@RalfJung RalfJung Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still in FCP at #130954, so we have to wait until tomorrow before we can land this PR as-is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That just merged so I was able to update & drop those gates.

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>
@tgross35 tgross35 removed the S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. label Oct 12, 2024
@tgross35
Copy link
Contributor Author

FCP complete at #67441 (comment). const_replace also merged so nothing here is blocked on that anymore.

@RalfJung
Copy link
Member

RalfJung commented Oct 12, 2024 via email

@bors
Copy link
Contributor

bors commented Oct 12, 2024

📌 Commit 19f6c17 has been approved by RalfJung

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 12, 2024
tgross35 added a commit to tgross35/rust that referenced this pull request Oct 12, 2024
…=RalfJung

Stabilize `const_option`

This makes the following API stable in const contexts:

```rust
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>
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 13, 2024
Rollup of 7 pull requests

Successful merges:

 - rust-lang#131120 (Stabilize `const_option`)
 - rust-lang#131207 (ci: aarch64-gnu-debug job)
 - rust-lang#131334 (Enable sanitizers for loongarch64-unknown-*)
 - rust-lang#131358 (force "HEAD" for non-CI and `git_upstream_merge_base` for CI environment)
 - rust-lang#131418 (Use throw intrinsic from stdarch in wasm libunwind)
 - rust-lang#131579 (Remap path prefix in the panic message of `tests/ui/meta/revision-bad.rs`)
 - rust-lang#131591 (add latest crash tests)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 13, 2024
Rollup of 7 pull requests

Successful merges:

 - rust-lang#131120 (Stabilize `const_option`)
 - rust-lang#131334 (Enable sanitizers for loongarch64-unknown-*)
 - rust-lang#131358 (force "HEAD" for non-CI and `git_upstream_merge_base` for CI environment)
 - rust-lang#131418 (Use throw intrinsic from stdarch in wasm libunwind)
 - rust-lang#131579 (Remap path prefix in the panic message of `tests/ui/meta/revision-bad.rs`)
 - rust-lang#131591 (add latest crash tests)
 - rust-lang#131626 (remove a couple of redundant String to String conversion)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit c8b2f7e into rust-lang:master Oct 13, 2024
6 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Oct 13, 2024
@tgross35 tgross35 deleted the stabilize-const_option branch October 13, 2024 06:27
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 13, 2024
Rollup merge of rust-lang#131120 - tgross35:stabilize-const_option, r=RalfJung

Stabilize `const_option`

This makes the following API stable in const contexts:

```rust
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>
@tgross35
Copy link
Contributor Author

tgross35 commented Oct 13, 2024

It looks like this may have made the version cutoff after all 🎉 lucky us that a LLVM bump delayed #131560

flip1995 pushed a commit to flip1995/rust that referenced this pull request Oct 18, 2024
…=RalfJung

Stabilize `const_option`

This makes the following API stable in const contexts:

```rust
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tracking issue for const Option functions
5 participants