Commit 1b24399d
1b24399daca9Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
codegen: emit NULL pointer compare for object-vs-nil equality
TY_OBJECT pointers are never NULL (non-nullable), so obj==nil is always false and obj!=nil is always true; emit a simple pointer comparison instead of falling through to unsupported().
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 1b24399daca9
- Parent
- a0d20c23ca8c
src/codegen.cmodifiedView file
@@ -6832,6 +6832,16 @@
68326832
emit_expr(c, argv[0], b); buf_printf(b, "), %d)", eq ? 0 : 1);68336833
return;68346834
}6835+
/* object vs nil: identity/pointer comparison (Object#== fallback).6836+
A non-nullable TY_OBJECT pointer is never NULL, so obj==nil=false6837+
and obj!=nil=true. A nullable object also works correctly via NULL. */6838+
if ((ty_is_object(rt) && a0 == TY_NIL) || (rt == TY_NIL && ty_is_object(a0))) {6839+
int obj_n = ty_is_object(rt) ? recv : argv[0];6840+
buf_puts(b, "(");6841+
emit_expr(c, obj_n, b);6842+
buf_printf(b, " %s NULL)", eq ? "==" : "!=");6843+
return;6844+
}68356845
unsupported(c, id, "equality");68366846
}68376847