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
{{ message }}
Repository navigation
Integer overflow false positive with (0u8..).take(256) #25708
The issue here is that while the yielded values do not depend on a integer overflow occurring, it does so as part for preparing the state for the next, invalid iteration.
It is therefore not possible to use them for counting through the whole range of an integer value in debug mode.
A possible solution would be to use conditional compilation to add a overflow flag to the struct in debug mode, have it use wrapping operation and panic on the next next() call.
Return the sequence 0, 1, 2, ..., 255, 255, 255, 255, 255, ...
This would make the (0u8..).take(256) work, but gives weird behavior after the first 255.
Make a breaking change to extend the struct, either only in debug mode or always.
If (1) or (2) is wanted, I can implement it, but otherwise this issue can be closed with the label rust-2-breakage-wishlist, because the behavior of 0, 1, 2, ..., 255, panic is impossible to implement for the debug mode.
(This behavior is impossible, because you have 256 states for the struct, but want to yield 257 different behaviors: 0, ..., 255, panic.)
Activity
bluss commentedon May 22, 2015
See also #25696.
tbu- commentedon May 6, 2016
There are three ways to go about this:
This would make the (0u8..).take(256) work, but gives weird behavior after the first 255.
If (1) or (2) is wanted, I can implement it, but otherwise this issue can be closed with the label
rust-2-breakage-wishlist, because the behavior of 0, 1, 2, ..., 255, panic is impossible to implement for the debug mode.(This behavior is impossible, because you have 256 states for the struct, but want to yield 257 different behaviors: 0, ..., 255, panic.)
steveklabnik commentedon Oct 31, 2018
Triage: still panics
CAD97 commentedon May 20, 2020
For anyone falling on this issue: the correct way to iterate over all values is to use an inclusive range,
0..=u8::MAX.Rollup merge of rust-lang#72368 - CAD97:rangeto, r=dtolnay
Rollup merge of rust-lang#72368 - CAD97:rangeto, r=dtolnay