-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Comments
@gankro Actually there is a It could be tagged as a documentation bug, and an easy one? :) |
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.
cc #22020 |
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. |
Here's the implementation:
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 |
I'll take a shot at this. |
Clarify overflow behavior of `next_power_of_two`. Related Issue: rust-lang#18604
Improve documentation of next_power_of_two Clarify overflow behavior of `next_power_of_two`. Related Issue: #18604
Improve documentation of next_power_of_two Clarify overflow behavior of `next_power_of_two`. Related Issue: #18604
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? |
Yep this is just a bug, the intention is that it panics in debug mode on overflow. |
Confirmed in today's nightly: https://play.rust-lang.org/?gist=3bd6bdb2ff2c5ea633f9ae3091faf5d7&version=nightly&mode=debug |
feat: Complete derive helper attributes
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.
The text was updated successfully, but these errors were encountered: