Commit 4c45b797

4c45b7974edeMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

codegen: sort comparator block uses C shadow scope for poly-widened params

When multiple sort/each blocks in the same method share param names with different element types (int vs string), the LocalVar for the shared name gets widened to poly by the block-param scope collision. Use a C shadow scope { et lv_a = _ta; et lv_b = _tb; ... } to emit the block body with correctly-typed locals, matching the existing emit_minmax_cmp_expr pattern.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
4c45b7974ede
Parent
8b8a2044e5eb

src/codegen.cmodifiedView file

@@ -1037,15 +1037,21 @@
10371037 TyKind spt1 = slv1 ? slv1->type : TY_UNKNOWN;
10381038 if (slv0) slv0->type = et;
10391039 if (slv1) slv1->type = et;
1040+ for (int j = 0; j < bn; j++) infer_type(c, bb[j]); /* refresh ntype cache */
10401041 int save = g_indent; g_indent += 2;
1041- emit_indent(g_pre, g_indent); buf_printf(g_pre, "lv_%s = _t%d; lv_%s = _t%d;\n", p0, ta, p1, tb);
1042+ /* Shadow the outer (possibly poly) block params with et-typed locals */
1043+ emit_indent(g_pre, g_indent); buf_puts(g_pre, "{\n"); g_indent++;
1044+ emit_indent(g_pre, g_indent); emit_ctype(c, et, g_pre); buf_printf(g_pre, " lv_%s = _t%d; ", p0, ta);
1045+ emit_ctype(c, et, g_pre); buf_printf(g_pre, " lv_%s = _t%d;\n", p1, tb);
10421046 for (int j = 0; j < bn - 1; j++) emit_stmt(c, bb[j], g_pre, g_indent);
1043- Buf cb; memset(&cb, 0, sizeof cb); emit_expr(c, bb[bn - 1], &cb); g_indent = save;
1047+ Buf cb; memset(&cb, 0, sizeof cb); emit_expr(c, bb[bn - 1], &cb);
1048+ emit_indent(g_pre, g_indent);
1049+ buf_printf(g_pre, "if ((%s) > 0) { sp_%sArray_set(_t%d, _t%d, _t%d); sp_%sArray_set(_t%d, _t%d + 1, _t%d); }\n",
1050+ cb.p ? cb.p : "0", k, tr, tj, tb, k, tr, tj, ta); free(cb.p);
1051+ g_indent--; g_indent = save;
1052+ emit_indent(g_pre, g_indent + 2); buf_puts(g_pre, "}\n");
10441053 if (slv0) slv0->type = spt0;
10451054 if (slv1) slv1->type = spt1;
1046- emit_indent(g_pre, g_indent + 2);
1047- buf_printf(g_pre, "if ((%s) > 0) { sp_%sArray_set(_t%d, _t%d, _t%d); sp_%sArray_set(_t%d, _t%d + 1, _t%d); }\n",
1048- cb.p ? cb.p : "0", k, tr, tj, tb, k, tr, tj, ta); free(cb.p);
10491055 emit_indent(g_pre, g_indent + 1); buf_puts(g_pre, "}\n");
10501056 buf_printf(b, "_t%d", tr);
10511057 return 1;