Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions internal/vm/arith.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ func bigOp(op bytecode.Op, a, b *big.Int) object.Value {
// comparisons keep the Phase 0 fast path; everything else dispatches as a
// method so user classes (and the embedded-Ruby Comparable mixin) can define
// `<`, `<=`, `>`, `>=` and `==`.
// hasCustomNeq reports whether a's class overrides `!=` — that is, defines it
// anywhere below BasicObject (whose default `!=` returns !(self == other)). When
// it does, the OpNeq opcode must dispatch that method rather than invert `==`.
func (vm *VM) hasCustomNeq(a object.Value) bool {
for c := vm.classOf(a); c != nil && c != vm.cBasicObject; c = c.super {
if lookupOwnOrIncluded(c, "!=") != nil {
return true
}
}
return false
}

func (vm *VM) binaryOp(op bytecode.Op, a, b object.Value) object.Value {
// An instance of a user subclass of a built-in value type uses that value's
// own operators (so a String-subclass "+", an Array-subclass "*", and the
Expand All @@ -245,6 +257,13 @@ func (vm *VM) binaryOp(op bytecode.Op, a, b object.Value) object.Value {
}
switch op {
case bytecode.OpEq, bytecode.OpNeq:
// `!=` is a real method — BasicObject#!= returns !(self == other) — so the
// inline "invert ==" shortcut is only valid for the default. A receiver
// that OVERRIDES #!= must dispatch its own method (MRI calls it), rather
// than have the opcode compute !(self == other).
if op == bytecode.OpNeq && vm.hasCustomNeq(a) {
return vm.send(a, "!=", []object.Value{b}, nil)
}
// Objects dispatch `==` (so Object identity, a user `==`, or
// Comparable#== all apply); a builtin instance whose class defines its own
// `==` (e.g. Digest::Instance#==, which compares hex digests) dispatches it
Expand Down
31 changes: 31 additions & 0 deletions internal/vm/neq_dispatch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package vm_test

import "testing"

// TestNotEqualDispatch checks that `a != b` calls a user-defined #!= when the
// receiver overrides it (MRI dispatches the method), and otherwise keeps the
// default BasicObject#!= behaviour of !(a == b). Verified against MRI 4.0.6.
func TestNotEqualDispatch(t *testing.T) {
for _, c := range []struct{ src, want string }{
// A class that overrides #!=: the operator dispatches the override, so the
// result is whatever #!= returns — not !(==).
{`class C1; def ==(o); false; end; def !=(o); "custom"; end; end; p (C1.new != 1)`, "\"custom\"\n"},
// #!= override returning nil (falsy) is returned verbatim, not coerced.
{`class C2; def !=(o); nil; end; end; p (C2.new != 1)`, "nil\n"},
// An override inherited from a superclass (below BasicObject) still dispatches.
{`class B3; def !=(o); :sup; end; end; class C3 < B3; end; p (C3.new != 1)`, ":sup\n"},
// #!= override reached through an included module.
{`module M4; def !=(o); :mod; end; end; class C4; include M4; end; p (C4.new != 1)`, ":mod\n"},
// No #!= override: the default BasicObject#!= gives !(self == other).
{`class D1; def ==(o); true; end; end; p (D1.new != D1.new)`, "false\n"},
{`class D2; def ==(o); false; end; end; p (D2.new != D2.new)`, "true\n"},
// Built-in value types (no #!= override) keep structural inequality.
{`p (1 != 2)`, "true\n"}, {`p (1 != 1)`, "false\n"},
{`p ("a" != "b")`, "true\n"}, {`p (nil != nil)`, "false\n"},
{`p ([1, 2] != [1, 3])`, "true\n"}, {`p ([1, 2] != [1, 2])`, "false\n"},
} {
if got := eval(t, c.src); got != c.want {
t.Errorf("%s\n got=%q want=%q", c.src, got, c.want)
}
}
}