Skip to content

next_power_of_two should panic on overflow #18604

Closed

Description

@Gankra

If you feed in a number such that the next_power_of_two would overflow, it spits out 0. I'm fine with that honestly, since it's easy to detect, but maybe that isn't what we want here. Maybe it should return a Result/Option/panic/saturate?

Should be specified so that the behaviour can be relied upon, or explicitly documented as unspecified.

Activity

  1. darnuria commented on Nov 17, 2014

    @darnuria
    Contributor

    @gankro Actually there is a std::num::UnsignedInt::checked_next_power_of_two a good contribution would be to add a documentation precision into std::num::UnsignedInt::next_power_of_two on how is defined behavior on overflow?

    It could be tagged as a documentation bug, and an easy one? :)

  2. Gankra commented on Nov 17, 2014

    @Gankra
    ContributorAuthor

    Yes, this can be considered a documentation bug, assuming we sit down and settle what we want the semantics to be. I've got no strong opinion, so I'll defer to @bjz and @aturon (unless you have a thought on what it should be?).

  3. aturon commented on Nov 20, 2014

    @aturon
    Contributor
  4. theemathas commented on Mar 18, 2015

    @theemathas
    Contributor
  5. steveklabnik commented on Jun 6, 2016

    @steveklabnik
    Contributor

    Triage: docs wise, this says " Unspecified behavior on overflow.". @theemathas has linked to the overflow RFC, and this discussion was well before that, so there's no reason for overflow behavior to be particularly exceptional here, right? It follows the same rules as everything else, unless @rust-lang/libs wants to make it special in some way.

  6. added
    T-libs-api
    on Jun 9, 2016
  7. brson commented on Jun 9, 2016

    @brson
    Contributor

    Here's the implementation:

            pub fn next_power_of_two(self) -> Self {
                let bits = size_of::<Self>() * 8;
                let one: Self = Self::one();
                one << ((bits - self.wrapping_sub(one).leading_zeros() as usize) % bits)
            }
    

    I think @gankro's original comments were from before the overflow RFC so this no longer results in 0 on overflow but behaves per the RFC. And as stated there is now a checked_ variant of this function. I think we should just update the documentation to state that it's overflow behavior corresponds to whatever setting controls overflow and move on. Is there existing precedent for methods in std allowing overflow internally?

  8. added
    A-docs
    on Mar 10, 2017
  9. irfanhudda commented on Mar 21, 2017

    @irfanhudda
    Contributor

    I'll take a shot at this.

  10. 3 remaining items

  11. Mark-Simulacrum commented on Jun 15, 2017

    @Mark-Simulacrum
    Member

    So, I was going to close this, but actually, it looks like the behavior the documentation currently specifies isn't the behavior the code produces. Namely, based on #40706 (comment) I would expect https://is.gd/wRatpD to panic in debug mode, but it doesn't. Nominating for libs team discussion: Can we confirm that we want to panic (I think we should) in debug?

  12. added
    P-medium
    and removed on Jun 20, 2017
  13. alexcrichton commented on Jun 20, 2017

    @alexcrichton
    Member

    Yep this is just a bug, the intention is that it panics in debug mode on overflow.

  14. added
    C-bug
    on Jul 22, 2017
  15. changed the title [-]next_power_of_two has unspecified overflow behaviour[/-] [+]next_power_of_two should panic on overflow[/+] on Jul 22, 2017
  16. removed
    A-docs
    on Jul 22, 2017
  17. added a commit that references this issue on Nov 4, 2017
    15ea3d8
  18. added a commit that references this issue on Nov 5, 2017
    4efcc66
  19. added a commit that references this issue on Dec 11, 2024
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

    C-bugP-mediumT-libs-api

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions