Skip to content

Array/Hash value equality: dispatch member #==/#eql?/#hash (MRI rb_equal) - #533

Merged
tannevaled merged 4 commits into
mainfrom
feat/agent-arrayeq
Aug 31, 2026
Merged

tannevaled merged 4 commits into
mainfrom
feat/agent-arrayeq

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Array / Hash value equality (MRI rb_equal / rb_eql)

Array#==, Array#eql?, Hash#==, Hash#eql?, and the membership / set
operations compared elements by Go identity or the internal structural path, so
an element with a redefined #== / #eql? / #hash was ignored:
[C.new] == [C.new] was false where MRI gives true.

What changed

  • Element #== dispatch — Array#== / Hash#== and include?, index,
    rindex, count, assoc, rassoc, key, value?, Hash#<=/</>=/>
    now dispatch each member's own Ruby #== (MRI's rb_equal per element),
    including the reflexive Integer#== rule and the #equal? identity
    short-circuit (so a=[]; a<<a; a==a and [NaN] == [NaN] are both true).
  • #eql? / #hash dispatch — Array#eql? / Hash#eql? compare members
    with #eql?; uniq, &, |, -, difference, union, intersection
    group members by Ruby #hash and compare with #eql? (a custom #hash/#eql?
    participates; #eql? is never asked across differing hash codes; a non-Integer
    #hash raises TypeError).
  • Array#== operand handling — accepts an Array-like #to_ary object
    (reflexive dispatch), treats an Array/Hash subclass structurally; Hash#==
    accepts a #to_hash object.
  • Array#<=> — returns an element's raw non-zero <=> result, converts a
    non-Array argument via #to_ary, and unwraps an Array subclass without calling
    to_ary.
  • Hash key lookup — no longer sends #eql? when the stored key IS the lookup
    object (identity), so Hash#== does not spuriously compare a key to itself.

rubyspec deltas (core)

spec before after
array/equal_value 7/13 13/13
array/eql 11/11 11/11
hash/equal_value 13/17 17/17
hash/eql — 16/16
array/comparison 7/10 10/10
array/uniq 17/27 25/27

The 2 remaining uniq failures are the pre-existing "tolerates increasing an
array size during iteration" edge (an iteration-helper concern, out of scope).

Verification

  • go test ./internal/vm/ and go test ./... green.
  • GOOS=wasip1 GOARCH=wasm go build ./... OK; gofmt -l empty on changed files.
  • Merged coverage 100.0% (-coverpkg over all internal/...); every changed
    function at 100% statement coverage.

🤖 Generated with Claude Code

tannevaled and others added 4 commits August 31, 2026 15:36
Array#==, Hash#==, and the membership/search methods (include?, index,
rindex, count, assoc, rassoc, key, value?, Hash#<=/</>=/>) compared
elements by Go identity or the internal structural path, so an element
with a redefined #== was ignored: [C.new] == [C.new] was false where MRI
gives true.

Thread a live VM through the recursive value-equality routine
(valueEqualRec) so every leaf whose own #== matters — a user object, or a
built-in with a custom #== such as Digest/Set — has that Ruby #==
dispatched, matching MRI's rb_equal per element (including the reflexive
Integer#== rule and the identity short-circuit that terminates
self-referential members). VM-less callers keep the structural fallback.

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

Extend the value-equality work to eql? and the set operations, and finish
Array#== / Array#<=> conformance:

- Array#eql? / Hash#eql? dispatch each member's own #eql? (VM-aware
  valueEql), so a value with a redefined #eql? participates.
- uniq / & / | / - / difference / union / intersection group members by
  Ruby #hash and compare with #eql? (eqlHashCode + eqlKeys), matching MRI:
  a custom #hash/#eql? participates and #eql? is never asked across
  differing hash codes.
- Array#== accepts an Array-like #to_ary object (reflexive dispatch), treats
  an Array subclass as a plain array, and makes [NaN] == [NaN] true via the
  per-element #equal? short-circuit (while bare NaN == NaN stays false).
- Array#<=> returns an element's raw non-zero <=> result, converts the
  argument via #to_ary, and unwraps an Array subclass without calling to_ary.
- Hash key lookup no longer sends #eql? when the stored key IS the lookup
  object (identity), so Hash#== does not spuriously compare a key to itself.

Specs: equal_value 7->13, hash equal_value 13->17, hash eql 16/16,
comparison 7->10, uniq 17->25 (2 remaining are the pre-existing
iteration-mutation edge). Full internal/vm suite green.

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

- eqlHashCode raises TypeError on a non-Integer #hash (MRI parity) and folds
  a Bignum #hash, via the shared hashAsInt helper.
- Array#- (difference) is handled entirely in binaryOp, which has the VM to
  dispatch each member's #eql?/#hash; the dead VM-less arrayOp #- branch and
  the now-unused free arrayIncludes are removed.
- rb_eql has no reflexive rule, so Array#eql?/Hash#eql? consult only the
  receiver member's #eql? (drop the unreachable right-operand dispatch).
- reflexiveContainerEq is guarded at its callers (no dead nil-VM branch).
- Extend array_equality_test with the #to_ary/#to_hash reflexive cases,
  Array/Hash subclass equality, [NaN] == [NaN], reflexive right operand,
  Bignum/non-Integer #hash, recursive-hash #eql?, and a VM-less Struct-of-Set
  path, bringing every changed function to 100% statement coverage.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Rerouting Set-member comparison through the top-level custom-#== dispatch
left Set.eq reachable only via the VM-less path (Struct#== over a Set field),
so add the unequal and size-mismatched Struct-of-Set cases to keep Set.eq at
100% statement coverage alongside the rest of the changed equality code.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 5325342 into main Aug 31, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-arrayeq branch August 31, 2026 15:43
tannevaled added a commit that referenced this pull request Aug 31, 2026
Locks in the gains from #530 (Encoding compatibility: Converter.new,
concat negotiation, multibyte codepoints, ascii_only?), #531 (language
def/method/super/keyword-argument), #532 (Enumerable residuals) and #533
(Array/Hash value equality dispatching #==/#eql?/#hash). Measured 17654
passing on the pinned corpus, up from 17470.

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