Skip to content

Integer#& / #| / #^: run the coerce protocol on a non-Integer operand - #455

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

tannevaled merged 1 commit into
mainfrom
feat/cluster25

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

The bitwise operators fed their right operand straight through bigArg, which
raised X can't be coerced into Integer for anything that was not already an
Integer or Bignum. MRI instead runs the numeric coerce protocol: for an
operand that defines #coerce, rhs.coerce(self) yields a [x, y] pair and the
operator is re-dispatched on it, so 6 | obj where obj.coerce(6) == [6, 3]
returns 7.

A Float is the documented exception — MRI does not coerce a Float bitwise operand
— so a Float (and any operand without #coerce) still raises a TypeError, and
a #coerce that does not return a two-element array raises
coerce must return [x, y]. The TypeError now names the operand's real class
via vm.classOf rather than the static classNameOf.

Verified against ruby 4.0.6. Full package coverage stays at 100.0%.
Zero-regression rubyspec sweep; core/integer/{bit_and,bit_or,bit_xor}_spec
each +1.

🤖 Generated with Claude Code

The bitwise operators fed their right operand straight through bigArg, which
raised "X can't be coerced into Integer" for anything that was not already an
Integer or Bignum. MRI instead runs the numeric coerce protocol: for an operand
that defines #coerce, rhs.coerce(self) yields a [x, y] pair and the operator is
re-dispatched on it, so `6 | obj` where obj.coerce(6) == [6, 3] returns 7.

A Float is the documented exception — MRI does not coerce a Float bitwise
operand — so a Float (and any operand without #coerce) still raises a TypeError,
and a #coerce that does not return a two-element array raises
"coerce must return [x, y]". The TypeError now names the operand's real class via
vm.classOf rather than the static classNameOf.

Verified against ruby 4.0.6. Full package coverage stays at 100.0%. Zero-
regression rubyspec sweep; core/integer/{bit_and,bit_or,bit_xor}_spec each +1.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 5bc53b1 into main Aug 25, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/cluster25 branch August 25, 2026 14:21
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