Commit de608f2b
de608f2b23b3Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
codegen: box pattern-match destructure and capture binds into widened locals
Pattern-matching binds locals from array elements (array/find patterns) and from the matched scrutinee (local-target, capture, guard-body). Box the int rvalue into a poly-widened local slot, matching the masgn target paths: the array element get via emit_boxed_text(element type), and the scrutinee temp via emit_boxed_text(scrutinee type). Unblocks array_pattern_match_basic, pattern_match_splat_rest, pattern_match_as_and_guard, rightward_assign_destructure.
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- de608f2b23b3
- Parent
- 38233dfe74c4
src/codegen_stmt.cmodifiedView file
@@ -1119,7 +1119,7 @@
11191119
11201120
if (!strcmp(pty, "LocalVariableTargetNode")) {11211121
const char *lnm = nt_str(nt, pat, "name");1122-
if (lnm) { emit_indent(b, body_indent); buf_printf(b, "lv_%s = _t%d;\n", lnm, t); }1122+
if (lnm) { emit_indent(b, body_indent); buf_printf(b, "lv_%s = ", lnm); LocalVar *plv = scope_local(comp_scope_of(c, id), lnm); if (plv && plv->type == TY_POLY && pt != TY_POLY && pt != TY_UNKNOWN) { char ex[24]; snprintf(ex, sizeof ex, "_t%d", t); Buf bx; memset(&bx, 0, sizeof bx); emit_boxed_text(c, pt, ex, &bx); buf_puts(b, bx.p ? bx.p : "sp_box_nil()"); free(bx.p); } else buf_printf(b, "_t%d", t); buf_puts(b, ";\n"); }11231123
}11241124
else if (!strcmp(pty, "IfNode")) {11251125
guard = nt_ref(nt, pat, "predicate");@@ -1131,7 +1131,7 @@
11311131
const char *bty = nt_type(nt, body[k]);11321132
if (bty && !strcmp(bty, "LocalVariableTargetNode")) {11331133
const char *lnm = nt_str(nt, body[k], "name");1134-
if (lnm) { emit_indent(b, body_indent); buf_printf(b, "lv_%s = _t%d;\n", lnm, t); }1134+
if (lnm) { emit_indent(b, body_indent); buf_printf(b, "lv_%s = ", lnm); LocalVar *plv = scope_local(comp_scope_of(c, id), lnm); if (plv && plv->type == TY_POLY && pt != TY_POLY && pt != TY_UNKNOWN) { char ex[24]; snprintf(ex, sizeof ex, "_t%d", t); Buf bx; memset(&bx, 0, sizeof bx); emit_boxed_text(c, pt, ex, &bx); buf_puts(b, bx.p ? bx.p : "sp_box_nil()"); free(bx.p); } else buf_printf(b, "_t%d", t); buf_puts(b, ";\n"); }11351135
}11361136
}11371137
}@@ -1141,7 +1141,7 @@
11411141
if (tgt >= 0 && nt_type(nt, tgt) &&11421142
!strcmp(nt_type(nt, tgt), "LocalVariableTargetNode")) {11431143
const char *lnm = nt_str(nt, tgt, "name");1144-
if (lnm) { emit_indent(b, body_indent); buf_printf(b, "lv_%s = _t%d;\n", lnm, t); }1144+
if (lnm) { emit_indent(b, body_indent); buf_printf(b, "lv_%s = ", lnm); LocalVar *plv = scope_local(comp_scope_of(c, id), lnm); if (plv && plv->type == TY_POLY && pt != TY_POLY && pt != TY_UNKNOWN) { char ex[24]; snprintf(ex, sizeof ex, "_t%d", t); Buf bx; memset(&bx, 0, sizeof bx); emit_boxed_text(c, pt, ex, &bx); buf_puts(b, bx.p ? bx.p : "sp_box_nil()"); free(bx.p); } else buf_printf(b, "_t%d", t); buf_puts(b, ";\n"); }11451145
}11461146
int val = nt_ref(nt, pat, "value");11471147
if (val >= 0 && nt_type(nt, val) && !strcmp(nt_type(nt, val), "ArrayPatternNode"))@@ -1166,7 +1166,16 @@
11661166
const char *lnm = nt_str(nt, reqs[i], "name");11671167
if (!lnm) continue;11681168
emit_indent(b, body_indent);1169-
buf_printf(b, "lv_%s = sp_%sArray_get(_t%d, %dLL);\n", lnm, k, t, (long long)i);1169+
buf_printf(b, "lv_%s = ", lnm);1170+
LocalVar *plv = scope_local(comp_scope_of(c, id), lnm);1171+
char gx[64]; snprintf(gx, sizeof gx, "sp_%sArray_get(_t%d, %dLL)", k, t, i);1172+
if (plv && plv->type == TY_POLY && strcmp(k, "Poly")) {1173+
Buf bx; memset(&bx, 0, sizeof bx);1174+
emit_boxed_text(c, ty_array_elem(arr_t), gx, &bx);1175+
buf_puts(b, bx.p ? bx.p : "sp_box_nil()"); free(bx.p);1176+
}1177+
else buf_puts(b, gx);1178+
buf_puts(b, ";\n");11701179
}11711180
if (rest_nid >= 0 && nt_type(nt, rest_nid) &&11721181
!strcmp(nt_type(nt, rest_nid), "SplatNode")) {@@ -3108,7 +3117,16 @@
31083117
const char *lnm = nt_str(nt, reqs[i], "name");31093118
if (!lnm) continue;31103119
emit_indent(b, indent);3111-
buf_printf(b, "lv_%s = sp_%sArray_get(_t%d, %dLL);\n", lnm, k, tarr, (long long)i);3120+
buf_printf(b, "lv_%s = ", lnm);3121+
LocalVar *plv = scope_local(comp_scope_of(c, id), lnm);3122+
char gx[64]; snprintf(gx, sizeof gx, "sp_%sArray_get(_t%d, %dLL)", k, tarr, i);3123+
if (plv && plv->type == TY_POLY && strcmp(k, "Poly")) {3124+
Buf bx; memset(&bx, 0, sizeof bx);3125+
emit_boxed_text(c, ty_array_elem(vt != TY_UNKNOWN ? vt : TY_INT_ARRAY), gx, &bx);3126+
buf_puts(b, bx.p ? bx.p : "sp_box_nil()"); free(bx.p);3127+
}3128+
else buf_puts(b, gx);3129+
buf_puts(b, ";\n");31123130
}31133131
}31143132
else if (!strcmp(pty, "HashPatternNode")) {