From 47353328bfd091eeeeaa3e56756e5d4c509b04c5 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 25 Aug 2026 15:45:30 +0200 Subject: [PATCH] Integer#& / #| / #^: run the coerce protocol on a non-Integer operand MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- internal/vm/builtins.go | 32 +++++++++++--- .../vm/integer_bit_coerce_internal_test.go | 42 +++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 internal/vm/integer_bit_coerce_internal_test.go diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index e98f3c44..58c5e6c4 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -4496,14 +4496,14 @@ func (vm *VM) bootstrap() { vm.cInteger.define(">>", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { return vm.integerShift(bigVal(self), args[0], true) }) - vm.cInteger.define("&", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return object.NormInt(new(big.Int).And(bigVal(self), bigArg(args[0]))) + vm.cInteger.define("&", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + return vm.integerBitOp(bigVal(self), args[0], "&", (*big.Int).And) }) - vm.cInteger.define("|", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return object.NormInt(new(big.Int).Or(bigVal(self), bigArg(args[0]))) + vm.cInteger.define("|", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + return vm.integerBitOp(bigVal(self), args[0], "|", (*big.Int).Or) }) - vm.cInteger.define("^", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return object.NormInt(new(big.Int).Xor(bigVal(self), bigArg(args[0]))) + vm.cInteger.define("^", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + return vm.integerBitOp(bigVal(self), args[0], "^", (*big.Int).Xor) }) vm.cInteger.define("~", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return object.NormInt(new(big.Int).Not(bigVal(self))) @@ -7964,6 +7964,26 @@ func bigArg(v object.Value) *big.Int { return nil } +// integerBitOp applies a bitwise operator (&, |, ^) to an Integer receiver. An +// Integer or Bignum right operand is combined directly; any other operand except +// a Float runs Ruby's coerce protocol — rhs.coerce(self) yields a [x, y] pair and +// the operator is re-dispatched on it — while a Float (and anything without +// #coerce) raises a TypeError, matching MRI, which does not coerce a Float here. +func (vm *VM) integerBitOp(a *big.Int, arg object.Value, name string, op func(z, x, y *big.Int) *big.Int) object.Value { + if b, ok := object.BigOf(arg); ok { + return object.NormInt(op(new(big.Int), a, b)) + } + if _, isFloat := arg.(object.Float); !isFloat && vm.respondsToDynamic(arg, "coerce") { + pair := vm.send(arg, "coerce", []object.Value{object.NormInt(a)}, nil) + if arr, ok := pair.(*object.Array); ok && len(arr.Elems) == 2 { + return vm.send(arr.Elems[0], name, []object.Value{arr.Elems[1]}, nil) + } + raise("TypeError", "coerce must return [x, y]") + } + raise("TypeError", "%s can't be coerced into Integer", vm.classOf(arg).name) + return nil +} + // shiftInt shifts a left by n bits (right by -n when n is negative), promoting // to a Bignum as needed and demoting a result that fits back into an Integer. func shiftInt(a *big.Int, n int64) object.Value { diff --git a/internal/vm/integer_bit_coerce_internal_test.go b/internal/vm/integer_bit_coerce_internal_test.go new file mode 100644 index 00000000..932ddee7 --- /dev/null +++ b/internal/vm/integer_bit_coerce_internal_test.go @@ -0,0 +1,42 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestIntegerBitwiseCoerce covers the coerce protocol of Integer#&, #| and #^: +// an Integer or Bignum operand is combined directly, any other operand that +// defines #coerce is coerced (rhs.coerce(self) -> [x, y], then the operator is +// re-dispatched on the pair), and a Float — or an operand without #coerce — +// raises a TypeError. Verified against ruby 4.0.6. +func TestIntegerBitwiseCoerce(t *testing.T) { + const c = `class C; def coerce(o); [o, 5]; end; end; ` + cases := []struct{ src, want string }{ + // Plain integer operands still work, promoting to Bignum as needed. + {`p 6 & 3`, `2`}, + {`p 6 | 3`, `7`}, + {`p 6 ^ 3`, `5`}, + {`p (2**64) | 1`, `18446744073709551617`}, + {`p (2**70) & (2**70 - 1)`, `0`}, + // A non-Integer operand with #coerce is coerced, then re-dispatched. + {c + `p 6 | C.new`, `7`}, + {c + `p 6 & C.new`, `4`}, + {c + `p 3 ^ C.new`, `6`}, + // A Float never runs the coerce protocol here; it is a TypeError. + {`begin; 3 | 3.4; rescue TypeError => e; p e.class; end`, `TypeError`}, + {`begin; 3 & 3.4; rescue TypeError => e; p e.class; end`, `TypeError`}, + // An operand without #coerce is a TypeError naming its class. + {`begin; 3 | Object.new; rescue TypeError => e; p e.message; end`, `"Object can't be coerced into Integer"`}, + {`class D; end; begin; 3 ^ D.new; rescue TypeError => e; p e.message; end`, `"D can't be coerced into Integer"`}, + {`begin; 3 & "x"; rescue TypeError => e; p e.class; end`, `TypeError`}, + // A #coerce that does not return a two-element array is a TypeError. + {`class E; def coerce(o); [1]; end; end; begin; 3 | E.new; rescue TypeError => e; p e.message; end`, `"coerce must return [x, y]"`}, + } + for _, tc := range cases { + if got := eval(t, tc.src); got != tc.want+"\n" { + t.Errorf("src=%q got=%q want=%q", tc.src, got, tc.want+"\n") + } + } +}