Skip to content

Integer#<< / #>>: handle oversized and #to_int-coercible shift amounts - #453

Merged
tannevaled merged 1 commit into
mainfrom
feat/cluster24
Aug 25, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/cluster24

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

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;
  • 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; the residual failures are a spec_helper
bignum_value discrepancy that affects MRI identically, tracked separately).

🤖 Generated with Claude Code

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>
@tannevaled
tannevaled merged commit 184a091 into main Aug 25, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/cluster24 branch August 25, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant