From 159849addf7b31b6d09693e3483e48c9d3641a93 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Thu, 27 Aug 2026 11:49:50 +0200 Subject: [PATCH] Add Hash#key and Hash.ruby2_keywords_hash / ruby2_keywords_hash? - Hash#key(value) returns the first key whose value equals value (by ==), or nil. - Hash.ruby2_keywords_hash(hash) returns a copy of hash flagged (via a new Ruby2Keywords field) as a keyword hash for *args forwarding, leaving the original unflagged; Hash.ruby2_keywords_hash? reports that flag. Both raise "wrong argument type X (expected Hash)" for a non-Hash argument. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises 15810 -> 15820 (core/hash key +3, ruby2_keywords_hash +5). Co-Authored-By: Claude Opus 4.8 --- internal/object/object.go | 4 ++++ internal/vm/activesupport.go | 10 ++++++++++ internal/vm/builtins.go | 29 +++++++++++++++++++++++++++++ internal/vm/hash_residuals_test.go | 29 +++++++++++++++++++++++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 5 files changed, 73 insertions(+), 1 deletion(-) diff --git a/internal/object/object.go b/internal/object/object.go index 986639b6..b7b49489 100644 --- a/internal/object/object.go +++ b/internal/object/object.go @@ -607,6 +607,10 @@ type Hash struct { // Frozen marks the Hash as frozen (Hash#freeze); an in-place mutator raises // FrozenError. Default false, so ordinary hashes are unaffected. Frozen bool + // Ruby2Keywords flags a hash built by Hash.ruby2_keywords_hash, marking it as + // a keyword-argument hash for `*args`-style forwarding (reported by + // Hash.ruby2_keywords_hash?). Default false. + Ruby2Keywords bool } // strKey is the comparable map key for a Ruby String, distinct from a Symbol of diff --git a/internal/vm/activesupport.go b/internal/vm/activesupport.go index f5387de2..e0751f10 100644 --- a/internal/vm/activesupport.go +++ b/internal/vm/activesupport.go @@ -853,3 +853,13 @@ func hashArg(v object.Value) *object.Hash { } return h } + +// hashArgOrRaise returns v as a *Hash or raises the "wrong argument type X +// (expected Hash)" TypeError that Hash.ruby2_keywords_hash(?) reports. +func hashArgOrRaise(vm *VM, v object.Value) *object.Hash { + h, ok := v.(*object.Hash) + if !ok { + raise("TypeError", "wrong argument type %s (expected Hash)", vm.classOf(v).name) + } + return h +} diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 143badd7..d7a66130 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -3550,6 +3550,24 @@ func (vm *VM) bootstrap() { } return h }} + // Hash.ruby2_keywords_hash(hash) returns a copy of hash flagged as a keyword + // hash for `*args` forwarding; Hash.ruby2_keywords_hash? reports that flag. + // Both raise TypeError for a non-Hash argument. + vm.cHash.smethods["ruby2_keywords_hash"] = &Method{name: "ruby2_keywords_hash", owner: vm.cHash, + native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + h := hashArgOrRaise(vm, args[0]) + out := object.NewHash() + for _, k := range h.Keys { + v, _ := h.Get(k) + out.Set(k, v) + } + out.Ruby2Keywords = true + return out + }} + vm.cHash.smethods["ruby2_keywords_hash?"] = &Method{name: "ruby2_keywords_hash?", owner: vm.cHash, + native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + return object.Bool(hashArgOrRaise(vm, args[0]).Ruby2Keywords) + }} // #[] reads args[0] and returns a stored/default value; #[]= copies element // values into the hash. Neither retains the args slice, so both take the // no-copy OpSend fast path (defineNR). @@ -3959,6 +3977,17 @@ func (vm *VM) bootstrap() { } vm.cHash.define("has_value?", hashHasValue) vm.cHash.define("value?", hashHasValue) + // Hash#key(value): the first key whose value equals value (by ==), else nil. + vm.cHash.define("key", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + h := self.(*object.Hash) + for _, k := range h.Keys { + v, _ := h.Get(k) + if valueEqual(v, args[0]) { + return k + } + } + return object.NilV + }) // select/reject return a Hash (unlike Enumerable's Array forms), so they are // native rather than inherited. vm.cHash.define("select", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { diff --git a/internal/vm/hash_residuals_test.go b/internal/vm/hash_residuals_test.go index e6fe382f..140243b4 100644 --- a/internal/vm/hash_residuals_test.go +++ b/internal/vm/hash_residuals_test.go @@ -120,3 +120,32 @@ func TestHashCompareByIdentity(t *testing.T) { } } } + +// TestHashKeyAndRuby2Keywords covers Hash#key (reverse value lookup) and the +// Hash.ruby2_keywords_hash / ruby2_keywords_hash? pair, asserted against MRI +// Ruby 4.0.6. +func TestHashKeyAndRuby2Keywords(t *testing.T) { + cases := []struct{ src, want string }{ + {`p({a: 1, b: 2, c: 1}.key(1))`, ":a\n"}, + {`p({a: 1}.key(9))`, "nil\n"}, + {`p({"x" => 5}.key(5))`, "\"x\"\n"}, + {`kh = Hash.ruby2_keywords_hash({a: 1}); p [kh, Hash.ruby2_keywords_hash?(kh)]`, "[{a: 1}, true]\n"}, + {`p Hash.ruby2_keywords_hash?({a: 1})`, "false\n"}, + // The original hash is unchanged; only the returned copy carries the flag. + {`h = {a: 1}; Hash.ruby2_keywords_hash(h); p Hash.ruby2_keywords_hash?(h)`, "false\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want) + } + } + errs := []struct{ src, want string }{ + {`Hash.ruby2_keywords_hash?(5)`, "wrong argument type Integer (expected Hash)"}, + {`Hash.ruby2_keywords_hash("x")`, "wrong argument type String (expected Hash)"}, + } + for _, c := range errs { + if err := runErr(t, c.src); err == nil || !strings.Contains(err.Error(), c.want) { + t.Errorf("src=%q err=%v, want substring %q", c.src, err, c.want) + } + } +} diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 30f2e268..adef6a43 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -15810 +15820