Skip to content

Hash: advance MRI 4.0.6 conformance (aliases, block arity, compare_by_identity/default retention, sized enumerators, inspect dispatch, dig/fetch/delete/default_proc=/initialize/to_hash) - #545

Merged
tannevaled merged 3 commits into
mainfrom
feat/agent-hash
Sep 1, 2026

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advances MRI 4.0.6 conformance for core/hash residual (non-equality) methods. Measured against /opt/homebrew/bin/ruby 4.0.x and the rubyspec corpus: core/hash/*_spec.rb goes from ~479 to 532 passing examples (fail 21, error 4), with no regressions in the hash suite or in core/enumerable Hash usage.

What changed (Hash methods only)

Genuine built-in aliases — installed as shared method records so Hash.instance_method(:x) == Hash.instance_method(:y) holds, fixing whole spec files that were failing on identity:

  • has_key?/include?/member? ↔ key?, has_value? ↔ value?, length ↔ size, store ↔ []=, update ↔ merge!, filter ↔ select, filter! ↔ select!, to_s ↔ inspect.

Block arity — select/reject/delete_if/keep_if/select!/reject! now yield the key and value as two separate arguments (a |*a| block sees [k, v], a single-parameter block sees the key), matching MRI; each/map still yield the single pair.

compare_by_identity / default propagation — merge/slice/except/select/reject/compact/transform_values carry the receiver's compare_by_identity flag into their result (set before insertion, so identity-distinct keys survive); merge additionally retains the receiver's default value and default proc; Hash.ruby2_keywords_hash retains all three.

Sized enumerators — the block-less iteration methods return sized Enumerators, so #size reports the hash length without materialising — which for the in-place forms (transform_values!, select!, …) had destructively mutated the hash.

Per-method fixes

  • Hash#inspect dispatches #inspect to each key/value through the VM (honouring a Ruby-level override) and to_s is a true alias of it; symbol labels follow MRI's 4.0 rule (a: bare, "<=>": quoted).
  • dig reads a first-level miss through the hash default, so it respects Hash.new defaults and keeps digging.
  • merge/merge!/update coerce a non-Hash argument via #to_hash.
  • fetch raises ArgumentError for other than 1..2 arguments.
  • delete calls a supplied block with the key when the key is absent.
  • to_h with a block coerces a non-Array pair via #to_ary.
  • new Hash#to_hash returning self.
  • Hash[] coerces a single argument via #to_hash then #to_ary, and names a nil element as nil (not NilClass) in its error.
  • Hash#initialize resets both the default value and default proc and is marked private.
  • default_proc= coerces via #to_proc and rejects a fixed-arity lambda whose arity is not 2.

Out of scope / not fixed

The remaining residual failures need machinery outside the Hash methods: hash_spec (#hash value function — deliberately untouched), inspect_spec (stale corpus expecting the pre-3.4 => format, which MRI 4.0 itself no longer produces), new_spec capacity:/unknown-keyword handling and allocate/subclass wrapping and Hash instance variables (VM call-convention/allocation features), and the transform_keys!/delete/shift mutate-while-iterating cases.

Verification

  • go test ./internal/vm/ and go test ./... green.
  • GOOS=wasip1 GOARCH=wasm go build ./... OK.
  • gofmt -l clean on changed files.
  • New internal/vm/hash_method_residuals_test.go covers the added branches; every function changed/added is at 100% coverage; repo total unchanged at 99.9%.

🤖 Generated with Claude Code

tannevaled and others added 3 commits September 1, 2026 09:40
… sized enumerators, inspect dispatch

Advance MRI 4.0.6 conformance for core/hash residual methods:
- Make has_key?/include?/member?/length/store/update/filter/filter!/to_s
  genuine built-in aliases (shared method records) so UnboundMethod == holds.
- select/reject/delete_if/keep_if/select!/reject! yield key and value as two
  separate arguments (not a single pair), matching MRI.
- merge/slice/except/select/reject/compact/transform_values propagate the
  compare_by_identity flag; merge also carries the receiver's default and
  default_proc.
- Give Hash iteration methods sized Enumerators so #size does not destructively
  materialise the in-place forms.
- Hash#inspect dispatches #inspect to each key/value through the VM and to_s is
  a true alias of it.
- merge/merge!/update coerce a non-Hash argument via #to_hash.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…cion, to_hash, tests

Further MRI 4.0.6 conformance for core/hash residual methods:
- dig reads a first-level miss through the hash default (value or proc), so it
  respects Hash.new defaults and can keep digging into them.
- fetch raises ArgumentError for other than 1..2 arguments.
- delete calls a supplied block with the key when the key is absent.
- to_h with a block coerces a non-Array pair via #to_ary.
- Add Hash#to_hash returning self.
- Hash[] coerces a single argument via #to_hash then #to_ary, and names a nil
  element as 'nil' (not 'NilClass') in its element-type error.
- Hash#initialize resets both the default value and default proc and is marked
  private.
- default_proc= coerces via #to_proc and rejects a fixed-arity lambda whose
  arity is not 2.
- Hash.ruby2_keywords_hash retains the default, default_proc and
  compare_by_identity flag.
- Add internal/vm/hash_method_residuals_test.go covering the new branches.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ll coverage

Exercise every symbol-label branch of symIsPlainLabel (bare label, trailing
?/!, quoted operator/ivar/setter, empty) by calling #inspect explicitly rather
than via p (which uses the low-level repr).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 5537f17 into main Sep 1, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-hash branch September 1, 2026 08:56
tannevaled added a commit that referenced this pull request Sep 1, 2026
Locks in #544 (Complex + Rational), #545 (Hash methods), #546 (Regexp,
MatchData and match globals). Measured 18102 passing on the pinned corpus,
up from 17983.

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