Commit 5a6a18b8
5a6a18b862aaMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Add map/collect on TY_POLY receiver using sp_poly_arr_len/sp_poly_arr_get
Parameters with nil defaults (e.g. arr = nil) are widened to TY_POLY.
Calling .map { |x| ... } on such a parameter previously failed with
"unsupported call". Now emit_collect_expr handles TY_POLY receivers by
iterating with sp_poly_arr_len / sp_poly_arr_get (runtime helpers that
dispatch over all boxed array variants) and building a statically-typed
result array from the block's inferred return type.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 5a6a18b862aa
- Parent
- 08027fdf9e40
src/codegen.cmodifiedView file
@@ -1603,6 +1603,72 @@
16031603
TyKind rt = comp_ntype(c, recv);16041604
if (ty_is_hash(rt)) return emit_hash_collect_expr(c, id, b);16051605
int range_recv = (rt == TY_RANGE);1606+
if (rt == TY_POLY) {1607+
/* poly-typed receiver (e.g. `arr = nil` default): iterate via1608+
sp_poly_arr_len / sp_poly_arr_get and build a typed result array */1609+
int is_map2 = !strcmp(name, "map") || !strcmp(name, "collect");1610+
if (!is_map2) return 0;1611+
TyKind restype2 = comp_ntype(c, id);1612+
int res_poly2 = (restype2 == TY_POLY_ARRAY);1613+
const char *rk2 = res_poly2 ? "Poly" : array_kind(restype2);1614+
if (!rk2) return 0;1615+
const char *p0p = block_param_name(c, block, 0); if (p0p) p0p = rename_local(p0p);1616+
int body2 = nt_ref(nt, block, "body");1617+
int bn2 = 0;1618+
const int *bb2 = body2 >= 0 ? nt_arr(nt, body2, "body", &bn2) : NULL;1619+
if (bn2 < 1) return 0;1620+
int trecv2 = ++g_tmp, tn2 = ++g_tmp, tres2 = ++g_tmp, ti2 = ++g_tmp;1621+
Buf rb2; memset(&rb2, 0, sizeof rb2); emit_expr(c, recv, &rb2);1622+
emit_indent(g_pre, g_indent);1623+
buf_printf(g_pre, "sp_RbVal _t%d = ", trecv2);1624+
buf_puts(g_pre, rb2.p ? rb2.p : "sp_box_nil()");1625+
buf_puts(g_pre, ";\n");1626+
free(rb2.p);1627+
emit_indent(g_pre, g_indent);1628+
buf_printf(g_pre, "mrb_int _t%d = sp_poly_arr_len(_t%d);\n", tn2, trecv2);1629+
emit_indent(g_pre, g_indent);1630+
buf_printf(g_pre, "sp_%sArray *_t%d = sp_%sArray_new();\n", rk2, tres2, rk2);1631+
emit_indent(g_pre, g_indent);1632+
buf_printf(g_pre, "SP_GC_ROOT(_t%d);\n", tres2);1633+
emit_indent(g_pre, g_indent);1634+
buf_printf(g_pre, "for (mrb_int _t%d = 0; _t%d < _t%d; _t%d++) {\n",1635+
ti2, ti2, tn2, ti2);1636+
/* pin block param to TY_POLY via shadow declaration */1637+
Scope *csc2 = p0p ? comp_scope_of(c, block) : NULL;1638+
LocalVar *clv2 = (csc2 && p0p) ? scope_local(csc2, p0p) : NULL;1639+
TyKind csaved2 = clv2 ? clv2->type : TY_UNKNOWN;1640+
if (clv2) clv2->type = TY_POLY;1641+
for (int j2 = 0; j2 < bn2; j2++) infer_type(c, bb2[j2]);1642+
emit_indent(g_pre, g_indent + 1);1643+
buf_puts(g_pre, "{\n");1644+
emit_indent(g_pre, g_indent + 2);1645+
buf_printf(g_pre, "sp_RbVal lv_%s = sp_poly_arr_get(_t%d, _t%d);\n",1646+
p0p ? p0p : "_dummy", trecv2, ti2);1647+
for (int j2 = 0; j2 < bn2 - 1; j2++) emit_stmt(c, bb2[j2], g_pre, g_indent + 2);1648+
int saveIndent2 = g_indent; g_indent = g_indent + 2;1649+
Buf vb2; memset(&vb2, 0, sizeof vb2); emit_expr(c, bb2[bn2 - 1], &vb2);1650+
g_indent = saveIndent2;1651+
TyKind bty2 = comp_ntype(c, bb2[bn2 - 1]);1652+
emit_indent(g_pre, g_indent + 2);1653+
buf_printf(g_pre, "sp_%sArray_push(_t%d, ", rk2, tres2);1654+
if (res_poly2 && bty2 != TY_POLY) {1655+
Buf bx2; memset(&bx2, 0, sizeof bx2);1656+
emit_boxed_text(c, bty2, vb2.p ? vb2.p : "", &bx2);1657+
buf_puts(g_pre, bx2.p ? bx2.p : ""); free(bx2.p);1658+
}1659+
else {1660+
buf_puts(g_pre, vb2.p ? vb2.p : "");1661+
}1662+
buf_puts(g_pre, ");\n");1663+
free(vb2.p);1664+
emit_indent(g_pre, g_indent + 1);1665+
buf_puts(g_pre, "}\n");1666+
emit_indent(g_pre, g_indent);1667+
buf_puts(g_pre, "}\n");1668+
if (clv2) clv2->type = csaved2;1669+
buf_printf(b, "_t%d", tres2);1670+
return 1;1671+
}16061672
if (!ty_is_array(rt) && !range_recv) return 0;16071673
const char *k = range_recv ? "Int" : (rt == TY_POLY_ARRAY ? "Poly" : array_kind(rt));16081674
if (!k) return 0;