diff --git a/internal/vm/arith.go b/internal/vm/arith.go index 13333ead..48940b71 100644 --- a/internal/vm/arith.go +++ b/internal/vm/arith.go @@ -265,7 +265,14 @@ func (vm *VM) binaryOp(op bytecode.Op, a, b object.Value) object.Value { return object.Bool(eq) } } - return binary(op, a, b) + // The structural compare is VM-aware so that an Array/Hash operand compares + // its elements with each element's own Ruby #== (MRI's rb_equal per element), + // not by Go identity. + eq := vm.vmValueEqual(a, b) + if op == bytecode.OpNeq { + eq = !eq + } + return object.Bool(eq) case bytecode.OpLt, bytecode.OpGt, bytecode.OpLe, bytecode.OpGe: if hasFastOrdering(a) { // A built-in number compared against a non-numeric right operand runs @@ -334,6 +341,16 @@ func (vm *VM) binaryOp(op bytecode.Op, a, b object.Value) object.Value { if _, isSet := a.(*Set); isSet && (op == bytecode.OpAdd || op == bytecode.OpSub) { return vm.send(a, arithOpName(op), []object.Value{b}, nil) } + // Array#- (difference) removes each receiver element #eql? to some element of + // the argument. It routes through the VM so a member's own #eql?/#hash is + // dispatched (MRI's rb_ary_diff), which the VM-less arrayOp path cannot do. + if aa, ok := a.(*object.Array); ok && op == bytecode.OpSub { + bb, ok := b.(*object.Array) + if !ok { + return raise("TypeError", "no implicit conversion of %s into Array", b.Inspect()) + } + return object.NewArrayFromSlice(vm.arrayDifferenceEql(aa.Elems, bb.Elems)) + } // An Enumerator dispatches its + (Enumerator#+ builds an Enumerator::Chain of // self and the operand) as a method rather than falling into the numeric // coercion path. @@ -681,9 +698,9 @@ func stringOp(op bytecode.Op, a *object.String, b object.Value) object.Value { return raise("NoMethodError", "undefined method '%s' for a String", op) } -// arrayOp applies a fast-path operator with an Array receiver: + concatenates, -// - removes (set difference, keeping order/duplicates of the left), and * either -// repeats (Integer) or joins (String). +// arrayOp applies a fast-path operator with an Array receiver: + concatenates and +// * either repeats (Integer) or joins (String). Array#- (difference) is handled +// in binaryOp, which has the VM needed to dispatch each member's #eql?/#hash. func arrayOp(op bytecode.Op, a *object.Array, b object.Value) object.Value { switch op { case bytecode.OpAdd: @@ -694,18 +711,6 @@ func arrayOp(op bytecode.Op, a *object.Array, b object.Value) object.Value { out := make([]object.Value, 0, len(a.Elems)+len(bb.Elems)) out = append(append(out, a.Elems...), bb.Elems...) return object.NewArrayFromSlice(out) - case bytecode.OpSub: - bb, ok := b.(*object.Array) - if !ok { - raise("TypeError", "no implicit conversion of %s into Array", b.Inspect()) - } - var out []object.Value - for _, e := range a.Elems { - if !arrayIncludes(bb.Elems, e) { - out = append(out, e) - } - } - return object.NewArrayFromSlice(out) case bytecode.OpMul: // Array * String (join with a separator) is intercepted in binaryOp and // routed through Array#join for full coercion/encoding handling, so only the @@ -740,43 +745,102 @@ func (vm *VM) curried(p *Proc, need int, got []object.Value) *Proc { }} } -// arrayUniq returns elems with duplicates removed, keeping first-seen order and -// comparing with eql?. With a block, elements are distinguished by the block's -// return value rather than the element itself. +// needsEqlDispatch reports whether a set-operation member's own #eql? / #hash may +// be user-defined and so must be dispatched (a user object, or a built-in whose +// class defines a value-based equality such as Set). A plain value type keeps the +// built-in structural eql?/hash, which is both correct and cheaper. +func (vm *VM) needsEqlDispatch(v object.Value) bool { + if _, ok := v.(*RObject); ok { + return true + } + return hasCustomEq(vm, v) +} + +// eqlHashCode is the bucketing hash for the set operations: MRI groups members by +// #hash before ever calling #eql?, so an object with a custom #hash has it +// dispatched (and only same-bucket members are then compared). A value type uses +// the built-in structural hash. +func (vm *VM) eqlHashCode(v object.Value) int64 { + if !vm.needsEqlDispatch(v) { + return vm.hashValue(v) + } + h := vm.send(v, "hash", nil, nil) + rh, ok := hashAsInt(h) + if !ok { + // MRI requires #hash to return an Integer and raises otherwise. + raise("TypeError", "no implicit conversion of %s into Integer", vm.classOf(h).name) + } + return rh +} + +// eqlKeys reports MRI eql?-equality of two set-operation members: the identity +// short-circuit first — so a member equals itself without #eql? being sent, even +// a deliberately non-reflexive one — then a dispatched #eql? when either side may +// define it, else the built-in structural eql?. +func (vm *VM) eqlKeys(a, b object.Value) bool { + if a == b { + return true + } + if vm.needsEqlDispatch(a) || vm.needsEqlDispatch(b) { + return vm.send(a, "eql?", []object.Value{b}, nil).Truthy() + } + return valueEql(a, b) +} + +// arrayUniq returns elems with duplicates removed, keeping first-seen order. +// Members are grouped by #hash (eqlHashCode) and, within a bucket, compared with +// #eql? (eqlKeys), matching MRI: a redefined #hash/#eql? participates, and #eql? +// is never asked across differing hash codes. With a block, elements are +// distinguished by the block's return value rather than the element itself. func (vm *VM) arrayUniq(elems []object.Value, blk *Proc) []object.Value { - var out, keys []object.Value + var out []object.Value + buckets := map[int64][]object.Value{} for _, e := range elems { key := e if blk != nil { key = vm.callBlock(blk, []object.Value{e}) } + h := vm.eqlHashCode(key) seen := false - for _, k := range keys { - if valueEql(key, k) { + for _, k := range buckets[h] { + if vm.eqlKeys(key, k) { seen = true break } } if !seen { - keys = append(keys, key) + buckets[h] = append(buckets[h], key) out = append(out, e) } } return out } -// arrayIncludes backs the set operators &, | and - (difference), which compare -// with eql? — so e.g. 1 and 1.0 are distinct members. Membership tests like -// include?/index/count use == instead and do not go through here. -func arrayIncludes(elems []object.Value, v object.Value) bool { +// arrayIncludesEql backs the set operators &, |, - and their named forms, which +// compare members with #eql? (so e.g. 1 and 1.0 are distinct, and a user #eql? +// participates). Membership tests like include?/index/count use == instead and do +// not go through here. +func (vm *VM) arrayIncludesEql(elems []object.Value, v object.Value) bool { for _, e := range elems { - if valueEql(e, v) { + if vm.eqlKeys(e, v) { return true } } return false } +// arrayDifferenceEql implements Array#- : every receiver element that is #eql? to +// no element of the argument, receiver duplicates preserved. +func (vm *VM) arrayDifferenceEql(a, b []object.Value) []object.Value { + var out []object.Value + for _, e := range a { + if !vm.arrayIncludesEql(b, e) { + out = append(out, e) + } + } + return out +} + func negate(v object.Value) object.Value { switch n := v.(type) { case object.Integer: @@ -810,10 +874,77 @@ func negate(v object.Value) object.Value { type eqPair struct{ a, b object.Value } func valueEqual(a, b object.Value) bool { - return valueEqualRec(a, b, nil) + return valueEqualRec(nil, a, b, nil) +} + +// vmValueEqual is the VM-aware form of valueEqual: it compares two values +// structurally like valueEqual, but every leaf whose own #== matters — a user +// object (RObject) or a built-in with a custom #== (Digest, Set, …) — has that +// Ruby #== dispatched, matching MRI's rb_equal. This is what makes Array#== and +// Hash#== compare their elements by value (an element with a redefined #== +// participates) rather than by Go identity. A nil VM falls back to the +// structural-only comparison, so VM-less callers keep their current behaviour. +func (vm *VM) vmValueEqual(a, b object.Value) bool { + return valueEqualRec(vm, a, b, nil) +} + +// identityEqual is rb_equal's `#equal?` short-circuit: two values that are the +// same object are equal without any further comparison. Two Floats with the same +// bit pattern count as identical too — this is what makes `[NaN] == [NaN]` true +// (Array#== compares elements with rb_equal, and NaN.equal?(NaN) holds for the +// shared flonum) while a bare `NaN == NaN` stays false. +func identityEqual(a, b object.Value) bool { + if a == b { + return true + } + if af, ok := a.(object.Float); ok { + if bf, ok := b.(object.Float); ok { + return math.Float64bits(float64(af)) == math.Float64bits(float64(bf)) + } + } + return false +} + +// rbEqualElem compares two container members the way MRI's rb_equal does: the +// #equal? identity short-circuit first, then the full value comparison (which +// dispatches a member's own #== when the VM is live). A nil VM keeps the plain +// structural comparison for VM-less callers. +func (vm *VM) rbEqualElem(a, b object.Value, seen map[eqPair]struct{}) bool { + if vm != nil && identityEqual(a, b) { + return true + } + return valueEqualRec(vm, a, b, seen) +} + +// reflexiveContainerEq handles Array#== / Hash#== against an operand that is not +// the matching container type: MRI accepts one that answers the conversion method +// (#to_ary / #to_hash) by dispatching `other == self` (rb_equal). Restricted to +// user objects — the only values that carry such a coercion without being the +// container type. Callers invoke it only with a live VM. +func (vm *VM) reflexiveContainerEq(self, other object.Value, conv string) bool { + if _, isObj := other.(*RObject); !isObj { + return false + } + if !vm.send(other, "respond_to?", []object.Value{object.Symbol(conv)}, nil).Truthy() { + return false + } + return self == other || vm.send(other, "==", []object.Value{self}, nil).Truthy() } -func valueEqualRec(a, b object.Value, seen map[eqPair]struct{}) bool { +func valueEqualRec(vm *VM, a, b object.Value, seen map[eqPair]struct{}) bool { + // With a live VM, a leaf whose class defines its own #== (a user object, or a + // built-in like Digest/Set/BCrypt::Password) dispatches that method — MRI's + // rb_equal, which is what Array#== / Hash#== use per element. The identity + // shortcut mirrors rb_equal's `a == b` pointer check, which also terminates a + // self-referential element without recursing into its #==. + if vm != nil { + if _, isObj := a.(*RObject); isObj { + return a == b || vm.send(a, "==", []object.Value{b}, nil).Truthy() + } + if hasCustomEq(vm, a) { + return a == b || vm.send(a, "==", []object.Value{b}, nil).Truthy() + } + } // Complex compares component-wise, and equals a real number when its // imaginary part is zero (Complex(2, 0) == 2), in either operand order. if ac, ok := a.(*object.Complex); ok { @@ -883,7 +1014,19 @@ func valueEqualRec(a, b object.Value, seen map[eqPair]struct{}) bool { return ok && av == bv case *object.Array: bv, ok := b.(*object.Array) - if !ok || len(av.Elems) != len(bv.Elems) { + if !ok { + // An Array subclass instance is still a T_ARRAY to MRI, compared + // structurally (its #to_ary is never consulted), so unwrap it. + if o, isObj := b.(*RObject); isObj { + bv, ok = o.builtin.(*object.Array) + } + } + if !ok { + // Array#== with a non-Array that answers #to_ary: MRI dispatches + // `other == self` (rb_equal), so an Array-like object compares equal. + return vm != nil && vm.reflexiveContainerEq(a, b, "to_ary") + } + if len(av.Elems) != len(bv.Elems) { return false } key := eqPair{av, bv} @@ -896,14 +1039,24 @@ func valueEqualRec(a, b object.Value, seen map[eqPair]struct{}) bool { seen[key] = struct{}{} defer delete(seen, key) for i := range av.Elems { - if !valueEqualRec(av.Elems[i], bv.Elems[i], seen) { + if !vm.rbEqualElem(av.Elems[i], bv.Elems[i], seen) { return false } } return true case *object.Hash: bv, ok := b.(*object.Hash) - if !ok || av.Len() != bv.Len() { + if !ok { + // A Hash subclass instance is still a T_HASH, compared structurally. + if o, isObj := b.(*RObject); isObj { + bv, ok = o.builtin.(*object.Hash) + } + } + if !ok { + // Hash#== with a non-Hash that answers #to_hash dispatches `other == self`. + return vm != nil && vm.reflexiveContainerEq(a, b, "to_hash") + } + if av.Len() != bv.Len() { return false } key := eqPair{av, bv} @@ -918,14 +1071,14 @@ func valueEqualRec(a, b object.Value, seen map[eqPair]struct{}) bool { for _, k := range av.Keys { ae, _ := av.Get(k) be, present := bv.Get(k) - if !present || !valueEqualRec(ae, be, seen) { + if !present || !vm.rbEqualElem(ae, be, seen) { return false } } return hashIdentityMatch(av, bv) case *object.Range: bv, ok := b.(*object.Range) - return ok && av.Exclusive == bv.Exclusive && valueEqualRec(av.Lo, bv.Lo, seen) && valueEqualRec(av.Hi, bv.Hi, seen) + return ok && av.Exclusive == bv.Exclusive && vm.rbEqualElem(av.Lo, bv.Lo, seen) && vm.rbEqualElem(av.Hi, bv.Hi, seen) case *Set: bv, ok := b.(*Set) return ok && av.eq(bv) @@ -963,6 +1116,17 @@ func valueEqualRec(a, b object.Value, seen map[eqPair]struct{}) bool { bb, ok := b.(*ioBuffer) return ok && !ab.freed && !bb.freed && string(ab.data) == string(bb.data) } + // A structural mismatch where the right operand defines its own #== runs MRI's + // reflexive rule: a built-in that fails to match on the left (e.g. 1 == obj) + // dispatches `other == self`, so a user #== that matches a number still wins. + if vm != nil { + if _, isObj := b.(*RObject); isObj { + return a == b || vm.send(b, "==", []object.Value{a}, nil).Truthy() + } + if hasCustomEq(vm, b) { + return a == b || vm.send(b, "==", []object.Value{a}, nil).Truthy() + } + } // Reference types not handled above (classes, procs, …) compare by identity, // which is Ruby's default Object#==. return a == b @@ -973,10 +1137,42 @@ func valueEqualRec(a, b object.Value, seen map[eqPair]struct{}) bool { // their members with eql? too. A built-in value subclass instance is compared as // the value it wraps; everything else falls back to object identity. func valueEql(a, b object.Value) bool { - return valueEqlRec(a, b, nil) + return valueEqlRec(nil, a, b, nil) +} + +// vmValueEql is the VM-aware form of valueEql: Array/Hash members whose own #eql? +// matters (a user object) have it dispatched, matching MRI's rb_eql per member, +// so e.g. Hash#eql? compares values with each value's #eql?. A nil VM keeps the +// structural-only comparison. +func (vm *VM) vmValueEql(a, b object.Value) bool { + return valueEqlRec(vm, a, b, nil) } -func valueEqlRec(a, b object.Value, seen map[eqPair]struct{}) bool { +// rubyEql backs Object#eql?: an instance of a built-in subclass compares as its +// wrapped value, a plain user object by identity (dispatching #eql? here would +// recurse), and every other value structurally with member #eql? dispatched. +func (vm *VM) rubyEql(a, b object.Value) bool { + if ao, ok := a.(*RObject); ok { + if !object.IsNil(ao.builtin) { + return vm.vmValueEql(ao.builtin, b) + } + bo, ok := b.(*RObject) + return ok && ao == bo + } + return vm.vmValueEql(a, b) +} + +// rbEqlElem compares two container members the way MRI's rb_eql does: the +// #equal? identity short-circuit first, then the value comparison (dispatching a +// member's own #eql? when the VM is live). +func (vm *VM) rbEqlElem(a, b object.Value, seen map[eqPair]struct{}) bool { + if vm != nil && identityEqual(a, b) { + return true + } + return valueEqlRec(vm, a, b, seen) +} + +func valueEqlRec(vm *VM, a, b object.Value, seen map[eqPair]struct{}) bool { if o, ok := a.(*RObject); ok && !object.IsNil(o.builtin) { a = o.builtin } @@ -1014,7 +1210,7 @@ func valueEqlRec(a, b object.Value, seen map[eqPair]struct{}) bool { seen[key] = struct{}{} defer delete(seen, key) for i := range av.Elems { - if !valueEqlRec(av.Elems[i], bv.Elems[i], seen) { + if !vm.rbEqlElem(av.Elems[i], bv.Elems[i], seen) { return false } } @@ -1036,12 +1232,21 @@ func valueEqlRec(a, b object.Value, seen map[eqPair]struct{}) bool { for _, k := range av.Keys { v1, _ := av.Get(k) v2, present := bv.Get(k) - if !present || !valueEqlRec(v1, v2, seen) { + if !present || !vm.rbEqlElem(v1, v2, seen) { return false } } return hashIdentityMatch(av, bv) } + // A user object whose class defines #eql? dispatches it — MRI's rb_eql, used per + // member by Array#eql? / Hash#eql?. Unlike ==, rb_eql has no reflexive rule, so + // only the receiver's #eql? is consulted. Guarded so VM-less callers keep plain + // identity. + if vm != nil { + if _, isObj := a.(*RObject); isObj { + return a == b || vm.send(a, "eql?", []object.Value{b}, nil).Truthy() + } + } return a == b // identity for nil/true/false and other reference types } diff --git a/internal/vm/array_edges.go b/internal/vm/array_edges.go index 1e665dfc..c4457de0 100644 --- a/internal/vm/array_edges.go +++ b/internal/vm/array_edges.go @@ -42,11 +42,11 @@ func (vm *VM) registerArrayEdges() { // difference keeps every element of the receiver that is eql? to no element of // any argument array — duplicates in the receiver are preserved, exactly like a // chained `-`. - vm.cArray.define("difference", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("difference", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { others := arrArgs(args) var out []object.Value for _, e := range self.(*object.Array).Elems { - if !inAnyArray(others, e) { + if !vm.inAnyArray(others, e) { out = append(out, e) } } @@ -54,12 +54,12 @@ func (vm *VM) registerArrayEdges() { }) // union concatenates the receiver and every argument, then removes eql? // duplicates keeping first-seen order — a multi-argument, de-duplicating `|`. - vm.cArray.define("union", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("union", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { others := arrArgs(args) var out []object.Value add := func(elems []object.Value) { for _, e := range elems { - if !arrayIncludes(out, e) { + if !vm.arrayIncludesEql(out, e) { out = append(out, e) } } @@ -73,14 +73,14 @@ func (vm *VM) registerArrayEdges() { // intersection keeps each element of the receiver that is eql? to some element // of every argument array, de-duplicated in receiver order. With no arguments // it is simply the receiver de-duplicated (like #uniq under eql?). - vm.cArray.define("intersection", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("intersection", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { others := arrArgs(args) var out []object.Value for _, e := range self.(*object.Array).Elems { - if arrayIncludes(out, e) { + if vm.arrayIncludesEql(out, e) { continue // already emitted → keep the result de-duplicated } - if inAllArrays(others, e) { + if vm.inAllArrays(others, e) { out = append(out, e) } } @@ -88,10 +88,10 @@ func (vm *VM) registerArrayEdges() { }) // intersect? reports whether the receiver and the one argument array share any // element (compared with eql?), short-circuiting on the first match. - vm.cArray.define("intersect?", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("intersect?", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { other := arrArg(args[0]) for _, e := range self.(*object.Array).Elems { - if arrayIncludes(other.Elems, e) { + if vm.arrayIncludesEql(other.Elems, e) { return object.True } } @@ -214,9 +214,9 @@ func arrArgs(args []object.Value) []*object.Array { } // inAnyArray reports whether v is eql? to an element of any of the arrays. -func inAnyArray(arrs []*object.Array, v object.Value) bool { +func (vm *VM) inAnyArray(arrs []*object.Array, v object.Value) bool { for _, a := range arrs { - if arrayIncludes(a.Elems, v) { + if vm.arrayIncludesEql(a.Elems, v) { return true } } @@ -225,9 +225,9 @@ func inAnyArray(arrs []*object.Array, v object.Value) bool { // inAllArrays reports whether v is eql? to an element of every array (vacuously // true when there are none). -func inAllArrays(arrs []*object.Array, v object.Value) bool { +func (vm *VM) inAllArrays(arrs []*object.Array, v object.Value) bool { for _, a := range arrs { - if !arrayIncludes(a.Elems, v) { + if !vm.arrayIncludesEql(a.Elems, v) { return false } } diff --git a/internal/vm/array_equality_test.go b/internal/vm/array_equality_test.go new file mode 100644 index 00000000..95f84361 --- /dev/null +++ b/internal/vm/array_equality_test.go @@ -0,0 +1,158 @@ +package vm_test + +import "testing" + +// TestArrayHashValueEquality covers MRI's rb_equal-per-element rule: Array#== and +// Hash#== (and the membership/search methods that use ==) compare their members by +// dispatching each member's own Ruby #==, not by Go identity. A member with a +// redefined #== therefore participates in the comparison. Asserted against MRI +// Ruby 4.0. +func TestArrayHashValueEquality(t *testing.T) { + // A class whose #== always answers true: every wrapping of it compares equal. + always := "class C; def ==(o); true; end; end\n" + // A class whose #== matches only the Integer 42 (used to exercise both operand + // orders and the reflexive Integer#== path). + only42 := "class D; def ==(o); o == 42; end; end\n" + // A class whose #== matches any Integer (used for membership/subset). + anyInt := "class E; def ==(o); o.is_a?(Integer); end; end\n" + + cases := []struct{ src, want string }{ + // Array#== dispatches each element's #== (value equality), so distinct + // instances with a matching #== are equal. + {always + "a = C.new; p [[a] == [a], [a] == [C.new], [a] == [1]]", "[true, true, true]\n"}, + // A length mismatch short-circuits before any element #==. + {always + "a = C.new; p([a] == [a, a])", "false\n"}, + // Nested arrays recurse, still dispatching the leaf #==. + {always + "a = C.new; p([[a], [a]] == [[C.new], [C.new]])", "true\n"}, + + // Hash#== compares values with #==; keys must match (by eql?/hash). + {always + "a = C.new; p [{x: a} == {x: C.new}, {x: a} == {y: a}, {x: a} == {x: a, z: a}]", "[true, false, false]\n"}, + + // Membership and search use == with the element as the receiver. + {always + "a = C.new; p [[a].include?(C.new), [a].index(C.new), [a].rindex(C.new), [a, a].count(C.new)]", "[true, 0, 0, 2]\n"}, + {always + "a = C.new; p({k: a}.assoc(:k) == [:k, a])", "true\n"}, + {always + "a = C.new; p [{1 => a}.key(C.new) == 1, {x: a}.value?(C.new), {x: a}.has_value?(C.new)]", "[true, true, true]\n"}, + + // The reflexive Integer#== rule: 42 == D.new dispatches D.new == 42, so an + // element order of [42] == [D.new] still holds — in both operand orders. + {only42 + "p [[42] == [D.new], [D.new] == [42], [41] == [D.new]]", "[true, true, false]\n"}, + + // Hash#<= (subset) compares the shared values with #==. + {anyInt + "p [({a: E.new} <= {a: 7}), ({a: E.new} < {a: 7, b: 8})]", "[true, true]\n"}, + // Membership through a custom #==. + {anyInt + "p [[E.new].include?(9), {x: E.new}.value?(9)]", "[true, true]\n"}, + + // A self-referential array compares equal to itself without looping + // (MRI's recursive-comparison rule), and two mutually recursive arrays too. + {"a = []; a << a; p(a == a)", "true\n"}, + {"a = []; a << a; b = []; b << b; p(a == b)", "true\n"}, + + // Plain value elements keep their ordinary numeric ==: 1 == 1.0 in an array. + {"p [[1] == [1.0], [1, 2] == [1, 2], [1] == [2]]", "[true, true, false]\n"}, + + // A built-in with a custom #== as an element (Set) is compared by value. + {"require 'set'; p [[Set[1, 2]] == [Set[2, 1]], [Set[1]] == [Set[2]]]", "[true, false]\n"}, + + // An Array subclass is still an Array: == compares structurally, in both + // operand orders and through an explicit method send. + {"class ML < Array; end; p [ML.new([1, 2]) == [1, 2], [1, 2] == ML.new([1, 2]), ML.new([1, 2]).send(:==, [1, 2])]", "[true, true, true]\n"}, + + // [NaN] == [NaN] is true because Array#== checks #equal? per element first, + // while a bare NaN == NaN stays false. + {"p [[Float::NAN] == [Float::NAN], Float::NAN == Float::NAN]", "[true, false]\n"}, + + // A built-in-with-custom-== member on the RIGHT still drives element equality + // (the reflexive fall-through): 1 == Set[1] is false, so the arrays differ. + {"require 'set'; p([1] == [Set[1]])", "false\n"}, + + // Array#== vs a non-Array: a plain value, a user object without #to_ary, and + // one with #to_ary (which dispatches other == self). + {"p([1] == 1)", "false\n"}, + {"p([1] == Object.new)", "false\n"}, + {"class AL; def respond_to?(m, *); m == :to_ary; end; def ==(o); o == [1]; end; end; p([1] == AL.new)", "true\n"}, + + // Hash#== treats a Hash subclass structurally (both orders), and a non-Hash + // answering #to_hash dispatches other == self; a plain non-Hash is not equal. + {"class MH < Hash; end; h = MH.new; h[:a] = 1; p [h == {a: 1}, {a: 1} == h]", "[true, true]\n"}, + {"p({a: 1} == 5)", "false\n"}, + {"class HL; def respond_to?(m, *); m == :to_hash; end; def ==(o); true; end; end; p({a: 1} == HL.new)", "true\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } +} + +// TestArrayHashSetOpsAndEql covers the eql?/hash-based operations (uniq, &, |, -, +// difference, union, intersection) dispatching a member's own #eql?/#hash, and +// Hash#eql? / Array#eql? dispatching member #eql?. Asserted against MRI Ruby 4.0. +func TestArrayHashSetOpsAndEql(t *testing.T) { + // A class with value-based #eql?/#hash keyed on @n. + k := "class K; attr_reader :n; def initialize(n); @n = n; end; " + + "def eql?(o); o.is_a?(K) && o.n == @n; end; def hash; @n; end; end\n" + + cases := []struct{ src, want string }{ + // uniq groups by #hash then #eql?. + {k + "p [K.new(1), K.new(1), K.new(2)].uniq.size", "2\n"}, + // & / | / - dispatch #eql?/#hash. + {k + "p ([K.new(1), K.new(2)] & [K.new(2)]).size", "1\n"}, + {k + "p ([K.new(1)] | [K.new(1), K.new(2)]).size", "2\n"}, + {k + "p ([K.new(1), K.new(2)] - [K.new(1)]).size", "1\n"}, + {k + "p [K.new(1), K.new(2)].difference([K.new(2)]).size", "1\n"}, + {k + "p [K.new(1)].union([K.new(1)], [K.new(3)]).size", "2\n"}, + {k + "p [K.new(1), K.new(2)].intersection([K.new(2)], [K.new(2)]).size", "1\n"}, + // A #hash mismatch keeps members distinct even when #eql? would say equal. + {"class H; def eql?(o); true; end; def hash; object_id; end; end; p [H.new, H.new].uniq.size", "2\n"}, + // A Bignum #hash is folded to a bucket like any other. + {"class B; def hash; 10**40; end; def eql?(o); o.is_a?(B); end; end; p [B.new, B.new].uniq.size", "1\n"}, + // A non-Integer #hash raises TypeError, as in MRI. + {"class N; def hash; \"x\"; end; def eql?(o); true; end; end; " + + "p(([N.new, N.new].uniq rescue $!.class))", "TypeError\n"}, + + // Value-type set operations keep eql? (not ==) semantics: 1 and 1.0 differ. + {"p [[1, 1.0, 1].uniq, [1] | [1.0], [1, 1.0] - [1]]", "[[1, 1.0], [1, 1.0], [1.0]]\n"}, + + // Hash#eql? and Array#eql? dispatch each member's #eql?. + {k + "p({1 => K.new(5)}.eql?({1 => K.new(5)}))", "true\n"}, + {k + "p [K.new(3)].eql?([K.new(3)])", "true\n"}, + // An Array-subclass member in an eql? comparison is unwrapped to its array. + {"class MA < Array; end; p([MA.new([1])].eql?([[1]]))", "true\n"}, + // Two mutually recursive hashes are eql? (the recursive-pair terminates). + {"h1 = {}; h2 = {}; h1[:x] = h1; h2[:x] = h2; p(h1.eql?(h2))", "true\n"}, + + // A Set member (built-in value equality) inside a Struct compares by value + // through the VM-less Struct#== path — equal, unequal, and size-mismatched. + {"require 'set'; S = Struct.new(:a); p [S.new(Set[1]) == S.new(Set[1]), S.new(Set[1]) == S.new(Set[2]), S.new(Set[1]) == S.new(Set[1, 2])]", "[true, false, false]\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } +} + +// TestArrayComparison covers Array#<=> : returning an element's raw non-zero <=> +// result, length ordering, #to_ary coercion of the argument, and subclass +// handling. Asserted against MRI Ruby 4.0. +func TestArrayComparison(t *testing.T) { + cases := []struct{ src, want string }{ + {"p([1, 2, 3] <=> [1, 2, 3])", "0\n"}, + {"p([1, 2] <=> [1, 2, 3])", "-1\n"}, + {"p([1, 2, 3] <=> [1, 2])", "1\n"}, + {"p([1, 2, 3] <=> [1, 5, 3])", "-1\n"}, + // The element's raw non-zero <=> result propagates (here a nil → nil). + {"class C; def <=>(o); nil; end; end; p([C.new] <=> [C.new])", "nil\n"}, + // A non-Array argument answering #to_ary is converted. + {"class TA; def to_ary; [1, 2, 3]; end; end; p([1, 2, 3] <=> TA.new)", "0\n"}, + // An Array subclass argument is compared structurally (no #to_ary call). + {"class ML < Array; end; p([5, 6, 7] <=> ML.new([5, 6, 7]))", "0\n"}, + // A non-array-like argument is incomparable. + {"p([] <=> false)", "nil\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } +} diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 7cc6839d..c92ba398 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -274,10 +274,11 @@ func (vm *VM) bootstrap() { // value types by value (Go interface equality gives exactly this). return object.Bool(self == args[0]) }) - vm.cObject.define("eql?", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - // Like ==, but with no numeric coercion (see valueEql); the value types - // reach this through Object since none override it. - return object.Bool(valueEql(self, args[0])) + vm.cObject.define("eql?", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + // Like ==, but with no numeric coercion (see valueEql); Array/Hash members + // have their own #eql? dispatched. The value types reach this through Object + // since none override it. + return object.Bool(vm.rubyEql(self, args[0])) }) objectIDFn := func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return vm.objectID(self) @@ -1075,8 +1076,8 @@ func (vm *VM) bootstrap() { vm.cObject.define("yield_self", thenFn) // Default equality: object identity for instances, structural for value // types (Comparable#== and user-defined == override this via dispatch). - vm.cBasicObject.define("==", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return object.Bool(rubyEqual(self, args[0])) + vm.cBasicObject.define("==", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + return object.Bool(vm.rubyEqual(self, args[0])) }) // Default <=>: 0 when the two are the same object, nil otherwise — the MRI // Object#<=>. It compares by identity (like #equal?) rather than sending #==, @@ -2491,7 +2492,7 @@ func (vm *VM) bootstrap() { found := false var out []object.Value for _, e := range a.Elems { - if valueEqual(e, args[0]) { + if vm.vmValueEqual(e, args[0]) { found = true } else { out = append(out, e) @@ -2572,9 +2573,9 @@ func (vm *VM) bootstrap() { } return a }) - arrayInclude := func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + arrayInclude := func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { for _, e := range self.(*object.Array).Elems { - if valueEqual(e, args[0]) { + if vm.vmValueEqual(e, args[0]) { return object.True } } @@ -2849,23 +2850,23 @@ func (vm *VM) bootstrap() { }) // Set intersection (&) and union (|): both deduplicate, keeping first-seen // order, matching Ruby. - vm.cArray.define("&", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("&", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) b := arrArg(args[0]) var out []object.Value for _, e := range a.Elems { - if arrayIncludes(b.Elems, e) && !arrayIncludes(out, e) { + if vm.arrayIncludesEql(b.Elems, e) && !vm.arrayIncludesEql(out, e) { out = append(out, e) } } return object.NewArrayFromSlice(out) }) - vm.cArray.define("|", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("|", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) b := arrArg(args[0]) var out []object.Value for _, e := range append(append([]object.Value{}, a.Elems...), b.Elems...) { - if !arrayIncludes(out, e) { + if !vm.arrayIncludesEql(out, e) { out = append(out, e) } } @@ -3334,9 +3335,9 @@ func (vm *VM) bootstrap() { } return vm.arrayJoin(a, sep, map[*object.Array]bool{}) }) - vm.cArray.define("index", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("index", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { for i, e := range self.(*object.Array).Elems { - if valueEqual(e, args[0]) { + if vm.vmValueEqual(e, args[0]) { return object.IntValue(int64(i)) } } @@ -3354,7 +3355,7 @@ func (vm *VM) bootstrap() { for i := len(a.Elems) - 1; i >= 0; i-- { var match bool if len(args) > 0 { - match = valueEqual(a.Elems[i], args[0]) + match = vm.vmValueEqual(a.Elems[i], args[0]) } else { match = vm.callBlock(blk, []object.Value{a.Elems[i]}).Truthy() } @@ -3407,7 +3408,18 @@ func (vm *VM) bootstrap() { sa := self.(*object.Array) b, ok := args[0].(*object.Array) if !ok { - return object.NilV + // An Array subclass is already array-like (unwrap it, never calling + // #to_ary); any other object is converted via #to_ary, and a value that + // does not convert makes the arrays incomparable (nil). + if o, isObj := args[0].(*RObject); isObj { + b, ok = o.builtin.(*object.Array) + } + if !ok && vm.respondsToDynamic(args[0], "to_ary") { + b, ok = asArray(vm.send(args[0], "to_ary", nil, nil)) + } + if !ok { + return object.NilV + } } // Comparing a pair already being compared means the two descend into // each other, and MRI calls that equal at this point rather than going @@ -3431,11 +3443,11 @@ func (vm *VM) bootstrap() { n = len(be) } for i := 0; i < n; i++ { - c, ok := vm.send(a[i], "<=>", []object.Value{be[i]}, nil).(object.Integer) - if !ok { - return object.NilV // an incomparable pair makes the arrays incomparable - } - if c != 0 { + // MRI returns the element's raw <=> result the moment it is not the + // Integer 0 — so a -1/+1, a nil (incomparable), or any other object all + // propagate unchanged; only an exact 0 continues to the next pair. + c := vm.send(a[i], "<=>", []object.Value{be[i]}, nil) + if iv, isInt := c.(object.Integer); !isInt || iv != 0 { return c } } @@ -4059,11 +4071,11 @@ func (vm *VM) bootstrap() { h.Rehash() return h }) - hashHasValue := func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + hashHasValue := func(vm *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]) { + if vm.vmValueEqual(v, args[0]) { return object.True } } @@ -4072,11 +4084,11 @@ 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 { + vm.cHash.define("key", func(vm *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]) { + if vm.vmValueEqual(v, args[0]) { return k } } @@ -4172,21 +4184,21 @@ func (vm *VM) bootstrap() { // assoc/rassoc scan the pairs in insertion order and return the first // [key, value] whose key (assoc) or value (rassoc) is Ruby-== to the // argument, or nil when none matches. They never consult the default. - vm.cHash.define("assoc", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cHash.define("assoc", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { h := self.(*object.Hash) for _, k := range h.Keys { - if valueEqual(k, args[0]) { + if vm.vmValueEqual(k, args[0]) { v, _ := h.Get(k) return hashPair(k, v) } } return object.NilV }) - vm.cHash.define("rassoc", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cHash.define("rassoc", func(vm *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]) { + if vm.vmValueEqual(v, args[0]) { return hashPair(k, v) } } @@ -4198,23 +4210,23 @@ func (vm *VM) bootstrap() { // one without #to_hash raises TypeError "no implicit conversion of X into // Hash" (matching MRI's rb_check_hash_type + Check_Type). vm.cHash.define("<=", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return object.Bool(hashSubset(self.(*object.Hash), vm.toHash(args[0]))) + return object.Bool(vm.hashSubset(self.(*object.Hash), vm.toHash(args[0]))) }) // Hash#< is proper-subset: <= and strictly smaller (fewer pairs). Because a // subset with equal size is an equal hash, the size test alone distinguishes // proper from improper containment. vm.cHash.define("<", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { h, other := self.(*object.Hash), vm.toHash(args[0]) - return object.Bool(h.Len() < other.Len() && hashSubset(h, other)) + return object.Bool(h.Len() < other.Len() && vm.hashSubset(h, other)) }) // Hash#>= is superset-by-pair: every pair of other is present in the receiver. vm.cHash.define(">=", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return object.Bool(hashSubset(vm.toHash(args[0]), self.(*object.Hash))) + return object.Bool(vm.hashSubset(vm.toHash(args[0]), self.(*object.Hash))) }) // Hash#> is proper-superset: >= and strictly larger. vm.cHash.define(">", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { h, other := self.(*object.Hash), vm.toHash(args[0]) - return object.Bool(h.Len() > other.Len() && hashSubset(other, h)) + return object.Bool(h.Len() > other.Len() && vm.hashSubset(other, h)) }) // Hash#to_proc returns a unary lambda that looks a key up in the hash: // pr.call(k) == h[k]. Lookup goes through #[], so a default value or @@ -4462,7 +4474,7 @@ func (vm *VM) bootstrap() { if vm.callBlock(blk, []object.Value{e}).Truthy() { n++ } - } else if valueEqual(e, args[0]) { + } else if vm.vmValueEqual(e, args[0]) { n++ } } @@ -6511,14 +6523,14 @@ func hashPair(k, v object.Value) *object.Array { // hashSubset reports whether every (key, value) pair of sub is present in sup, // with values compared by valueEqual (MRI's rb_equal). It backs Hash#<= / #< / // #>= / #>: a hash is a subset of another when all its pairs are contained. -func hashSubset(sub, sup *object.Hash) bool { +func (vm *VM) hashSubset(sub, sup *object.Hash) bool { if sub.Len() > sup.Len() { return false } for _, k := range sub.Keys { sv, _ := sub.Get(k) pv, present := sup.Get(k) - if !present || !valueEqual(sv, pv) { + if !present || !vm.vmValueEqual(sv, pv) { return false } } @@ -8293,12 +8305,23 @@ func (vm *VM) integerShiftBig(a, m *big.Int) object.Value { // rubyEqual is the default Object#== : pointer identity for instances, and // structural equality for the immutable value types. -func rubyEqual(a, b object.Value) bool { +func (vm *VM) rubyEqual(a, b object.Value) bool { if ao, ok := a.(*RObject); ok { + // An instance of a built-in subclass (Array/Hash/String/…) that adds no + // #== of its own inherits the built-in's value equality, so compare as the + // wrapped value — e.g. MyArray[1, 2, 3] == [1, 2, 3]. + if !object.IsNil(ao.builtin) { + return vm.vmValueEqual(ao.builtin, b) + } + // This is BasicObject#== itself (the default), reached only when the + // receiver defined no closer #==, so a plain user object compares by + // identity — dispatching #== here would recurse back into this method. bo, ok := b.(*RObject) return ok && ao == bo } - return valueEqual(a, b) + // A built-in container receiver compares structurally, with each element's own + // Ruby #== dispatched (MRI's rb_equal per element). + return vm.vmValueEqual(a, b) } // spaceshipNumeric implements Integer#<=> and Float#<=>: -1/0/1 across the diff --git a/internal/vm/prelude_test.go b/internal/vm/prelude_test.go index d9c1fea8..c5ac7e73 100644 --- a/internal/vm/prelude_test.go +++ b/internal/vm/prelude_test.go @@ -68,10 +68,11 @@ func TestExceptionObjectFallback(t *testing.T) { } func TestRubyEqualValueTypes(t *testing.T) { - if !rubyEqual(object.Integer(5), object.Integer(5)) { + vm := bareVM() + if !vm.rubyEqual(object.Integer(5), object.Integer(5)) { t.Error("5 == 5 should be true") } - if rubyEqual(object.Integer(5), object.NewString("x")) { + if vm.rubyEqual(object.Integer(5), object.NewString("x")) { t.Error("5 == \"x\" should be false") } } diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 631bec08..62c07c2e 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -618,7 +618,11 @@ func (vm *VM) installHashKeyHook() { raise("TypeError", "no implicit conversion of %s into Integer", vm.classOf(hv).name) } eql := func(stored object.Value) bool { - return vm.send(k, "eql?", []object.Value{stored}, nil).Truthy() + // A key is #eql? to itself; MRI's table never re-sends #eql? for the + // identical object (this is what lets a self-comparison — e.g. Hash#== + // fetching its own key's value — avoid a spurious, and in mspec + // forbidden, #eql? call, and matches a non-reflexive #eql? too). + return stored == k || vm.send(k, "eql?", []object.Value{stored}, nil).Truthy() } return rh, eql, true }