From eda2fb44c6a5e0fb7fd3b2869507f9f1d0d2d807 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 6 Sep 2026 11:27:55 +0200 Subject: [PATCH] Dispatch a user-defined #!= instead of inverting #== in the != opcode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rbgo compiled `a != b` to OpNeq, which binaryOp evaluated as !(a == b) — so a class that overrides #!= (as MRI's BasicObject#!= documents callers may) was ignored: `C.new != x` returned !(C#==) rather than C#!=. This surfaced across core/array, core/range and core/bsearch specs and the mspec ShouldProxy harness. OpNeq now dispatches the receiver's #!= when its class overrides it anywhere below BasicObject (whose default #!= is still the inline !(self == other) fast path); built-in value types, which never override #!=, are unaffected. Co-Authored-By: Claude Opus 4.8 --- internal/vm/arith.go | 19 +++++++++++++++++++ internal/vm/neq_dispatch_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 internal/vm/neq_dispatch_test.go diff --git a/internal/vm/arith.go b/internal/vm/arith.go index b0cafae6..fb79ad2d 100644 --- a/internal/vm/arith.go +++ b/internal/vm/arith.go @@ -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 @@ -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 diff --git a/internal/vm/neq_dispatch_test.go b/internal/vm/neq_dispatch_test.go new file mode 100644 index 00000000..a08eb243 --- /dev/null +++ b/internal/vm/neq_dispatch_test.go @@ -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) + } + } +}