You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
@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? :)
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?).
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.
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?
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?
Activity
darnuria commentedon Nov 17, 2014
@gankro Actually there is a
std::num::UnsignedInt::checked_next_power_of_twoa good contribution would be to add a documentation precision intostd::num::UnsignedInt::next_power_of_twoon how is defined behavior on overflow?It could be tagged as a documentation bug, and an easy one? :)
Gankra commentedon 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 commentedon Nov 20, 2014
cc @nikomatsakis
theemathas commentedon Mar 18, 2015
cc #22020
steveklabnik commentedon Jun 6, 2016
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 commentedon Jun 9, 2016
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
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?irfanhudda commentedon Mar 21, 2017
I'll take a shot at this.
3 remaining items
Mark-Simulacrum commentedon Jun 15, 2017
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 commentedon Jun 20, 2017
Yep this is just a bug, the intention is that it panics in debug mode on overflow.
[-]next_power_of_two has unspecified overflow behaviour[/-][+]next_power_of_two should panic on overflow[/+]Fix rust-lang#18604: next_power_of_two should panic on overflow
Auto merge of #45754 - scottmcm:checked-npot, r=dtolnay
scottmcm commentedon Nov 6, 2017
Confirmed in today's nightly: https://play.rust-lang.org/?gist=3bd6bdb2ff2c5ea633f9ae3091faf5d7&version=nightly&mode=debug
Merge pull request rust-lang#18604 from ChayimFriedman2/complete-helpers