Repository navigation
Integer#<< / #>>: handle oversized and #to_int-coercible shift amounts - #453
Merged
Merged
Conversation
Integer#<< and #>> took the shift amount through intArg, which rejected a Bignum
("no implicit conversion of <bignum> into Integer") and never coerced a
non-Integer via #to_int. A shift by a huge amount therefore raised a spurious
TypeError instead of MRI's degenerate result.
The amount is now coerced with #to_int (a missing #to_int, or one returning a
non-Integer, raises the matching TypeError), and an amount that does not fit a
machine int is handled the way MRI does: a huge right shift collapses to 0 — or
-1 for a negative receiver — a huge left shift of zero stays 0, and any other
huge left shift raises RangeError rather than trying to allocate an
address-space-sized integer. The common machine-int amount keeps its fast path
straight to shiftInt, so the hot path is unchanged.
Verified against ruby 4.0.6. Full package coverage stays at 100.0%. Zero-
regression rubyspec sweep; core/integer/left_shift_spec 24 -> 29 and
core/integer/right_shift_spec 24 -> 30 (rbgo now matches MRI's own result on
these files under the same harness).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Integer#<<and#>>took the shift amount throughintArg, which rejected aBignum (
no implicit conversion of <bignum> into Integer) and never coerced anon-Integer via
#to_int. A shift by a huge amount therefore raised a spuriousTypeErrorinstead of MRI's degenerate result.The amount is now coerced with
#to_int(a missing#to_int, or one returning anon-Integer, raises the matching
TypeError), and an amount that does not fit amachine int is handled the way MRI does:
0— or-1for a negative receiver;0;RangeErrorrather than trying to allocate anaddress-space-sized integer.
The common machine-int amount keeps its fast path straight to
shiftInt, so thehot path is unchanged.
Verified against ruby 4.0.6. Full package coverage stays at 100.0%.
Zero-regression rubyspec sweep;
core/integer/left_shift_spec24 → 29 andcore/integer/right_shift_spec24 → 30 (rbgo now matches MRI's own result onthese files under the same harness; the residual failures are a
spec_helperbignum_valuediscrepancy that affects MRI identically, tracked separately).🤖 Generated with Claude Code