From 0c4f98f6c1296712a0ab5cba421557066cbc52d9 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 30 Aug 2026 13:53:18 +0200 Subject: [PATCH] Coerce a non-Hash keyword splat (**obj) via #to_hash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A `**operand` keyword splat whose operand was not already a Hash raised TypeError unconditionally, so `f(**obj)` for an object with #to_hash — and `f(**nil)` — failed where MRI accepts them. The OpHashMerge handler now mirrors MRI's implicit keyword-splat conversion: nil contributes nothing (`f(**nil)` is `f()`), any other non-Hash operand is coerced with #to_hash, and a value with no #to_hash ("no implicit conversion of X into Hash") or whose #to_hash yields a non-Hash ("can't convert X to Hash (X#to_hash gives Y)") raises TypeError with MRI's message. A Hash operand and a hash-literal `**` stay on the unchanged fast path. Verified against MRI Ruby 4.0.6, including the `Encoding::Converter.new(…, **opts)` spec whose options carry #to_hash. ruby/spec ratchet FLOOR 16305 -> 16335 (measured total 16391). Co-Authored-By: Claude Opus 4.8 --- internal/vm/kwsplat_to_hash_test.go | 51 +++++++++++++++++++++++++++++ internal/vm/vm.go | 19 ++++++++++- scripts/conformance/rubyspec/FLOOR | 2 +- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 internal/vm/kwsplat_to_hash_test.go diff --git a/internal/vm/kwsplat_to_hash_test.go b/internal/vm/kwsplat_to_hash_test.go new file mode 100644 index 00000000..1392ab3a --- /dev/null +++ b/internal/vm/kwsplat_to_hash_test.go @@ -0,0 +1,51 @@ +package vm_test + +import ( + "strings" + "testing" +) + +// TestKeywordSplatToHash covers the `**operand` keyword splat when the operand +// is not already a Hash: nil contributes nothing, any other value is coerced via +// #to_hash, and a missing or non-Hash #to_hash is a TypeError — all matching MRI +// Ruby 4.0.6. A Hash operand and a hash-literal `**` are the unchanged fast path. +func TestKeywordSplatToHash(t *testing.T) { + cases := []struct{ src, want string }{ + // An object with #to_hash is splatted into keyword arguments. + {`o = Object.new; def o.to_hash; {a: 1, b: 2}; end +def f(**k); k; end +p f(**o)`, "{a: 1, b: 2}\n"}, + // The splat follows positional arguments and mingles with the rest hash. + {`o = Object.new; def o.to_hash; {x: 9}; end +def g(m, **k); [m, k]; end +p g(1, **o)`, "[1, {x: 9}]\n"}, + // **nil contributes nothing (f(**nil) is f()). + {`def f(**k); k; end; p f(**nil)`, "{}\n"}, + // A Hash operand splats unchanged (the fast path), and later keys win. + {`def f(**k); k; end; p f(**{}, **{a: 1})`, "{a: 1}\n"}, + {`h = {a: 1}; o = Object.new; def o.to_hash; {b: 2}; end +def f(**k); k; end +p f(**h, **o)`, "{a: 1, b: 2}\n"}, + // The coercion also applies inside a hash literal's ** entry. + {`o = Object.new; def o.to_hash; {c: 3}; end; p({x: 0, **o})`, "{x: 0, c: 3}\n"}, + {`p({**{a: 1}, **{b: 2}})`, "{a: 1, b: 2}\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } + + errCases := []struct{ src, substr string }{ + // No #to_hash: the implicit-conversion TypeError. + {`o = Object.new; def f(**k); k; end; f(**o)`, "no implicit conversion of Object into Hash"}, + // #to_hash returning a non-Hash: the "gives" TypeError. + {`o = Object.new; def o.to_hash; 42; end; def f(**k); k; end; f(**o)`, + "can't convert Object to Hash (Object#to_hash gives Integer)"}, + } + for _, c := range errCases { + if err := runErr(t, c.src); err == nil || !strings.Contains(err.Error(), c.substr) { + t.Errorf("src=%q err=%v, want containing %q", c.src, err, c.substr) + } + } +} diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 9675c7cd..293e4c4c 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -1358,7 +1358,24 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, val := pop() other, ok := val.(*object.Hash) if !ok { - raise("TypeError", "no implicit conversion of %s into Hash", vm.classOf(val).name) + // A `**operand` keyword splat that is not already a Hash: nil + // contributes nothing (`f(**nil)` ≡ `f()`), and any other value is + // coerced via #to_hash — matching MRI's implicit keyword-splat + // conversion. A value with no #to_hash, or whose #to_hash yields a + // non-Hash, is a TypeError. + if val == object.NilV { + break + } + if !vm.respondsToDynamic(val, "to_hash") { + raise("TypeError", "no implicit conversion of %s into Hash", vm.classOf(val).name) + } + r := vm.send(val, "to_hash", nil, nil) + h, hok := r.(*object.Hash) + if !hok { + raise("TypeError", "can't convert %s to Hash (%s#to_hash gives %s)", + vm.classOf(val).name, vm.classOf(val).name, vm.classOf(r).name) + } + other = h } acc := stack[len(stack)-1].(*object.Hash) for _, k := range other.Keys { diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index f1124092..bfbb6b9b 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -16315 +16335