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

Fix Range#size for unsigned edge cases #14978

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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
@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
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.

3 participants