From f065264bf2e57c5d895892d86ca51d409e3c4f1e Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 1 Nov 2026 09:31:06 +0100 Subject: [PATCH 1/2] vm: a splat must not call #to_a on an Array subclass, or past a refusing #respond_to? MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two more of the five defects in #799 that are independent of the version move, both in `splatToArray` and both the same mistake in different clothes: the splat asked THIS VM a question MRI asks of the value's type. ``` language/variables_spec.rb "does not call #to_a on an Array subclass" mock received forbidden to_a "does not call #to_a if #respond_to? returns false" mock received forbidden to_a ``` ## MRI's own path `*v` compiles to the `splatarray` instruction, and its helper is ```c static inline VALUE vm_splat_array(VALUE flag, VALUE ary) { VALUE tmp = rb_check_to_array(ary); if (NIL_P(tmp)) return rb_ary_new3(1, ary); else if (RTEST(flag)) return rb_ary_dup(tmp); else return tmp; } ``` with `rb_check_to_array` being `rb_check_convert_type_with_id(ary, T_ARRAY, "Array", idTo_a)`. Two properties of that one call are the whole of this change: - it returns the value **untouched** when the value is already `T_ARRAY`, and an instance of `Class.new(Array)` is `T_ARRAY`, so `#to_a` is never sent; - it goes through `rb_check_funcall`, which consults an **overridden** `#respond_to?` first and does not call the method when that answers false (vm_eval.c `check_funcall_respond_to`). rbgo's fast path was a Go type assertion on `*object.Array`, which a subclass instance fails — it is an `RObject` whose `builtin` field holds the wrapped array — and the fallback asked the method table directly with `respondsTo` rather than `respondsToConversion`, the helper this repository already has for exactly `check_funcall_respond_to` and already uses for `#to_ary`. The second one has visible damage, not just a stray call: ```ruby class Refuser def respond_to?(name, include_private = false) = false def to_a = [:wrong] end a = *Refuser.new ``` MRI wraps the object, so `a == [refuser]`. rbgo called the method it had just been told did not exist and **took the result**: `a == [:wrong]`. ## ⛔ The neighbour that makes the first fix non-obvious ```ruby it "returns an Array when the splatted object is an Array subclass" do x = (a = *cls.new([1, 2])) x.should.instance_of?(Array) end ``` That example **passes on `main` today, because of the defect**: `#to_a` is wrongly sent and `Array#to_a` happens to answer with a plain copy. Returning the subclass instance itself would therefore have repaired one example by breaking its neighbour. `vm_splat_array` returns `rb_ary_dup(tmp)`, so the fix returns a fresh plain Array built from the elements — which also keeps the splat from aliasing the subclass instance's own storage. ## Measured, with the control `language/variables_spec.rb`, pinned corpus 87b1631, shim at 3.4.1: | binary | result | |---|---| | `main` | `pass=115 fail=4` | | after #856 (the uninitialized-global warning) | `pass=116 fail=3` | | **this branch, on top of main** | **`pass=118 fail=1`** | `fail=1` is `parses a non-ASCII upcased character as a constant identifier`, the last of the five and a parser-level one. A probe of the four shapes, on MRI and on both binaries: | | MRI 2.6.10 | `main` | this branch | |---|---|---|---| | `#to_a` sent for an Array subclass | no | **yes** | no | | result `instance_of?(Array)` | true | true | true | | `#to_a` sent past `respond_to? == false` | no | **yes** | no | | the refused object is wrapped | yes | **no** (`[:wrong]`) | yes | ## Two measurements that corrected what I was about to assert Both are test rows now, in MRI's answer rather than the tidier-sounding one: - a **private** `#to_a` **is** reached, by both engines — `rb_check_funcall` passes `priv=TRUE`, and a method-table lookup finds a private method just the same. I had written the opposite into the test before running it. - a Hash is **converted**, not wrapped: `*{x: 1}` is `[[:x, 1]]`, because `Hash#to_a` exists. ## One divergence left, deliberately not folded in MRI asks an overridden `#respond_to?` with **two** arguments, `(:to_a, true)`; rbgo's `respondsToConversion` sends one, so the receiver's own default fills in `false`: ``` MRI : respond_to?(:to_a, true) rbgo : respond_to?(:to_a, false) ``` Matching it means `vm_respond_to`'s arity rule (pass one argument to a one-argument `#respond_to?`, two otherwise, raise above two), and `respondsToConversion` is on every conversion site — `to_ary`, `to_str`, `to_sym`, `to_hash` — so it is a change with its own blast radius and its own measurement. Filed rather than smuggled in here; no spec in the gate sees it, because the shim's `.with` is not strict about the argument list. Tests: `splat_conversion_test.go` — the four repaired shapes, and eight controls including the ones that prove the conversion still HAPPENS, because a splat that had simply stopped calling `#to_a` would pass every row of the first group. Refs #799 Co-authored-by: Claude Opus 5 --- internal/vm/aot_runtime.go | 38 +++++++- internal/vm/splat_conversion_test.go | 141 +++++++++++++++++++++++++++ 2 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 internal/vm/splat_conversion_test.go diff --git a/internal/vm/aot_runtime.go b/internal/vm/aot_runtime.go index 49ffcb92..f2f683db 100644 --- a/internal/vm/aot_runtime.go +++ b/internal/vm/aot_runtime.go @@ -39,15 +39,43 @@ func (vm *VM) aotSplat(v object.Value) object.Value { return vm.splatToArray(v) } -// splatToArray implements Ruby's splat (`*v`) coercion: an Array passes through; -// any other object that responds to #to_a is converted with it (MatchData, nil, -// Set, …) provided the result is an Array; everything else is wrapped in a -// one-element Array. Note MRI uses #to_a here, not #to_ary. +// splatToArray implements Ruby's splat (`*v`) coercion: an Array — including an +// instance of a user subclass — passes through; any other object that responds +// to #to_a is converted with it (MatchData, nil, Set, …) provided the result is +// an Array; everything else is wrapped in a one-element Array. Note MRI uses +// #to_a here, not #to_ary. +// +// "Responds to" is respondsToConversion, not respondsTo, because MRI sends the +// conversion through rb_check_funcall, which consults an OVERRIDDEN #respond_to? +// first and does not call the method when that answers false +// (vm_eval.c check_funcall_respond_to). Asking the method table directly instead +// made `*obj` call #to_a on an object that had just said it does not respond to +// it, and take the result. func (vm *VM) splatToArray(v object.Value) object.Value { if a, ok := v.(*object.Array); ok { return a } - if vm.respondsTo(v, "to_a") { + // An Array SUBCLASS is still an Array. MRI reaches the conversion through + // rb_check_to_array -> rb_check_convert_type_with_id(ary, T_ARRAY, "Array", + // idTo_a), which returns the value untouched whenever TYPE(val) is already + // T_ARRAY — and an instance of `Class.new(Array)` is T_ARRAY — so #to_a is + // never sent. Here that shape is an RObject whose `builtin` holds the wrapped + // array, which is this VM's spelling of the same question. + // + // ⛔ The copy is not incidental, and returning the subclass instance itself + // would fix one example by breaking its neighbour. vm_splat_array returns + // rb_ary_dup(tmp), so `(a = *subclass)` is a PLAIN Array: + // language/variables_spec.rb asserts `x.should.instance_of?(Array)` in + // "returns an Array when the splatted object is an Array subclass", and until + // this change that example passed only BECAUSE #to_a was wrongly sent and + // Array#to_a happens to answer with a plain copy. The dup also keeps the + // splat from aliasing the subclass instance's own storage. + if o, ok := v.(*RObject); ok { + if a, ok := o.builtin.(*object.Array); ok { + return object.NewArrayFromSlice(append([]object.Value(nil), a.Elems...)) + } + } + if vm.respondsToConversion(v, "to_a") { r := vm.send(v, "to_a", nil, nil) if a, ok := r.(*object.Array); ok { return a diff --git a/internal/vm/splat_conversion_test.go b/internal/vm/splat_conversion_test.go new file mode 100644 index 00000000..a18c01c7 --- /dev/null +++ b/internal/vm/splat_conversion_test.go @@ -0,0 +1,141 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm_test + +import "testing" + +// A splat (`*v`) reaches #to_a the way MRI's rb_check_to_array does, which means +// it does not reach it at all in two cases this used to get wrong. +// +// MRI compiles `*v` to the splatarray instruction, whose helper is +// +// static inline VALUE +// vm_splat_array(VALUE flag, VALUE ary) +// { +// VALUE tmp = rb_check_to_array(ary); +// if (NIL_P(tmp)) return rb_ary_new3(1, ary); +// else if (RTEST(flag)) return rb_ary_dup(tmp); +// else return tmp; +// } +// +// and rb_check_to_array is rb_check_convert_type_with_id(ary, T_ARRAY, "Array", +// idTo_a). Two properties of that call are the whole of this test: it returns +// the value UNTOUCHED when the value is already T_ARRAY — which an Array +// subclass instance is — and it goes through rb_check_funcall, which asks an +// overridden #respond_to? first and does not call the method when that says no. +// +// Measured against /usr/bin/ruby on the same source before being written here. +func TestSplatDoesNotCallToAWhenMRIWouldNot(t *testing.T) { + cases := []struct{ name, src, want string }{ + { + // ⛔ An Array SUBCLASS. rbgo used to miss this because its fast path is a + // Go type assertion on *object.Array, and a subclass instance is an + // RObject wrapping one — so the assertion failed, Array#to_a was found + // in the method table, and the spec's `should_not_receive(:to_a)` fired. + name: "an Array subclass is already an Array", + src: `cls = Class.new(Array) do + def to_a; $called = true; super; end +end +$called = false +a = *cls.new([1, 2]) +p [a, $called]`, + want: "[[1, 2], false]\n", + }, { + // ⛔ The neighbour that makes the fix non-obvious: the RESULT must be a + // plain Array, because vm_splat_array returns rb_ary_dup(tmp). Until the + // fix this passed only BECAUSE #to_a was wrongly sent — Array#to_a + // answers with a plain copy — so returning the subclass instance would + // have repaired the row above by breaking this one. + name: "the result of splatting a subclass is a plain Array", + src: `cls = Class.new(Array) +x = (a = *cls.new([1, 2])) +p [x.instance_of?(Array), a.instance_of?(Array)]`, + want: "[true, true]\n", + }, { + // And the dup is a dup: the splat must not alias the subclass instance's + // own storage, or appending to the result would mutate the original. + name: "splatting a subclass does not alias its storage", + src: `cls = Class.new(Array) +ary = cls.new([1, 2]) +a = *ary +a << 3 +p [ary.size, a.size]`, + want: "[2, 3]\n", + }, { + // ⛔ An object that says it does not respond to #to_a. rbgo asked the + // method table directly, so it called #to_a on an object that had just + // answered false — and took the result, which is the visible damage: + // the element was :wrong instead of the object itself. + name: "respond_to? false means no conversion", + src: `class SplatRefuser + def respond_to?(name, include_private = false); false; end + def to_a; [:wrong]; end +end +a = *SplatRefuser.new +p [a.size, a.first.is_a?(SplatRefuser)]`, + want: "[1, true]\n", + }, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + }) + } +} + +// The controls: every shape that must keep working, including the ones that +// prove the conversion still HAPPENS. A splat that had simply stopped calling +// #to_a would pass all four rows above. +func TestSplatStillConverts(t *testing.T) { + cases := []struct{ name, src, want string }{ + {"a plain Array passes through", `p(*[1, 2])`, "1\n2\n"}, + {"an Array is copied, not aliased", `ary = [1, 2] +a = *ary +a << 3 +p [ary.size, a.size]`, "[2, 3]\n"}, + {"nil splats to empty", `a = *nil +p a`, "[]\n"}, + {"an object answering #to_a is converted", `class SplatYes + def to_a; [7, 8]; end +end +p(*SplatYes.new)`, "7\n8\n"}, + // A private #to_a IS reached, by both engines: rb_check_funcall passes + // priv=TRUE, and here the method table lookup finds a private method just + // the same. Measured on /usr/bin/ruby — this row asserts MRI's answer, not + // the one that reads as tidier. + {"a private #to_a is reached", `class SplatPriv + private + def to_a; [7, 8]; end +end +a = *SplatPriv.new +p a`, "[7, 8]\n"}, + {"a nil #to_a result wraps the object", `class SplatNil + def to_a; nil; end +end +a = *SplatNil.new +p a.first.is_a?(SplatNil)`, "true\n"}, + {"a non-Array #to_a result is a TypeError", `class SplatBad + def to_a; 42; end +end +begin + a = *SplatBad.new +rescue TypeError => e + puts e.message +end`, "can't convert SplatBad to Array (SplatBad#to_a gives Integer)\n"}, + // Hash#to_a exists, so a Hash is CONVERTED, not wrapped — the pair list, + // not the Hash in a one-element Array. Measured both ways round. + {"a Hash is converted through #to_a", `a = *{x: 1} +p a`, "[[:x, 1]]\n"}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + }) + } +} From 8b12b98fd0e9a3397b115c41676d96ec31cebbe4 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 1 Nov 2026 10:43:26 +0100 Subject: [PATCH 2/2] vm: no #respond_to? at all is not an overridden one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ratchet on this branch failed with one regression, `language/array_spec.rb` 23 → 22: ``` The unpacking splat operator (*) | when applied to a BasicObject coerces it to Array if it respond_to?(:to_a) undefined method 'respond_to?' for # ``` A BasicObject does not include Kernel, so it has no `#respond_to?` at all — and `respondsToConversion` asked ```go if m := vm.findMethod(v, "respond_to?"); m != vm.cKernel.methods["respond_to?"] { ``` which is TRUE for `nil`. An object with no `#respond_to?` therefore took the branch that CALLS it. MRI says what to do instead, and says it first thing: `vm_respond_to` opens with `if (!cme) return -1`, and −1 means **undecided** rather than false, so `rb_check_funcall` falls through to the plain method lookup. One `m != nil` here is that line. ## ⛔ Two of the four shapes were already broken on `main` The same helper serves the masgn path's `#to_ary`, so this was live before this branch existed — with no spec to catch it. Measured through `cmd/oracle` on the reference, the base commit, and this branch: | | MRI 2.6.10 | base (`main`) | this branch before | after | |---|---|---|---|---| | `a = *BasicObject.new` | ok | ok | **NoMethodError** | ok | | splat a BasicObject with `#to_a` | ok | ok | **NoMethodError** | ok | | `a, b = BasicObject.new` | ok | **NoMethodError** | NoMethodError | ok | | masgn a BasicObject with `#to_ary` | ok | **NoMethodError** | NoMethodError | ok | `cmd/oracle` reports `2 engines AGREED, byte for byte` for this branch against `/usr/bin/ruby` on that probe. So routing the splat through the shared helper did not create the defect; it reached a spec that could see it. `language/array_spec.rb` is back to 23/23, and the local ratchet now reports no regression. ## ⛔ And a figure in this PR's description is WRONG It claims `language/variables_spec.rb` goes to `pass=118 fail=1` on this branch. It does not — it goes to **`pass=117 fail=2`**. The 118 came from a binary I had built one command before creating this branch, while the working tree still carried #856's warning, so I credited the splat fix with an example that belongs to #856. The two remaining failures name it themselves: the non-ASCII constant (go-ruby-parser#57) and *"warns about accessing uninitialized global variable"*, which is #856's. Measured again, each binary built from its own committed branch and labelled per file rather than read out of one unordered listing: | binary | `language/variables_spec.rb` | |---|---| | base (pre-#856) | `pass=115 fail=4` | | #856 alone | `pass=116 fail=3` | | **this branch alone** | **`pass=117 fail=2`** | | both | `pass=118 fail=1` | A location is not an attribution. The control has to be built from the same commit as the treatment, not from whatever the working tree happened to hold. Tests: four BasicObject shapes in `splat_conversion_test.go`, including the two that were failing on `main`. Refs #799 Co-authored-by: Claude Opus 5 --- internal/vm/splat_conversion_test.go | 54 ++++++++++++++++++++++++++++ internal/vm/vm.go | 15 +++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/internal/vm/splat_conversion_test.go b/internal/vm/splat_conversion_test.go index a18c01c7..36cc138a 100644 --- a/internal/vm/splat_conversion_test.go +++ b/internal/vm/splat_conversion_test.go @@ -87,6 +87,60 @@ p [a.size, a.first.is_a?(SplatRefuser)]`, } } +// ⛔ A BasicObject HAS NO #respond_to?, and treating that as an overridden one +// sent it the method anyway. +// +// `respondsToConversion` asked `findMethod(v, "respond_to?") != Kernel's entry`, +// which is TRUE for nil — so an object outside Kernel took the branch that +// CALLS #respond_to?: +// +// a = *BasicObject.new # NoMethodError: undefined method 'respond_to?' +// +// MRI says what to do instead, and says it first thing: vm_respond_to opens with +// `if (!cme) return -1`, and -1 means UNDECIDED rather than false, so +// rb_check_funcall falls through to the plain method lookup. +// +// Two of these four shapes were already broken on `main` — the masgn path uses +// the same helper for #to_ary — and no spec covered them; routing the splat +// through that helper is what surfaced it, via +// `language/array_spec.rb`'s "when applied to a BasicObject coerces it to Array +// if it respond_to?(:to_a)". All four agree with /usr/bin/ruby now, byte for +// byte, which is how they were checked. +func TestConversionOfAnObjectWithNoRespondTo(t *testing.T) { + cases := []struct{ name, src, want string }{ + { + "splatting a bare BasicObject wraps it", + `a = *BasicObject.new +p a.size`, "1\n", + }, { + "splatting a BasicObject with #to_a converts it", + `k = Class.new(BasicObject) do + def to_a; [1, 2]; end +end +p(*k.new)`, "1\n2\n", + }, { + // ⛔ Broken on main, through the same helper, with no spec to catch it. + "multiple assignment from a bare BasicObject", + `a, b = BasicObject.new +p b`, "nil\n", + }, { + "multiple assignment from a BasicObject with #to_ary", + `k = Class.new(BasicObject) do + def to_ary; [1, 2]; end +end +a, b = k.new +p [a, b]`, "[1, 2]\n", + }, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + }) + } +} + // The controls: every shape that must keep working, including the ones that // prove the conversion still HAPPENS. A splat that had simply stopped calling // #to_a would pass all four rows above. diff --git a/internal/vm/vm.go b/internal/vm/vm.go index cf69be33..8c519f21 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -3942,8 +3942,21 @@ func (vm *VM) defineModuleIn(parent *RClass, name string, body *bytecode.ISeq, s // ruby/spec's "does not call #to_ary if #respond_to? returns false" pins down. // With the default #respond_to? the plain method lookup answers, and no Ruby // call is made. +// +// ⛔ NO #respond_to? AT ALL is not an overridden one, and conflating the two +// sent #respond_to? to an object that does not have it. A BasicObject does not +// include Kernel, so findMethod answers nil — and `nil != Kernel's entry` is +// true, which took the branch that calls it: +// +// a, b = BasicObject.new # NoMethodError: undefined method 'respond_to?' +// +// That was live on `main` through the masgn path before this guard; MRI runs it +// without complaint. Upstream says so explicitly: vm_respond_to opens with +// `if (!cme) return -1`, and -1 means UNDECIDED rather than false, so +// rb_check_funcall goes on to the plain method lookup — which is the fall- +// through below. func (vm *VM) respondsToConversion(v object.Value, name string) bool { - if m := vm.findMethod(v, "respond_to?"); m != vm.cKernel.methods["respond_to?"] { + if m := vm.findMethod(v, "respond_to?"); m != nil && m != vm.cKernel.methods["respond_to?"] { return vm.send(v, "respond_to?", []object.Value{object.SymVal(name)}, nil).Truthy() } return vm.findMethod(v, name) != nil