Commit 69492116
69492116bfa0Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Handle x == nil / x != nil for any receiver
Poly -> sp_poly_nil_p, nil literal -> constant, any other concrete (non-nullable) type -> always false/true with the operand evaluated for side effects.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 69492116bfa0
- Parent
- 831ee0e7e53d
src/codegen.cmodifiedView file
@@ -765,6 +765,20 @@
765765
766766
if (argc == 1 && (!strcmp(name, "==") || !strcmp(name, "!="))) {767767
int eq = !strcmp(name, "==");768+
/* `x == nil` / `x != nil` for any receiver */769+
int a_nil = nt_type(nt, argv[0]) && !strcmp(nt_type(nt, argv[0]), "NilNode");770+
int r_nil = nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "NilNode");771+
if (a_nil || r_nil) {772+
int other = a_nil ? recv : argv[0];773+
TyKind ot = comp_ntype(c, other);774+
if (ot == TY_POLY) {775+
buf_puts(b, eq ? "sp_poly_nil_p(" : "(!sp_poly_nil_p(");776+
emit_expr(c, other, b); buf_puts(b, eq ? ")" : "))");777+
}778+
else if (ot == TY_NIL) buf_puts(b, eq ? "1" : "0");779+
else { buf_puts(b, "(("); emit_expr(c, other, b); buf_printf(b, "), %d)", eq ? 0 : 1); }780+
return;781+
}768782
if (rt == TY_STRING || a0 == TY_STRING) {769783
buf_puts(b, eq ? "sp_str_eq(" : "(!sp_str_eq(");770784
emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b);