Skip to content

Hash: advance MRI 4.0.6 conformance (capacity: kwarg, allocate, mutate-during-each, transform_keys! break, ruby2_keywords subclass) - #569

Merged
tannevaled merged 3 commits into
mainfrom
feat/agent-hash2
Sep 6, 2026
Merged

tannevaled merged 3 commits into
mainfrom
feat/agent-hash2

Conversation

@tannevaled

@tannevaled tannevaled commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Advances core/hash conformance against MRI Ruby 4.0 (ruby/spec). The
core/hash/*_spec.rb corpus goes from 532 → 538 passing examples (fail+error
25 → 19), with no regressions. All work is in the Hash section of
internal/vm/builtins.go; the equality / hash-code paths are untouched.

Fixes

  • Hash.new(capacity:) — a trailing {capacity: …} keyword hash is a
    preallocation hint rbgo does not act on, so it is stripped before the
    positional default is read: Hash.new(capacity: 42).default is nil (not the
    keyword hash), Hash.new(5, capacity: 42).default is 5, and
    Hash.new(capacity: 42) { … } installs a default_proc with no arity error.
    Only a hash whose sole key is :capacity is treated this way, so ordinary
    symbol-keyed positional defaults are unaffected. (core/hash/new)
  • Hash.allocate — returned a bare RObject with no backing value, so
    Hash.allocate.size blew up; it now returns a fully-formed empty Hash (and a
    subclass gets a wrapped Hash keeping its class identity). (core/hash/allocate)
  • Hash#each / #each_pair / #each_key / #each_value — now iterate a
    snapshot of the keys through a shared hashEachLive helper, so deleting or
    shifting the current entry (or one shifted by Delete's in-place compaction)
    no longer double-visits or skips a surviving key; a key removed ahead of the
    cursor is skipped, matching MRI. (core/hash/delete, core/hash/shift)
  • Hash#transform_keys! — transforms one key at a time in place so a break
    leaves the processed keys rewritten and the rest untouched (it is not atomic),
    reproducing MRI's partial result and key order. (core/hash/transform_keys)
  • Hash.ruby2_keywords_hash / ? — accept an instance of a Hash subclass
    and return the flagged copy as an instance of that subclass, instead of raising
    TypeError. (core/hash/ruby2_keywords_hash)

Out of scope (reported, not fixed)

The remaining core/hash residuals need machinery outside the Hash builtins:
inspect_spec (11) is a ruby_version_is-as-ternary artifact in the shared
spec_helper — rbgo's output already matches MRI 4.0; hash_spec (3) and one
element_set case need the #hash/#eql? infrastructure; the subclass-default
case in element_reference needs the receiver identity that native dispatch
unwraps away; new_spec's unknown-keyword case and the ruby2_keywords_hash
ivar-copy / Module#ruby2_keywords cases depend on keyword-argument separation
and plain-builtin instance-variable storage that rbgo does not yet have.

Verification

  • go test ./internal/vm/ and go test ./... green
  • GOOS=wasip1 GOARCH=wasm go build ./... OK
  • gofmt -l internal/vm/ empty
  • 100% coverage on every changed function (stripHashCapacityKwarg,
    hashEachLive, hashOrSubclassArg, and the touched builtins), no shadowed
    dead code

🤖 Generated with Claude Code

tannevaled and others added 3 commits September 6, 2026 10:14
…during-each, transform_keys! break)

- Hash.new/#initialize: strip a trailing {capacity: ...} keyword hash (a
  preallocation hint rbgo does not act on) so Hash.new(capacity: 42).default
  is nil and Hash.new(capacity: 42) { } sets a default_proc (core/hash/new).
- Hash.allocate: return a fully-formed empty Hash (Class#allocate built a bare
  RObject with no backing value, so #size failed); a subclass gets a wrapped
  Hash keeping its class identity (core/hash/allocate).
- Hash#each/#each_key/#each_value: iterate a snapshot of the keys via a shared
  hashEachLive helper, so deleting/shifting the current (or a shifted) entry in
  the block no longer double-visits or skips, matching MRI (core/hash/delete,
  core/hash/shift).
- Hash#transform_keys!: transform in place one key at a time so a break leaves
  the processed keys rewritten and the rest untouched, matching MRI's ordering
  and partial-result semantics (core/hash/transform_keys).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Cover every new branch: stripHashCapacityKwarg (strip vs keep), Hash.allocate
(base and subclass), hashEachLive (delete-ahead skip + the three closures) and
the in-place transform_keys! (break, seen-set retention, mapping reorder).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
hashArgOrRaise rejected a subclass instance (an RObject wrapping a Hash), so
Hash.ruby2_keywords_hash(MyHash.new) raised TypeError. Unwrap via a new
hashOrSubclassArg helper and, for a subclass argument, rebuild the flagged copy
as an instance of that subclass so kw.class stays MyHash and the predicate reads
the flag off either form (core/hash/ruby2_keywords_hash).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled tannevaled changed the title Hash: advance MRI 4.0.6 conformance (core/hash residuals) Hash: advance MRI 4.0.6 conformance (capacity: kwarg, allocate, mutate-during-each, transform_keys! break, ruby2_keywords subclass) Sep 6, 2026
@tannevaled
tannevaled merged commit 725471e into main Sep 6, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-hash2 branch September 6, 2026 09:28
tannevaled added a commit that referenced this pull request Sep 6, 2026
Locks in #569 (Hash), #570 (Encoding subsystem), #571 (Range) and #572 (the
!= opcode dispatching a user-defined #!=). Measured 18790 passing on the pinned
corpus, up from 18767.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant