Commit ef970434

ef9704349dffMerged

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

Description

spinel-c: fix array each block-param shadowing save/restore

Block params in Ruby shadow outer variables — the outer variable's value is preserved after the loop. Emit a typed save before the for-loop and a restore after it whenever the block param name matches an existing local in the enclosing scope.

Use emit_ctype for the save variable to match the outer variable's C type (mrb_int, const char *, sp_RbVal, etc.) instead of hard-coding sp_RbVal. Covers both TY_POLY_ARRAY and typed-array (Int/Str/Float) each / each_entry / reverse_each paths.

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

Details

Commit
ef9704349dff
Parent
f5b2abe6953c

src/codegen.cmodifiedView file

@@ -4989,6 +4989,14 @@
49894989 int t = ++g_tmp;
49904990 Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);
49914991 int ta = ++g_tmp;
4992+ /* Detect block param shadowing an outer variable; save/restore to preserve outer value */
4993+ Scope *cs_pa = p0 ? comp_scope_of(c, id) : NULL;
4994+ LocalVar *outer_pa = (p0 && cs_pa) ? scope_local(cs_pa, p0) : NULL;
4995+ int ts_pa = 0;
4996+ if (outer_pa) {
4997+ ts_pa = ++g_tmp; Buf ot_pa; memset(&ot_pa, 0, sizeof ot_pa); emit_ctype(c, outer_pa->type, &ot_pa);
4998+ emit_indent(b, indent); buf_printf(b, "%s _t%d = lv_%s;\n", ot_pa.p ? ot_pa.p : "sp_RbVal", ts_pa, p0); free(ot_pa.p);
4999+ }
49925000 emit_indent(b, indent);
49935001 buf_printf(b, "sp_PolyArray *_t%d = %s;\n", ta, rb.p ? rb.p : ""); free(rb.p);
49945002 emit_indent(b, indent);
@@ -4996,6 +5004,7 @@
49965004 if (p0) { emit_indent(b, indent + 1); buf_printf(b, "lv_%s = sp_PolyArray_get(_t%d, _t%d);\n", p0, ta, t); }
49975005 emit_stmts(c, body, b, indent + 1);
49985006 emit_indent(b, indent); buf_puts(b, "}\n");
5007+ if (outer_pa) { emit_indent(b, indent); buf_printf(b, "lv_%s = _t%d;\n", p0, ts_pa); }
49995008 return 1;
50005009 }
50015010 if ((!strcmp(name, "each") || !strcmp(name, "each_entry") || !strcmp(name, "reverse_each")) &&
@@ -5006,6 +5015,19 @@
50065015 int t = ++g_tmp, tn = ++g_tmp;
50075016 Buf rb; memset(&rb, 0, sizeof rb);
50085017 emit_expr(c, recv, &rb);
5018+ /* Detect block param shadowing an outer variable; save/restore to preserve outer value */
5019+ TyKind et = p0 ? ty_array_elem(rt) : TY_UNKNOWN;
5020+ Scope *cs = p0 ? comp_scope_of(c, id) : NULL;
5021+ LocalVar *outer = (p0 && cs) ? scope_local(cs, p0) : NULL;
5022+ int box_to_poly = outer && outer->type == TY_POLY && et != TY_POLY;
5023+ int ts = 0;
5024+ if (outer) {
5025+ /* Block params shadow outer variables in Ruby; save and restore */
5026+ ts = ++g_tmp;
5027+ Buf ot_ea; memset(&ot_ea, 0, sizeof ot_ea); emit_ctype(c, outer->type, &ot_ea);
5028+ emit_indent(b, indent);
5029+ buf_printf(b, "%s _t%d = lv_%s;\n", ot_ea.p ? ot_ea.p : "sp_RbVal", ts, p0); free(ot_ea.p);
5030+ }
50095031 if (rev) { emit_indent(b, indent); buf_printf(b, "mrb_int _t%d = sp_%sArray_length(%s);\n", tn, k, rb.p); }
50105032 emit_indent(b, indent);
50115033 if (rev) buf_printf(b, "for (mrb_int _t%d = _t%d - 1; _t%d >= 0; _t%d--) {\n", t, tn, t, t);
@@ -5015,11 +5037,24 @@
50155037 }
50165038 if (p0) {
50175039 emit_indent(b, indent + 1);
5018- buf_printf(b, "lv_%s = sp_%sArray_get(", p0, k);
5019- buf_puts(b, rb.p); buf_printf(b, ", _t%d);\n", t);
5040+ if (box_to_poly) {
5041+ if (et == TY_INT) buf_printf(b, "lv_%s = sp_box_int(sp_%sArray_get(", p0, k);
5042+ else if (et == TY_STRING) buf_printf(b, "lv_%s = sp_box_str(sp_%sArray_get(", p0, k);
5043+ else if (et == TY_FLOAT) buf_printf(b, "lv_%s = sp_box_float(sp_%sArray_get(", p0, k);
5044+ else if (et == TY_BOOL) buf_printf(b, "lv_%s = sp_box_bool(sp_%sArray_get(", p0, k);
5045+ else buf_printf(b, "lv_%s = sp_%sArray_get(", p0, k);
5046+ buf_puts(b, rb.p); buf_printf(b, ", _t%d)", t);
5047+ if (et == TY_INT || et == TY_STRING || et == TY_FLOAT || et == TY_BOOL) buf_puts(b, ")");
5048+ buf_puts(b, ";\n");
5049+ }
5050+ else {
5051+ buf_printf(b, "lv_%s = sp_%sArray_get(", p0, k);
5052+ buf_puts(b, rb.p); buf_printf(b, ", _t%d);\n", t);
5053+ }
50205054 }
50215055 emit_stmts(c, body, b, indent + 1);
50225056 emit_indent(b, indent); buf_puts(b, "}\n");
5057+ if (outer) { emit_indent(b, indent); buf_printf(b, "lv_%s = _t%d;\n", p0, ts); }
50235058 free(rb.p);
50245059 return 1;
50255060 }