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

next_power_of_two should panic on overflow #18604

Closed
Gankra opened this issue Nov 4, 2014 · 10 comments
Closed

next_power_of_two should panic on overflow #18604

Gankra opened this issue Nov 4, 2014 · 10 comments
Labels
C-bug Category: This is a bug. P-medium Medium priority T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Gankra
Copy link
Contributor

Gankra commented Nov 4, 2014

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.

@huonw huonw added the A-libs label Nov 4, 2014
@darnuria
Copy link
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? :)

@Gankra
Copy link
Contributor Author

Gankra commented Nov 17, 2014

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?).

@aturon
Copy link
Member

aturon commented Nov 20, 2014

cc @nikomatsakis

bors added a commit that referenced this issue Dec 20, 2014
The `is_power_of_two()` method of the `UnsignedInt` trait currently returns `true` for `self == 0`. Zero is not a power of two, assuming an integral exponent `k >= 0`. I've therefore moved this functionality to the new method `is_power_of_two_or_zero()` and reformed `is_power_of_two()` to return false for `self == 0`.

To illustrate the usefulness of the existence of both functions, consider `HashMap`. Its capacity must be zero or a power of two; conversely, it also requires a (non-zero) power of two for key and val alignment.

Also, added a small amount of documentation regarding #18604.
@theemathas
Copy link
Contributor

cc #22020

@steveklabnik
Copy link
Member

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.

@brson brson added A-docs T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jun 9, 2016
@brson
Copy link
Contributor

brson commented Jun 9, 2016

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?

@steveklabnik steveklabnik added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Mar 10, 2017
@irfanhudda
Copy link
Contributor

I'll take a shot at this.

irfanhudda added a commit to irfanhudda/rust that referenced this issue Apr 18, 2017
Clarify overflow behavior of `next_power_of_two`.

Related Issue: rust-lang#18604
bors added a commit that referenced this issue Jun 7, 2017
Improve documentation of next_power_of_two

Clarify overflow behavior of `next_power_of_two`.

Related Issue: #18604
bors added a commit that referenced this issue Jun 8, 2017
Improve documentation of next_power_of_two

Clarify overflow behavior of `next_power_of_two`.

Related Issue: #18604
@Mark-Simulacrum
Copy link
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?

@alexcrichton alexcrichton added P-medium Medium priority and removed I-nominated labels Jun 20, 2017
@alexcrichton
Copy link
Member

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

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@Mark-Simulacrum Mark-Simulacrum changed the title next_power_of_two has unspecified overflow behaviour next_power_of_two should panic on overflow Jul 22, 2017
@Mark-Simulacrum Mark-Simulacrum removed the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Jul 22, 2017
scottmcm added a commit to scottmcm/rust that referenced this issue Nov 4, 2017
bors added a commit that referenced this issue Nov 5, 2017
Fix #18604: next_power_of_two should panic on overflow

Fixes #18604

Is it possible to write a test for this?  My experiments showed `x.py test` running in release mode, so my attempt at a `#[should_panic]` didn't work.
@scottmcm
Copy link
Member

scottmcm commented Nov 6, 2017

lnicola pushed a commit to lnicola/rust that referenced this issue Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. P-medium Medium priority T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests