Commit 91464057

91464057619fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Equality: cross-type == is always false (no coercion)

Comparing two different concrete types (e.g. "3" == 3) now yields false (== ) / true (!= ) with both operands still evaluated for side effects, instead of emitting sp_str_eq(str, int) and failing to compile. Reworked the ==/!= dispatch around an eq_family() classifier: same family compares by value (int/float share a family), a poly operand compares dynamically (fixing string-vs-poly, which previously mis-emitted sp_str_eq), and mismatched concrete families are never equal.

make test: 172 pass / 5 fail / 667 error (was 171 / 5 / 668); eq_string_vs_int green, no regressions.

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

Details

Commit
91464057619f
Parent
1b86a411fe89

src/codegen.cmodifiedView file

@@ -318,6 +318,18 @@
318318 return -1;
319319 }
320320
321+/* Value-equality family: operands in the same nonzero family compare by value;
322+ different nonzero families are never == (Ruby does no cross-type coercion,
323+ except int/float which share family 1). 0 = not a simple comparable type. */
324+static int eq_family(TyKind t) {
325+ if (ty_is_numeric(t)) return 1;
326+ if (t == TY_STRING) return 2;
327+ if (t == TY_BOOL) return 3;
328+ if (t == TY_SYMBOL) return 4;
329+ if (t == TY_RANGE) return 5;
330+ return 0;
331+}
332+
321333 static void emit_method_call(Compiler *c, int id, Buf *b) {
322334 const NodeTable *nt = c->nt;
323335 const char *name = nt_str(nt, id, "name");
@@ -1017,39 +1029,36 @@
10171029 else { buf_puts(b, "(("); emit_expr(c, other, b); buf_printf(b, "), %d)", eq ? 0 : 1); }
10181030 return;
10191031 }
1020- if (rt == TY_STRING || a0 == TY_STRING) {
1021- buf_puts(b, eq ? "sp_str_eq(" : "(!sp_str_eq(");
1022- emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b);
1023- buf_puts(b, eq ? ")" : "))");
1024- return;
1025- }
1026- if (ty_is_numeric(rt) || rt == TY_BOOL || rt == TY_SYMBOL) {
1027- buf_puts(b, "(");
1028- emit_expr(c, recv, b);
1029- buf_printf(b, " %s ", eq ? "==" : "!=");
1030- emit_expr(c, argv[0], b);
1031- buf_puts(b, ")");
1032- return;
1033- }
10341032 if (rt == TY_POLY_ARRAY && a0 == TY_POLY_ARRAY) {
10351033 buf_puts(b, eq ? "sp_PolyArray_eq(" : "(!sp_PolyArray_eq(");
10361034 emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b);
10371035 buf_puts(b, eq ? ")" : "))");
10381036 return;
10391037 }
1040- if (rt == TY_RANGE && a0 == TY_RANGE) {
1041- buf_puts(b, eq ? "sp_range_eq(" : "(!sp_range_eq(");
1042- emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b);
1043- buf_puts(b, eq ? ")" : "))");
1044- return;
1045- }
1046- if (rt == TY_RANGE || a0 == TY_RANGE) { buf_puts(b, eq ? "0" : "1"); return; } /* range vs non-range */
1038+ /* a poly operand compares dynamically (covers string-vs-poly etc.) */
10471039 if (rt == TY_POLY || a0 == TY_POLY) {
10481040 buf_puts(b, eq ? "sp_poly_eq(" : "(!sp_poly_eq(");
10491041 emit_boxed(c, recv, b); buf_puts(b, ", "); emit_boxed(c, argv[0], b);
10501042 buf_puts(b, eq ? ")" : "))");
10511043 return;
10521044 }
1045+ {
1046+ int fr = eq_family(rt), fa = eq_family(a0);
1047+ /* same comparable family: compare by value */
1048+ if (fr && fa && fr == fa) {
1049+ if (fr == 2) { buf_puts(b, eq ? "sp_str_eq(" : "(!sp_str_eq("); emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, eq ? ")" : "))"); }
1050+ else if (fr == 5) { buf_puts(b, eq ? "sp_range_eq(" : "(!sp_range_eq("); emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, eq ? ")" : "))"); }
1051+ else { buf_puts(b, "("); emit_expr(c, recv, b); buf_printf(b, " %s ", eq ? "==" : "!="); emit_expr(c, argv[0], b); buf_puts(b, ")"); }
1052+ return;
1053+ }
1054+ /* two different concrete types are never == in Ruby (no coercion);
1055+ still evaluate both operands for their side effects */
1056+ if (fr && fa) {
1057+ buf_puts(b, "(("); emit_expr(c, recv, b); buf_puts(b, "), (");
1058+ emit_expr(c, argv[0], b); buf_printf(b, "), %d)", eq ? 0 : 1);
1059+ return;
1060+ }
1061+ }
10531062 unsupported(c, id, "equality");
10541063 }
10551064