Commit 6afe7898
6afe78989cb0Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Array#each_index: handle all array kinds including empty []
Move each_index handler before if(k) guard so TY_POLY_ARRAY is covered; add no-op path for empty array literal (TY_UNKNOWN receiver) that was blocking bundle_tiny_array.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 6afe78989cb0
- Parent
- 5dbb0a3fcdea
src/codegen.cmodifiedView file
@@ -5465,6 +5465,12 @@
54655465
return;54665466
}54675467
}5468+
/* each_index on empty array literal [] (TY_UNKNOWN receiver): no-op loop */5469+
if (recv >= 0 && rt == TY_UNKNOWN && !strcmp(name, "each_index") &&5470+
nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ArrayNode")) {5471+
int en = 0; nt_arr(nt, recv, "elements", &en);5472+
if (en == 0) { emit_expr(c, recv, b); return; }5473+
}54685474
if (recv >= 0 && ty_is_array(rt)) {54695475
if (!strcmp(name, "pack") && argc == 1 && (rt == TY_INT_ARRAY || rt == TY_POLY_ARRAY)) {54705476
buf_printf(b, "sp_%sArray_pack(", rt == TY_POLY_ARRAY ? "Poly" : "Int");@@ -5546,6 +5552,28 @@
55465552
}55475553
return;55485554
}5555+
/* each_index { |i| ... } - iterate with index (works for all array kinds) */5556+
{5557+
int ei_blk = nt_ref(nt, id, "block");5558+
if (!strcmp(name, "each_index") && ei_blk >= 0) {5559+
const char *ek = (rt == TY_POLY_ARRAY) ? "Poly" : array_kind(rt);5560+
if (ek) {5561+
const char *ip = block_param_name(c, ei_blk, 0); if (ip) ip = rename_local(ip);5562+
int body = nt_ref(nt, ei_blk, "body");5563+
int trecv = ++g_tmp, ti = ++g_tmp;5564+
Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);5565+
emit_indent(g_pre, g_indent); emit_ctype(c, rt, g_pre);5566+
buf_printf(g_pre, " _t%d = %s;\n", trecv, rb.p ? rb.p : ""); free(rb.p);5567+
emit_indent(g_pre, g_indent);5568+
buf_printf(g_pre, "for (mrb_int _t%d = 0; _t%d < sp_%sArray_length(_t%d); _t%d++) {\n",5569+
ti, ti, ek, trecv, ti);5570+
if (ip) { emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "lv_%s = _t%d;\n", ip, ti); }5571+
emit_stmts(c, body, g_pre, g_indent + 1);5572+
emit_indent(g_pre, g_indent); buf_puts(g_pre, "}\n");5573+
buf_printf(b, "_t%d", trecv); return;5574+
}5575+
}5576+
}55495577
if (k) {55505578
if ((!strcmp(name, "to_a") || !strcmp(name, "to_ary") || !strcmp(name, "entries") ||55515579
!strcmp(name, "flatten") || !strcmp(name, "compact")) && argc == 0) {@@ -5822,23 +5850,6 @@
58225850
if (flv) flv->type = fsaved;58235851
buf_printf(b, "_t%d", trecv); return;58245852
}5825-
}5826-
/* each_index { |i| ... } - iterate with index */5827-
if (!strcmp(name, "each_index") && block >= 0) {5828-
/* statement-mode: just emit the loop and return the receiver */5829-
const char *ip = block_param_name(c, block, 0); if (ip) ip = rename_local(ip);5830-
int body = nt_ref(nt, block, "body");5831-
int trecv = ++g_tmp, ti = ++g_tmp;5832-
Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);5833-
emit_indent(g_pre, g_indent); emit_ctype(c, rt, g_pre);5834-
buf_printf(g_pre, " _t%d = %s;\n", trecv, rb.p ? rb.p : ""); free(rb.p);5835-
emit_indent(g_pre, g_indent);5836-
buf_printf(g_pre, "for (mrb_int _t%d = 0; _t%d < sp_%sArray_length(_t%d); _t%d++) {\n",5837-
ti, ti, k, trecv, ti);5838-
if (ip) { emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "lv_%s = _t%d;\n", ip, ti); }5839-
emit_stmts(c, body, g_pre, g_indent + 1);5840-
emit_indent(g_pre, g_indent); buf_puts(g_pre, "}\n");5841-
buf_printf(b, "_t%d", trecv); return;58425853
}58435854
if ((!strcmp(name, "all?") || !strcmp(name, "any?") ||58445855
!strcmp(name, "none?") || !strcmp(name, "one?")) &&