Skip to content

Fix Range#size for unsigned edge cases#14978

Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom
straight-shoota:fix/range-size-unsigned
Feb 28, 2025
Merged

Fix Range#size for unsigned edge cases#14978
straight-shoota merged 2 commits intocrystal-lang:masterfrom
straight-shoota:fix/range-size-unsigned

Conversation

@straight-shoota
Copy link
Member

Resolves part of #13648

@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:collection labels Sep 5, 2024
@straight-shoota straight-shoota self-assigned this Sep 5, 2024
ysbaddaden
ysbaddaden previously approved these changes Sep 6, 2024
@straight-shoota straight-shoota added this to the 1.14.0 milestone Sep 6, 2024
oprypin
oprypin previously requested changes Sep 6, 2024
n < 0 ? 0 : n.to_i32
return 0 if e < b

diff = (e &- b).to_i32.abs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't be right.. how can it be safe to just not check for overflows 🤔
We should double-check it, I'll probably do so today

Copy link
Member Author

@straight-shoota straight-shoota Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. e < b only excludes the obvious case. 🤦
We can still end up with a difference that doesn't fit into E (e.g. (Int8::MIN..1i8).size). But it would fit into Int32.

I suppose we could use non-wrapping arithmetics and cast e to Int32 if it's a smaller type. That should probably cover every constellation where the result fits into Int32.

Copy link
Member Author

@straight-shoota straight-shoota Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are so many edge cases to take into account 🤯
I'm wondering if we should collect a set of
for testing Range methods with optimized implementations for Int (i.e. the ones mentioned in #13648: size, sum, step.sum, map; maybe also sample).

@straight-shoota straight-shoota removed this from the 1.14.0 milestone Sep 7, 2024
@straight-shoota straight-shoota dismissed stale reviews from oprypin and ysbaddaden December 4, 2024 21:33

Fixed

@straight-shoota straight-shoota added this to the 1.16.0 milestone Feb 27, 2025
@straight-shoota straight-shoota merged commit b847480 into crystal-lang:master Feb 28, 2025
@straight-shoota straight-shoota deleted the fix/range-size-unsigned branch February 28, 2025 10:49
Blacksmoke16 pushed a commit to Blacksmoke16/crystal that referenced this pull request Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:collection

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants