Skip to content

Tracking Issue for const unreachable_unchecked #53188

Description

@gnzlbg

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.

Activity

  1. oli-obk commented on Aug 8, 2018

    @oli-obk
    Contributor

    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

  2. added
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    on Aug 8, 2018
  3. gnzlbg commented on Aug 8, 2018

    @gnzlbg
    ContributorAuthor

    @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?

  4. oli-obk commented on Aug 8, 2018

    @oli-obk
    Contributor

    No it makes sense after we have conditional control flow.

  5. added
    S-blockedStatus: Blocked on something else such as an RFC or other implementation work.
    on Aug 8, 2018
  6. q2p commented on Mar 21, 2020

    @q2p

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

  7. oli-obk commented on Mar 22, 2020

    @oli-obk
    Contributor

    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)

  8. added
    C-feature-acceptedCategory: A feature request that has been accepted pending implementation.
    T-langRelevant to the language team
    and removed
    S-blockedStatus: Blocked on something else such as an RFC or other implementation work.
    on Mar 23, 2020
  9. canova commented on Jul 17, 2020

    @canova
    Contributor

    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

  10. self-assigned this
    on Jul 17, 2020
  11. added a commit that references this issue on Jul 18, 2020
    2fad396
  12. josephlr commented on Sep 12, 2020

    @josephlr
    Contributor

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

  13. tesuji commented on Sep 20, 2020

    @tesuji
    Contributor
  14. jhpratt commented on May 10, 2021

    @jhpratt
    Member

    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.

  15. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    and removed
    C-feature-acceptedCategory: A feature request that has been accepted pending implementation.
    on Jun 27, 2021
  16. changed the title [-]Consider making unreachable_unchecked const[/-] [+]Tracking Issue for const `unreachable_unchecked`[/+] on Jun 27, 2021
  17. added a commit that references this issue on Oct 15, 2021
    d9a9d6e
  18. added 2 commits that reference this issue on Oct 15, 2021
    16bdcdb
    38d2699
  19. added a commit that references this issue on Oct 16, 2021
    3669d56
  20. added a commit that references this issue on Oct 16, 2021
    9ae0804
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions