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..36cc138a --- /dev/null +++ b/internal/vm/splat_conversion_test.go @@ -0,0 +1,195 @@ +// 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) + } + }) + } +} + +// ⛔ 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. +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) + } + }) + } +} 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