Commit a1eb5a78

a1eb5a78f390Merged

Ryan Seys <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

codegen: emit Object#== identity for objects without a custom ==

Comparing two user objects with == or != where the class chain defines neither == nor <=> previously fell through the equality gate and aborted compilation. CRuby's fallback is Object#==, which is object identity: an instance is equal only to itself, and two distinct instances are never equal.

Emit that fallback as a pointer comparison for pointer-backed objects, which reproduces CRuby exactly. Value-type objects are copied by value and have no stable identity, so identity is unrepresentable; rather than silently diverge (structural equality would report equal where CRuby reports not-equal) the gate now rejects them with a message asking for an explicit ==, which already dispatches when defined.

Gates: 951 tests pass; bootstrap IR+C fixpoint byte-identical for analyze and codegen.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
a1eb5a78f390
Parent
913a83318df6

src/codegen_call.cmodifiedView file

@@ -7801,6 +7801,23 @@
78017801 buf_printf(b, " %s NULL)", eq ? "==" : "!=");
78027802 return;
78037803 }
7804+ /* object == object with no user-defined == or <=>: Object#== identity.
7805+ Pointer-backed objects compare by address -- faithful to CRuby, where two
7806+ distinct instances are never == and an instance is == only to itself. A
7807+ value-type object has no stable identity (it is copied by value), so
7808+ identity is unrepresentable; rather than silently diverge (structural
7809+ equality would say true where CRuby says false) we refuse and ask for an
7810+ explicit ==. */
7811+ if (recv >= 0 && ty_is_object(rt) && ty_is_object(a0)) {
7812+ if (comp_ty_value_obj(c, rt) || comp_ty_value_obj(c, a0))
7813+ unsupported(c, id, "equality on a value-type object without a user-defined == (define == for comparison)");
7814+ buf_puts(b, "((void *)(");
7815+ emit_expr(c, recv, b);
7816+ buf_printf(b, ") %s (void *)(", eq ? "==" : "!=");
7817+ emit_expr(c, argv[0], b);
7818+ buf_puts(b, "))");
7819+ return;
7820+ }
78047821 unsupported(c, id, "equality");
78057822 }
78067823