Commit a0d20c23
a0d20c23ca8cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
fix: string<<tail, ivar nil-sentinel, and is_a? Encoding regression
- String
<<at tail position in a non-void method now correctly mutates the receiver and returns it (emit_stmt_tail_inner) - Assigning nil to typed ivars now emits the correct nil sentinel: TY_INT -> SP_INT_NIL, TY_FLOAT -> sp_float_nil(), TY_STRING -> NULL Previously all emitted the type's default value (0/0.0/empty-str), breaking ||= nil-guards and .nil? checks
- && short-circuit: box nullable string left-arm with sp_box_nullable_str so a NULL string short-circuits as nil (falsy), not as a truthy SP_TAG_STR value wrapping NULL
- poly.is_a?(Encoding) and similar builtin-class constants now use the tag-check fast-path instead of the dynamic TY_CLASS class-var path, which was trying to evaluate the constant as a runtime value
Refs #spinel-c
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- a0d20c23ca8c
- Parent
- 977a1562b64c
src/codegen.cmodifiedView file
@@ -6217,10 +6217,12 @@
62176217
if (yes >= 0) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_printf(b, "), %d)", yes); return; }62186218
}62196219
6220-
/* poly.is_a?(class_var) where the argument is a TY_CLASS typed expression */6220+
/* poly.is_a?(class_var) where the argument is a TY_CLASS typed expression.6221+
Skip if argv[0] is a ConstantReadNode: the fast-path below handles builtins. */62216222
if (recv >= 0 && rt == TY_POLY && argc == 1 &&62226223
(!strcmp(name, "is_a?") || !strcmp(name, "kind_of?") || !strcmp(name, "instance_of?")) &&6223-
comp_ntype(c, argv[0]) == TY_CLASS) {6224+
comp_ntype(c, argv[0]) == TY_CLASS &&6225+
!(nt_type(nt, argv[0]) && !strcmp(nt_type(nt, argv[0]), "ConstantReadNode"))) {62246226
int t = ++g_tmp, k = ++g_tmp;62256227
buf_printf(b, "({ sp_RbVal _t%d = ", t); emit_expr(c, recv, b); buf_printf(b, "; ");62266228
buf_printf(b, "sp_Class _t%d = ", k); emit_expr(c, argv[0], b); buf_printf(b, "; ");@@ -12566,7 +12568,7 @@
1256612568
Buf _vb; memset(&_vb,0,sizeof _vb); buf_printf(&_vb, "_t%d", t); \1256712569
/* reuse emit_boxed by faking: just box by left type */ \1256812570
if (lt==TY_INT) buf_printf(b, "sp_box_int(_t%d)", t); \12569-
else if (lt==TY_STRING) buf_printf(b, "sp_box_str(_t%d)", t); \12571+
else if (lt==TY_STRING) buf_printf(b, "sp_box_nullable_str(_t%d)", t); \1257012572
else if (lt==TY_FLOAT) buf_printf(b, "sp_box_float(_t%d)", t); \1257112573
else if (lt==TY_BOOL) buf_printf(b, "sp_box_bool(_t%d)", t); \1257212574
else if (lt==TY_SYMBOL) buf_printf(b, "sp_box_sym(_t%d)", t); \@@ -14438,6 +14440,9 @@
1443814440
if (vty && !strcmp(vty, "NilNode")) {1443914441
if (ivt == TY_RANGE) buf_puts(b, "(sp_Range){0}");1444014442
else if (ivt == TY_POLY) buf_puts(b, "sp_box_nil()");14443+
else if (ivt == TY_INT) buf_puts(b, "SP_INT_NIL");14444+
else if (ivt == TY_FLOAT) buf_puts(b, "sp_float_nil()");14445+
else if (ivt == TY_STRING) buf_puts(b, "NULL");1444114446
else buf_puts(b, default_value(ivt));1444214447
}1444314448
else if (v_empty_array && ivt == TY_POLY_ARRAY) buf_puts(b, "sp_PolyArray_new()");@@ -15435,6 +15440,22 @@
1543515440
emit_iteration_stmt(c, id, b, indent))1543615441
return;1543715442
15443+
/* string << at tail position: mutate receiver, then return it */15444+
if (!strcmp(ty, "CallNode")) {15445+
int _srecv = nt_ref(nt, id, "receiver");15446+
const char *_snm = nt_str(nt, id, "name");15447+
if (_srecv >= 0 && _snm && !strcmp(_snm, "<<") &&15448+
comp_ntype(c, _srecv) == TY_STRING &&15449+
emit_array_mutate_stmt(c, id, b, indent)) {15450+
emit_indent(b, indent); emit_tail_lead(b);15451+
int _wp = g_result_var ? g_result_poly : (g_ret_type == TY_POLY);15452+
if (_wp) emit_boxed(c, _srecv, b);15453+
else emit_expr(c, _srecv, b);15454+
buf_puts(b, ";\n");15455+
return;15456+
}15457+
}15458+
1543815459
/* a value expression: return it (or assign to the begin/rescue result) */1543915460
emit_indent(b, indent);1544015461
emit_tail_lead(b);