Skip to content

Tracking Issue for more const int functions #53718

Closed

Description

@TimDiekmann

(none exhaustive) list of libcore::num, which should be a const fn:

  1. Requires conditionals. Tracking Issue: Tracking issue for RFC 2342, "Allow if and match in constants" #49146 (implemented on nightly)
  2. Requires loops. Tracking Issue: Tracking issue for RFC 2344, "Allow loop in constant evaluation" #52000 (implemented on nightly)

This issue has been assigned to @9999years via this comment.

Activity

  1. added
    T-libs-api[DEPRECATED; DO NOT USE]
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    T-langRelevant to the language team
    on Aug 28, 2018
  2. TheDarkula commented on Aug 30, 2018

    @TheDarkula

    transmute() just got merged. You can start on your last block now :)

  3. TimDiekmann commented on Aug 31, 2018

    @TimDiekmann
    ContributorAuthor

    @TheDarkula Thank you, I'll start implementing them as soon as #53699 is merged.

  4. TimDiekmann commented on Aug 31, 2018

    @TimDiekmann
    ContributorAuthor

    Added rest of const_int_conversion to #53697

  5. TimDiekmann commented on Sep 1, 2018

    @TimDiekmann
    ContributorAuthor

    I'm currently rebasing #53697 and testing. Then the first bunch should be ready to merge.

  6. added a commit that references this issue on Sep 3, 2018
  7. mjbshaw commented on Dec 31, 2018

    @mjbshaw
    Contributor

    Adding a comment here so I don't have to hunt for the following related issue: #51267 is the tracking issue for feature = const_int_ops, which tracks:

    • count_zeros -- stabilized
    • count_ones -- stabilized
    • leading_zeros -- stabilized
    • trailing_zeros -- stabilized
  8. Lokathor commented on Jan 13, 2019

    @Lokathor
    Contributor

    wrapping_neg isn't on the list here it seems (though overflowing_neg is)

  9. ecstatic-morse commented on Jun 8, 2019

    @ecstatic-morse
    Contributor

    It's possible to make a great number of these const without conditional statements being legal in const fn by using indexing (e.g. [x, -x][(x < 0) as usize]). However, it's a bad idea because the technique results in much less efficient generated code. You can check this by viewing ASM in the previous link. I'm posting this to avoid duplication of effort in case someone else has the same idea.

  10. mark-i-m commented on Jun 8, 2019

    @mark-i-m
    Contributor

    In fact, we are already doing that to make Vec::new() a const fn 😛

    pub const fn new_in(a: A) -> Self {
    // !0 is usize::MAX. This branch should be stripped at compile time.
    // FIXME(mark-i-m): use this line when `if`s are allowed in `const`
    //let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
    // Unique::empty() doubles as "unallocated" and "zero-sized allocation"
    RawVec {
    ptr: Unique::empty(),
    // FIXME(mark-i-m): use `cap` when ifs are allowed in const
    cap: [0, !0][(mem::size_of::<T>() == 0) as usize],
    a,
    }
    }

  11. ecstatic-morse commented on Jun 8, 2019

    @ecstatic-morse
    Contributor

    @mark-i-m That's probably where I first saw this technique!

    My point was that making a similar modification would likely slow down these arithmetic operations considerably. The use in Vec::new can be eliminated via const-propagation post-monomorphization, but that optimization doesn't apply here.

  12. 38 remaining items

  13. added 2 commits that reference this issue on Feb 20, 2020
  14. tspiteri commented on Feb 21, 2020

    @tspiteri
    Contributor

    I think that the {to,from}_{be,le,ne}_bytes methods should be made const in stable. While it is true that currently the implementation depends on e.g. transmute, that is a hidden implementation detail, and they could already be implemented as const stably with e.g. b[3] << 24 | b[2] << 16 | b[1] << 8 | b[0]. Of course, I'm not suggesting that the implementation itself should be changed, just that it should not stop them from being marked const in stable.

  15. jhpratt commented on May 3, 2020

    @jhpratt
    Member

    Would a PR making .rem_euclid a const fn on stable be accepted? It could be trivially implemented as ((x % y) + y) % y. This reasoning has been used for other methods in the past.

  16. tspiteri commented on May 3, 2020

    @tspiteri
    Contributor

    @jhpratt

    It doesn't give the same results for negative y. 3.rem_euclid(-2) == 1, but ((3 % (-2)) + (-2)) % (-2) == -1. This could be solved by using y.abs().

    But it would overflow for example when x == i32::MAX - 1 && y == i32::MAX.

  17. myrrlyn commented on May 7, 2020

    @myrrlyn

    Can we stabilize the const_int_conversion gate, or is there a standing issue preventing stabilization? As has been mentioned upthread, these functions have equivalent const-able expressions, but as they are still unstable, these must be copied into each crate that needs them. This copying is bug-prone, so stabilization would reduce duplication and the possibility of error production.

  18. tspiteri commented on May 7, 2020

    @tspiteri
    Contributor

    @myrrlyn It's already stabilized in beta.

  19. added
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    I-libs-radarLibs issues that are tracked on the team's radar.
    on Jul 29, 2020
  20. Razican commented on Sep 10, 2020

    @Razican
    Contributor

    Hello, as far as I can tell, both [1] and [2] are now stable, right? so there is no extra blocking issue to implement/stabilize those, right? Is there something that can be done to get them stabilized?

  21. tspiteri commented on Sep 10, 2020

    @tspiteri
    Contributor

    @Razican Most of [1] were marked stable in #73858, so they will be stabilized in 1.47. The PR did not include division and remainder because they can panic, so maybe they need another footnote.

    To stabilize the rest that can be stabilized (the pow functions I think), someone needs to write a PR, which will then need to be approved, pass through FCP, and get merged.

  22. jhpratt commented on Jan 13, 2021

    @jhpratt
    Member

    #80962 is up to stabilize all remaining methods mentioned at the top of this issue. The diagnostic on nightly is functionally the same as already-stable equivalents (5 % 0 vs 5.rem_euclid(0), for example).

  23. added a commit that references this issue on Feb 8, 2021
  24. added
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    on Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.T-langRelevant to the language teamT-libs-api[DEPRECATED; DO NOT USE]

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions