Repository navigation
Hash: advance MRI 4.0.6 conformance (capacity: kwarg, allocate, mutate-during-each, transform_keys! break, ruby2_keywords subclass) - #569
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Advances
core/hashconformance against MRI Ruby 4.0 (ruby/spec). Thecore/hash/*_spec.rbcorpus goes from 532 → 538 passing examples (fail+error25 → 19), with no regressions. All work is in the
Hashsection ofinternal/vm/builtins.go; the equality / hash-code paths are untouched.Fixes
Hash.new(capacity:)— a trailing{capacity: …}keyword hash is apreallocation hint rbgo does not act on, so it is stripped before the
positional default is read:
Hash.new(capacity: 42).defaultisnil(not thekeyword hash),
Hash.new(5, capacity: 42).defaultis5, andHash.new(capacity: 42) { … }installs adefault_procwith no arity error.Only a hash whose sole key is
:capacityis treated this way, so ordinarysymbol-keyed positional defaults are unaffected. (
core/hash/new)Hash.allocate— returned a bareRObjectwith no backing value, soHash.allocate.sizeblew up; it now returns a fully-formed emptyHash(and asubclass gets a wrapped
Hashkeeping its class identity). (core/hash/allocate)Hash#each/#each_pair/#each_key/#each_value— now iterate asnapshot of the keys through a shared
hashEachLivehelper, so deleting orshifting 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 abreakleaves 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 aHashsubclassand 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/hashresiduals need machinery outside theHashbuiltins:inspect_spec(11) is aruby_version_is-as-ternary artifact in the sharedspec_helper — rbgo's output already matches MRI 4.0;
hash_spec(3) and oneelement_setcase need the#hash/#eql?infrastructure; the subclass-defaultcase in
element_referenceneeds the receiver identity that native dispatchunwraps away;
new_spec's unknown-keyword case and theruby2_keywords_hashivar-copy /
Module#ruby2_keywordscases depend on keyword-argument separationand plain-builtin instance-variable storage that rbgo does not yet have.
Verification
go test ./internal/vm/andgo test ./...greenGOOS=wasip1 GOARCH=wasm go build ./...OKgofmt -l internal/vm/emptystripHashCapacityKwarg,hashEachLive,hashOrSubclassArg, and the touched builtins), no shadoweddead code
🤖 Generated with Claude Code