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

Tracking Issue for const unreachable_unchecked #53188

Closed
gnzlbg opened this issue Aug 8, 2018 · 9 comments · Fixed by #74459 or #89509
Closed

Tracking Issue for const unreachable_unchecked #53188

gnzlbg opened this issue Aug 8, 2018 · 9 comments · Fixed by #74459 or #89509
Labels
A-const-eval Area: constant evaluation (mir interpretation) A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Aug 8, 2018

Currently one cannot call unreachable_unchecked in constant functions, so one cannot add hints of the form:

const fn foo(x: i32) -> i32 {
    if x < 0 { unreachable_unchecked() } else { x * 2 } 
}

These types of hints can heavily influence code generation, which can substantially change the performance of const fn when they are invoked at run-time.

It would be a shame to have to write and call foo_compile_time and foo_run_time because the code generated for foo_compile_time is sub-par.

This issue has been assigned to @canova via this comment.

@oli-obk
Copy link
Contributor

oli-obk commented Aug 8, 2018

One cannot do any sort of conditional control flow, so unreachable_unchecked would currently be less than useless. Any test you can write would be undefined behaviour

@oli-obk oli-obk added A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-const-eval Area: constant evaluation (mir interpretation) labels Aug 8, 2018
@gnzlbg
Copy link
Contributor Author

gnzlbg commented Aug 8, 2018

@oli-obk the RFC for if/match in const was merged and is pending an implementation right? When that's implemented, will unreachable_unchecked still be useless?

@oli-obk
Copy link
Contributor

oli-obk commented Aug 8, 2018

No it makes sense after we have conditional control flow.

@oli-obk oli-obk added the S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. label Aug 8, 2018
@q2p
Copy link

q2p commented Mar 21, 2020

As I understand if / match in const functions is now implemented.
Can unreachable_unchecked be made into a const fn now?

@oli-obk
Copy link
Contributor

oli-obk commented Mar 22, 2020

yes. Basically you just need to add the rustc_const_unstable attribute to the intrinsic and wrapper function. This is documented at https://rustc-dev-guide.rust-lang.org/stability.html?highlight=rustc_const_un#rustc_const_unstable Let me know if anything is unclear (in which case we should update the guide)

@jonas-schievink jonas-schievink added C-feature-accepted Category: A feature request that has been accepted pending implementation. T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. labels Mar 23, 2020
@canova
Copy link
Contributor

canova commented Jul 17, 2020

Hi, It looks like previous PR is not being worked on currently. I'm working on this and will create a PR soon.
@rustbot claim

@josephlr
Copy link
Contributor

As mentioned in #76209, this issue should be reopened (to track stablization)

@tesuji
Copy link
Contributor

tesuji commented Sep 20, 2020

cc @RalfJung ^

@oli-obk oli-obk reopened this Sep 20, 2020
@jhpratt
Copy link
Member

jhpratt commented May 10, 2021

After a short discussion on Zulip, it was decided that it's best to wait for const_panic to stabilize to avoid a potential footgun wherein users use this instead of panic!(), leading to UB at runtime.

I'm subscribed to the const_panic issue and intend to file a PR to stabilize this once the aforementioned happens.

@jonas-schievink jonas-schievink added C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. and removed C-feature-accepted Category: A feature request that has been accepted pending implementation. labels Jun 27, 2021
@jonas-schievink jonas-schievink changed the title Consider making unreachable_unchecked const Tracking Issue for const unreachable_unchecked Jun 27, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 15, 2021
…_unchecked, r=oli-obk

Stabilize `unreachable_unchecked` as `const fn`

Closes rust-lang#53188

This PR stabilizes `core::hint::unreachable_unchecked` as `const fn`. MIRI is able to detect when this method is called. Stabilization was delayed until `const_panic` was stabilized so as to avoid users calling this method in its place (thus resulting in runtime UB). With rust-lang#89508, that is no longer an issue.

`@rustbot` label +A-const-eval +A-const-fn +T-lang +S-blocked

(not sure why it's T-lang, but that's what the tracking issue is)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 15, 2021
…_unchecked, r=oli-obk

Stabilize `unreachable_unchecked` as `const fn`

Closes rust-lang#53188

This PR stabilizes `core::hint::unreachable_unchecked` as `const fn`. MIRI is able to detect when this method is called. Stabilization was delayed until `const_panic` was stabilized so as to avoid users calling this method in its place (thus resulting in runtime UB). With rust-lang#89508, that is no longer an issue.

`@rustbot` label +A-const-eval +A-const-fn +T-lang +S-blocked

(not sure why it's T-lang, but that's what the tracking issue is)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 15, 2021
…_unchecked, r=oli-obk

Stabilize `unreachable_unchecked` as `const fn`

Closes rust-lang#53188

This PR stabilizes `core::hint::unreachable_unchecked` as `const fn`. MIRI is able to detect when this method is called. Stabilization was delayed until `const_panic` was stabilized so as to avoid users calling this method in its place (thus resulting in runtime UB). With rust-lang#89508, that is no longer an issue.

``@rustbot`` label +A-const-eval +A-const-fn +T-lang +S-blocked

(not sure why it's T-lang, but that's what the tracking issue is)
jackh726 added a commit to jackh726/rust that referenced this issue Oct 16, 2021
…_unchecked, r=oli-obk

Stabilize `unreachable_unchecked` as `const fn`

Closes rust-lang#53188

This PR stabilizes `core::hint::unreachable_unchecked` as `const fn`. MIRI is able to detect when this method is called. Stabilization was delayed until `const_panic` was stabilized so as to avoid users calling this method in its place (thus resulting in runtime UB). With rust-lang#89508, that is no longer an issue.

```@rustbot``` label +A-const-eval +A-const-fn +T-lang +S-blocked

(not sure why it's T-lang, but that's what the tracking issue is)
@bors bors closed this as completed in 9ae0804 Oct 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: constant evaluation (mir interpretation) A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
9 participants