Commit 0598908a

0598908af5faMerged

dak2 <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): isolate block param scope so infer_type ignores outer same-named locals

infer_type walks the scope stack from the tail and returns the first matching binding, so a block parameter that shadowed an outer same-named local of a different type would resolve against the outer type. This dispatched the wrong runtime call (e.g. sp_str_repeat for an int param). Wrap each iterator helper's block body in push_scope/declare_var/pop_scope so infer_type sees the inner binding.

Follow up: https://github.com/matz/spinel/issues/77

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
0598908af5fa
Parent
9f101b63bdce

spinel_codegen.rbmodifiedView file

@@ -1982,7 +1982,9 @@
19821982 return "int"
19831983 end
19841984 if mname == "then" || mname == "yield_self"
1985- # Return type is the block's return type
1985+ # Return type is the block's return type. Bind the block param to
1986+ # the receiver's type so infer_type sees the inner shadow, not any
1987+ # outer same-named local of a different type.
19861988 if recv >= 0
19871989 blk = @nd_block[nid]
19881990 if blk >= 0
@@ -1990,7 +1992,16 @@
19901992 if bbody >= 0
19911993 bbs = get_stmts(bbody)
19921994 if bbs.length > 0
1993- return infer_type(bbs.last)
1995+ bp = get_block_param(nid, 0)
1996+ if bp == ""
1997+ bp = "_x"
1998+ end
1999+ recv_t = infer_type(recv)
2000+ push_scope
2001+ declare_var(bp, recv_t)
2002+ rt = infer_type(bbs.last)
2003+ pop_scope
2004+ return rt
19942005 end
19952006 end
19962007 end
@@ -2529,14 +2540,21 @@
25292540 end
25302541 if mname == "map"
25312542 if recv >= 0
2532- # Determine result array type from block return type
2543+ # Declare bp inside a scope so infer_type sees the inner element type, not a shadowed outer local.
25332544 blk = @nd_block[nid]
25342545 if blk >= 0
25352546 bbody = @nd_body[blk]
25362547 if bbody >= 0
25372548 bbs = get_stmts(bbody)
25382549 if bbs.length > 0
2550+ recv_t = infer_type(recv)
2551+ bp1 = get_block_param(nid, 0)
2552+ push_scope
2553+ if bp1 != ""
2554+ declare_var(bp1, iter_elem_type(recv_t))
2555+ end
25392556 bret = infer_type(bbs.last)
2557+ pop_scope
25402558 if bret == "string"
25412559 return "str_array"
25422560 end
@@ -15319,7 +15337,6 @@
1531915337 bp1 = "_x"
1532015338 end
1532115339 et = elem_type_of_array(recv_type)
15322- declare_var(bp1, et)
1532315340 tmp_t = new_temp
1532415341 tmp_f = new_temp
1532515342 tmp_res = new_temp
@@ -15327,7 +15344,9 @@
1532715344 emit(" " + c_type(recv_type) + " " + tmp_t + " = sp_" + pfx + "_new();")
1532815345 emit(" " + c_type(recv_type) + " " + tmp_f + " = sp_" + pfx + "_new();")
1532915346 emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_" + pfx + "_length(" + rc + "); " + itmp + "++) {")
15330- emit(" lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + itmp + ");")
15347+ emit(" " + c_type(et) + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + itmp + ");")
15348+ push_scope
15349+ declare_var(bp1, et)
1533115350 blk = @nd_block[nid]
1533215351 bexpr = "0"
1533315352 if @nd_body[blk] >= 0
@@ -15342,6 +15361,7 @@
1534215361 end
1534315362 end
1534415363 emit(" if (" + bexpr + ") sp_" + pfx + "_push(" + tmp_t + ", lv_" + bp1 + "); else sp_" + pfx + "_push(" + tmp_f + ", lv_" + bp1 + ");")
15364+ pop_scope
1534515365 emit(" }")
1534615366 emit(" " + name + " *" + tmp_res + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(tt) + ");")
1534715367 emit(" " + tmp_res + "->_0 = " + tmp_t + ";")
@@ -15946,11 +15966,12 @@
1594615966 if @nd_block[nid] >= 0
1594715967 blk = @nd_block[nid]
1594815968 bp = get_block_param(nid, 0)
15949- declare_var(bp, "int")
1595015969 tmp = new_temp
1595115970 emit(" sp_StrIntHash *" + tmp + " = sp_StrIntHash_new();")
1595215971 emit(" for (mrb_int _i = 0; _i < " + rc + "->len; _i++) {")
1595315972 emit(" mrb_int lv_" + bp + " = sp_StrIntHash_get(" + rc + ", " + rc + "->order[_i]);")
15973+ push_scope
15974+ declare_var(bp, "int")
1595415975 bbody = @nd_body[blk]
1595515976 bexpr = "0"
1595615977 if bbody >= 0
@@ -15960,6 +15981,7 @@
1596015981 end
1596115982 end
1596215983 emit(" sp_StrIntHash_set(" + tmp + ", " + rc + "->order[_i], " + bexpr + ");")
15984+ pop_scope
1596315985 emit(" }")
1596415986 return tmp
1596515987 end
@@ -19448,7 +19470,6 @@
1944819470 if bp == ""
1944919471 bp = "_c"
1945019472 end
19451- declare_var(bp, "string")
1945219473 tmp = new_temp
1945319474 src = rc
1945419475 if rt == "mutable_str"
@@ -19463,9 +19484,12 @@
1946319484 emit(" char *" + char_buf + " = sp_str_alloc_raw(" + cn_tmp + " + 1);")
1946419485 emit(" memcpy(" + char_buf + ", " + src_tmp + " + " + tmp + ", " + cn_tmp + ");")
1946519486 emit(" " + char_buf + "[" + cn_tmp + "] = 0;")
19466- emit(" lv_" + bp + " = " + char_buf + ";")
19487+ emit(" const char *lv_" + bp + " = " + char_buf + ";")
1946719488 @indent = @indent + 1
19489+ push_scope
19490+ declare_var(bp, "string")
1946819491 compile_stmts_body(@nd_body[@nd_block[nid]])
19492+ pop_scope
1946919493 @indent = @indent - 1
1947019494 emit(" " + tmp + " += " + cn_tmp + ";")
1947119495 emit(" }")
@@ -19483,7 +19507,6 @@
1948319507 if bp == ""
1948419508 bp = "_b"
1948519509 end
19486- declare_var(bp, "int")
1948719510 tmp = new_temp
1948819511 src = rc
1948919512 if rt == "mutable_str"
@@ -19492,9 +19515,12 @@
1949219515 src_tmp = new_temp
1949319516 emit(" const char *" + src_tmp + " = " + src + ";")
1949419517 emit(" for (mrb_int " + tmp + " = 0; " + src_tmp + "[" + tmp + "]; " + tmp + "++) {")
19495- emit(" lv_" + bp + " = (unsigned char)" + src_tmp + "[" + tmp + "];")
19518+ emit(" mrb_int lv_" + bp + " = (unsigned char)" + src_tmp + "[" + tmp + "];")
1949619519 @indent = @indent + 1
19520+ push_scope
19521+ declare_var(bp, "int")
1949719522 compile_stmts_body(@nd_body[@nd_block[nid]])
19523+ pop_scope
1949819524 @indent = @indent - 1
1949919525 emit(" }")
1950019526 return 1
@@ -19511,7 +19537,6 @@
1951119537 if bp == ""
1951219538 bp = "_l"
1951319539 end
19514- declare_var(bp, "string")
1951519540 @needs_str_array = 1
1951619541 tmp_arr = new_temp
1951719542 tmp_i = new_temp
@@ -19521,9 +19546,12 @@
1952119546 end
1952219547 emit(" sp_StrArray *" + tmp_arr + " = sp_str_split(" + src + ", \"\\n\");")
1952319548 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < " + tmp_arr + "->len; " + tmp_i + "++) {")
19524- emit(" lv_" + bp + " = " + tmp_arr + "->data[" + tmp_i + "];")
19549+ emit(" const char *lv_" + bp + " = " + tmp_arr + "->data[" + tmp_i + "];")
1952519550 @indent = @indent + 1
19551+ push_scope
19552+ declare_var(bp, "string")
1952619553 compile_stmts_body(@nd_body[@nd_block[nid]])
19554+ pop_scope
1952719555 @indent = @indent - 1
1952819556 emit(" }")
1952919557 return 1
@@ -19561,13 +19589,18 @@
1956119589 bp2 = "_b"
1956219590 end
1956319591 pfx = array_c_prefix(rt)
19564- et = c_type(elem_type_of_array(rt))
19592+ elem_t = elem_type_of_array(rt)
19593+ et = c_type(elem_t)
1956519594 tmp_i = new_temp
1956619595 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")
1956719596 emit(" " + et + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")
1956819597 emit(" " + et + " lv_" + bp2 + " = sp_" + pfx + "_get(" + arg + ", " + tmp_i + ");")
1956919598 @indent = @indent + 1
19599+ push_scope
19600+ declare_var(bp1, elem_t)
19601+ declare_var(bp2, elem_t)
1957019602 compile_stmts_body(@nd_body[@nd_block[nid]])
19603+ pop_scope
1957119604 @indent = @indent - 1
1957219605 emit(" }")
1957319606 @in_loop = old
@@ -19608,7 +19641,10 @@
1960819641 end
1960919642 emit(" for (lv_" + bp1 + " = " + rc + "; lv_" + bp1 + " <= " + limit_val + "; lv_" + bp1 + " += " + step_val + ") {")
1961019643 @indent = @indent + 1
19644+ push_scope
19645+ declare_var(bp1, "int")
1961119646 compile_stmts_body(@nd_body[@nd_block[nid]])
19647+ pop_scope
1961219648 @indent = @indent - 1
1961319649 emit(" }")
1961419650 if synth == 1
@@ -19631,13 +19667,17 @@
1963119667 bp1 = "_x"
1963219668 end
1963319669 pfx = array_c_prefix(rt)
19670+ et = elem_type_of_array(rt)
1963419671 tmp_c = new_temp
1963519672 tmp_i = new_temp
1963619673 emit(" for (mrb_int " + tmp_c + " = 0; " + tmp_c + " < " + n + "; " + tmp_c + "++)")
1963719674 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")
19638- emit(" lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")
19675+ emit(" " + c_type(et) + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")
1963919676 @indent = @indent + 1
19677+ push_scope
19678+ declare_var(bp1, et)
1964019679 compile_stmts_body(@nd_body[@nd_block[nid]])
19680+ pop_scope
1964119681 @indent = @indent - 1
1964219682 emit(" }")
1964319683 @in_loop = old
@@ -19666,12 +19706,14 @@
1966619706 tmp_i = new_temp
1966719707 emit(" sp_StrArray *" + tmp_arr + " = sp_re_scan(sp_re_pat_" + ridx.to_s + ", " + rc + ");")
1966819708 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < " + tmp_arr + "->len; " + tmp_i + "++) {")
19669- set_var_type(bp, "string")
19670- emit(" lv_" + bp + " = " + tmp_arr + "->data[" + tmp_i + "];")
19709+ emit(" const char *lv_" + bp + " = " + tmp_arr + "->data[" + tmp_i + "];")
19710+ push_scope
19711+ declare_var(bp, "string")
1967119712 blk = @nd_block[nid]
1967219713 if @nd_body[blk] >= 0
1967319714 compile_stmts_body(@nd_body[blk])
1967419715 end
19716+ pop_scope
1967519717 emit(" }")
1967619718 return 1
1967719719 end
@@ -20027,11 +20069,12 @@
2002720069 if @nd_block[nid] >= 0
2002820070 lblk = @nd_block[nid]
2002920071 lbp = get_block_param(nid, 0)
20030- declare_var(lbp, "string")
2003120072 ltmp = new_temp
2003220073 emit(" { char " + ltmp + "[4096];")
2003320074 emit(" while (fgets(" + ltmp + ", sizeof(" + ltmp + "), " + ftmp + ")) {")
2003420075 emit(" const char *lv_" + lbp + " = " + ltmp + ";")
20076+ push_scope
20077+ declare_var(lbp, "string")
2003520078 # Compile block body
2003620079 lbbody = @nd_body[lblk]
2003720080 if lbbody >= 0
@@ -20042,6 +20085,7 @@
2004220085 lbk = lbk + 1
2004320086 end
2004420087 end
20088+ pop_scope
2004520089 emit(" } }")
2004620090 return
2004720091 end
@@ -21474,15 +21518,28 @@
2147421518 tmp_j = new_temp
2147521519 tmp_len = new_temp
2147621520 pfx = array_c_prefix(rt)
21477- declare_var(bp1, rt)
2147821521 @needs_gc = 1
2147921522 emit(" mrb_int " + tmp_len + " = sp_" + pfx + "_length(" + rc + ");")
2148021523 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < " + tmp_len + "; " + tmp_i + " += " + n + ") {")
21481- emit(" lv_" + bp1 + " = sp_" + pfx + "_new();")
21524+ # When bp1 shadows an outer same-named local of a different C type, the
21525+ # function-level lv_<bp1> has the outer type. Emit a block-scoped fresh
21526+ # declaration with its own GC root so the slice survives allocations in
21527+ # the user block body.
21528+ outer_t = find_var_type(bp1)
21529+ if outer_t != "" && outer_t != rt
21530+ emit(" SP_GC_SAVE();")
21531+ emit(" " + c_type(rt) + " lv_" + bp1 + " = sp_" + pfx + "_new();")
21532+ emit(" SP_GC_ROOT(lv_" + bp1 + ");")
21533+ else
21534+ emit(" lv_" + bp1 + " = sp_" + pfx + "_new();")
21535+ end
2148221536 emit(" for (mrb_int " + tmp_j + " = 0; " + tmp_j + " < " + n + " && " + tmp_i + " + " + tmp_j + " < " + tmp_len + "; " + tmp_j + "++)")
2148321537 emit(" sp_" + pfx + "_push(lv_" + bp1 + ", sp_" + pfx + "_get(" + rc + ", " + tmp_i + " + " + tmp_j + "));")
2148421538 @indent = @indent + 1
21539+ push_scope
21540+ declare_var(bp1, rt)
2148521541 compile_stmts_body(@nd_body[@nd_block[nid]])
21542+ pop_scope
2148621543 @indent = @indent - 1
2148721544 emit(" }")
2148821545 @in_loop = old
@@ -21502,15 +21559,26 @@
2150221559 tmp_j = new_temp
2150321560 tmp_len = new_temp
2150421561 pfx = array_c_prefix(rt)
21505- declare_var(bp1, rt)
2150621562 @needs_gc = 1
2150721563 emit(" mrb_int " + tmp_len + " = sp_" + pfx + "_length(" + rc + ");")
2150821564 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " + " + n + " <= " + tmp_len + "; " + tmp_i + "++) {")
21509- emit(" lv_" + bp1 + " = sp_" + pfx + "_new();")
21565+ # See compile_each_slice_block: shadow case needs a fresh typed slot with
21566+ # its own GC root.
21567+ outer_t = find_var_type(bp1)
21568+ if outer_t != "" && outer_t != rt
21569+ emit(" SP_GC_SAVE();")
21570+ emit(" " + c_type(rt) + " lv_" + bp1 + " = sp_" + pfx + "_new();")
21571+ emit(" SP_GC_ROOT(lv_" + bp1 + ");")
21572+ else
21573+ emit(" lv_" + bp1 + " = sp_" + pfx + "_new();")
21574+ end
2151021575 emit(" for (mrb_int " + tmp_j + " = 0; " + tmp_j + " < " + n + "; " + tmp_j + "++)")
2151121576 emit(" sp_" + pfx + "_push(lv_" + bp1 + ", sp_" + pfx + "_get(" + rc + ", " + tmp_i + " + " + tmp_j + "));")
2151221577 @indent = @indent + 1
21578+ push_scope
21579+ declare_var(bp1, rt)
2151321580 compile_stmts_body(@nd_body[@nd_block[nid]])
21581+ pop_scope
2151421582 @indent = @indent - 1
2151521583 emit(" }")
2151621584 @in_loop = old
@@ -21561,7 +21629,10 @@
2156121629 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")
2156221630 emit(" " + c_type(elem_type_of_array(rt)) + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")
2156321631 @indent = @indent + 1
21632+ push_scope
21633+ declare_var(bp1, elem_type_of_array(rt))
2156421634 compile_stmts_body(@nd_body[@nd_block[nid]])
21635+ pop_scope
2156521636 @indent = @indent - 1
2156621637 emit(" }")
2156721638 emit(" " + result + " = lv_" + bp2 + ";")
@@ -21591,7 +21662,11 @@
2159121662 emit(" " + c_type(elem_type_of_array(rt)) + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
2159221663 emit(" mrb_int lv_" + bp2 + " = " + tmp + ";")
2159321664 @indent = @indent + 1
21665+ push_scope
21666+ declare_var(bp1, elem_type_of_array(rt))
21667+ declare_var(bp2, "int")
2159421668 compile_stmts_body(@nd_body[@nd_block[nid]])
21669+ pop_scope
2159521670 @indent = @indent - 1
2159621671 emit(" }")
2159721672 @in_loop = old
@@ -21618,7 +21693,12 @@
2161821693 emit(" " + c_type(elem_type_of_array(rt)) + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
2161921694 end
2162021695 @indent = @indent + 1
21696+ push_scope
21697+ if has_bp == 1
21698+ declare_var(bp1, elem_type_of_array(rt))
21699+ end
2162121700 compile_stmts_body(@nd_body[@nd_block[nid]])
21701+ pop_scope
2162221702 @indent = @indent - 1
2162321703 emit(" }")
2162421704 end
@@ -21629,7 +21709,12 @@
2162921709 emit(" lv_" + bp1 + " = (sp_sym)sp_IntArray_get(" + rc + ", " + tmp + ");")
2163021710 end
2163121711 @indent = @indent + 1
21712+ push_scope
21713+ if has_bp == 1
21714+ declare_var(bp1, "symbol")
21715+ end
2163221716 compile_stmts_body(@nd_body[@nd_block[nid]])
21717+ pop_scope
2163321718 @indent = @indent - 1
2163421719 emit(" }")
2163521720 end
@@ -21643,7 +21728,12 @@
2164321728 emit(" lv_" + bp1 + " = " + bp_tmp + ";")
2164421729 end
2164521730 @indent = @indent + 1
21731+ push_scope
21732+ if has_bp == 1
21733+ declare_var(bp1, elem_type)
21734+ end
2164621735 compile_stmts_body(@nd_body[@nd_block[nid]])
21736+ pop_scope
2164721737 @indent = @indent - 1
2164821738 emit(" }")
2164921739 end
@@ -21655,7 +21745,13 @@
2165521745 emit(" lv_" + bp2 + " = sp_StrIntHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2165621746 end
2165721747 @indent = @indent + 1
21748+ push_scope
21749+ declare_var(bp1, "string")
21750+ if bp2 != ""
21751+ declare_var(bp2, "int")
21752+ end
2165821753 compile_stmts_body(@nd_body[@nd_block[nid]])
21754+ pop_scope
2165921755 @indent = @indent - 1
2166021756 emit(" }")
2166121757 end
@@ -21667,7 +21763,13 @@
2166721763 emit(" lv_" + bp2 + " = sp_IntStrHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2166821764 end
2166921765 @indent = @indent + 1
21766+ push_scope
21767+ declare_var(bp1, "int")
21768+ if bp2 != ""
21769+ declare_var(bp2, "string")
21770+ end
2167021771 compile_stmts_body(@nd_body[@nd_block[nid]])
21772+ pop_scope
2167121773 @indent = @indent - 1
2167221774 emit(" }")
2167321775 end
@@ -21679,7 +21781,13 @@
2167921781 emit(" lv_" + bp2 + " = sp_StrStrHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2168021782 end
2168121783 @indent = @indent + 1
21784+ push_scope
21785+ declare_var(bp1, "string")
21786+ if bp2 != ""
21787+ declare_var(bp2, "string")
21788+ end
2168221789 compile_stmts_body(@nd_body[@nd_block[nid]])
21790+ pop_scope
2168321791 @indent = @indent - 1
2168421792 emit(" }")
2168521793 end
@@ -21691,7 +21799,13 @@
2169121799 emit(" lv_" + bp2 + " = sp_SymIntHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2169221800 end
2169321801 @indent = @indent + 1
21802+ push_scope
21803+ declare_var(bp1, "symbol")
21804+ if bp2 != ""
21805+ declare_var(bp2, "int")
21806+ end
2169421807 compile_stmts_body(@nd_body[@nd_block[nid]])
21808+ pop_scope
2169521809 @indent = @indent - 1
2169621810 emit(" }")
2169721811 end
@@ -21703,7 +21817,13 @@
2170321817 emit(" lv_" + bp2 + " = sp_SymStrHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2170421818 end
2170521819 @indent = @indent + 1
21820+ push_scope
21821+ declare_var(bp1, "symbol")
21822+ if bp2 != ""
21823+ declare_var(bp2, "string")
21824+ end
2170621825 compile_stmts_body(@nd_body[@nd_block[nid]])
21826+ pop_scope
2170721827 @indent = @indent - 1
2170821828 emit(" }")
2170921829 end
@@ -21715,7 +21835,13 @@
2171521835 emit(" lv_" + bp2 + " = sp_SymPolyHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2171621836 end
2171721837 @indent = @indent + 1
21838+ push_scope
21839+ declare_var(bp1, "symbol")
21840+ if bp2 != ""
21841+ declare_var(bp2, "poly")
21842+ end
2171821843 compile_stmts_body(@nd_body[@nd_block[nid]])
21844+ pop_scope
2171921845 @indent = @indent - 1
2172021846 emit(" }")
2172121847 end
@@ -21727,7 +21853,13 @@
2172721853 emit(" lv_" + bp2 + " = sp_StrPolyHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2172821854 end
2172921855 @indent = @indent + 1
21856+ push_scope
21857+ declare_var(bp1, "string")
21858+ if bp2 != ""
21859+ declare_var(bp2, "poly")
21860+ end
2173021861 compile_stmts_body(@nd_body[@nd_block[nid]])
21862+ pop_scope
2173121863 @indent = @indent - 1
2173221864 emit(" }")
2173321865 end
@@ -21737,7 +21869,12 @@
2173721869 emit(" sp_Range " + tmp2 + " = " + rc + ";")
2173821870 emit(" for (lv_" + bp1 + " = " + tmp2 + ".first; lv_" + bp1 + " <= " + tmp2 + ".last; lv_" + bp1 + "++) {")
2173921871 @indent = @indent + 1
21872+ push_scope
21873+ if has_bp == 1
21874+ declare_var(bp1, "int")
21875+ end
2174021876 compile_stmts_body(@nd_body[@nd_block[nid]])
21877+ pop_scope
2174121878 @indent = @indent - 1
2174221879 emit(" }")
2174321880 end
@@ -21746,7 +21883,12 @@
2174621883 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_PolyArray_length(" + rc + "); " + tmp + "++) {")
2174721884 emit(" lv_" + bp1 + " = sp_PolyArray_get(" + rc + ", " + tmp + ");")
2174821885 @indent = @indent + 1
21886+ push_scope
21887+ if has_bp == 1
21888+ declare_var(bp1, "poly")
21889+ end
2174921890 compile_stmts_body(@nd_body[@nd_block[nid]])
21891+ pop_scope
2175021892 @indent = @indent - 1
2175121893 emit(" }")
2175221894 end
@@ -21764,7 +21906,12 @@
2176421906 emit(" lv_" + bp1 + " = " + tmp + ";")
2176521907 end
2176621908 @indent = @indent + 1
21909+ push_scope
21910+ if bp1 != ""
21911+ declare_var(bp1, "int")
21912+ end
2176721913 compile_stmts_body(@nd_body[@nd_block[nid]])
21914+ pop_scope
2176821915 @indent = @indent - 1
2176921916 emit(" }")
2177021917 @in_loop = old
@@ -21782,7 +21929,12 @@
2178221929 emit(" lv_" + bp1 + " = " + tmp + ";")
2178321930 end
2178421931 @indent = @indent + 1
21932+ push_scope
21933+ if bp1 != ""
21934+ declare_var(bp1, "int")
21935+ end
2178521936 compile_stmts_body(@nd_body[@nd_block[nid]])
21937+ pop_scope
2178621938 @indent = @indent - 1
2178721939 emit(" }")
2178821940 @in_loop = old
@@ -21800,24 +21952,34 @@
2180021952 emit(" lv_" + bp1 + " = " + tmp + ";")
2180121953 end
2180221954 @indent = @indent + 1
21955+ push_scope
21956+ if bp1 != ""
21957+ declare_var(bp1, "int")
21958+ end
2180321959 compile_stmts_body(@nd_body[@nd_block[nid]])
21960+ pop_scope
2180421961 @indent = @indent - 1
2180521962 emit(" }")
2180621963 @in_loop = old
2180721964 end
2180821965
2180921966 def compile_tap_expr(nid)
21810- # Execute block with receiver bound to block param, return receiver
21967+ # Execute block with receiver bound to block param, return receiver.
21968+ # Open a C block so the param is a fresh local that shadows any
21969+ # outer same-named lv_<bp> without clobbering its value or type.
2181121970 rt = infer_type(@nd_receiver[nid])
2181221971 rc = compile_expr_gc_rooted(@nd_receiver[nid])
2181321972 bp = get_block_param(nid, 0)
2181421973 if bp == ""
2181521974 bp = "_x"
2181621975 end
21817- declare_var(bp, rt)
2181821976 tmp = new_temp
2181921977 emit(" " + c_type(rt) + " " + tmp + " = " + rc + ";")
21820- emit(" lv_" + bp + " = " + tmp + ";")
21978+ emit(" {")
21979+ emit(" " + c_type(rt) + " lv_" + bp + " = " + tmp + ";")
21980+ @indent = @indent + 1
21981+ push_scope
21982+ declare_var(bp, rt)
2182121983 blk = @nd_block[nid]
2182221984 bbody = @nd_body[blk]
2182321985 if bbody >= 0
@@ -21828,33 +21990,62 @@
2182821990 k = k + 1
2182921991 end
2183021992 end
21993+ pop_scope
21994+ @indent = @indent - 1
21995+ emit(" }")
2183121996 tmp
2183221997 end
2183321998
2183421999 def compile_then_expr(nid)
21835- # Execute block with receiver bound to block param, return block result
22000+ # Execute block with receiver bound to block param, return block result.
22001+ # Open a C block so the param is a fresh local that shadows any outer
22002+ # same-named lv_<bp>. The block's last-expression value is funneled
22003+ # through a result tmp declared in the enclosing scope so callers can
22004+ # still consume it after the C block closes.
2183622005 rt = infer_type(@nd_receiver[nid])
2183722006 rc = compile_expr_gc_rooted(@nd_receiver[nid])
2183822007 bp = get_block_param(nid, 0)
2183922008 if bp == ""
2184022009 bp = "_x"
2184122010 end
21842- declare_var(bp, rt)
21843- emit(" lv_" + bp + " = " + rc + ";")
2184422011 blk = @nd_block[nid]
2184522012 bbody = @nd_body[blk]
22013+
22014+ # Peek at the last expression's type under the inner binding so the
22015+ # result tmp is declared with the type that infer_type sees inside
22016+ # the block, not the type of any outer same-named local.
22017+ ret_t = "int"
22018+ bs = []
2184622019 if bbody >= 0
2184722020 bs = get_stmts(bbody)
22021+ if bs.length > 0
22022+ push_scope
22023+ declare_var(bp, rt)
22024+ ret_t = infer_type(bs.last)
22025+ pop_scope
22026+ end
22027+ end
22028+
22029+ result_tmp = new_temp
22030+ emit(" " + c_type(ret_t) + " " + result_tmp + " = " + c_default_val(ret_t) + ";")
22031+ emit(" {")
22032+ emit(" " + c_type(rt) + " lv_" + bp + " = " + rc + ";")
22033+ @indent = @indent + 1
22034+ push_scope
22035+ declare_var(bp, rt)
22036+ if bs.length > 0
2184822037 k = 0
2184922038 while k < bs.length - 1
2185022039 compile_stmt(bs[k])
2185122040 k = k + 1
2185222041 end
21853- if bs.length > 0
21854- return compile_expr(bs.last)
21855- end
22042+ last_expr = compile_expr(bs.last)
22043+ emit(" " + result_tmp + " = " + last_expr + ";")
2185622044 end
21857- "0"
22045+ pop_scope
22046+ @indent = @indent - 1
22047+ emit(" }")
22048+ result_tmp
2185822049 end
2185922050
2186022051 # Emit the loop-open lines for iterating over a receiver expression.
@@ -21888,11 +22079,12 @@
2188822079 if bp1 == ""
2188922080 bp1 = "_x"
2189022081 end
21891- declare_var(bp1, iter_elem_type(recv_type))
2189222082 tmp_sum = new_temp
2189322083 tmp_i = new_temp
2189422084 emit(" mrb_int " + tmp_sum + " = 0;")
2189522085 emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
22086+ push_scope
22087+ declare_var(bp1, iter_elem_type(recv_type))
2189622088 blk = @nd_block[nid]
2189722089 bexpr = "0"
2189822090 if @nd_body[blk] >= 0
@@ -21907,6 +22099,7 @@
2190722099 end
2190822100 end
2190922101 emit(" " + tmp_sum + " += " + bexpr + ";")
22102+ pop_scope
2191022103 emit(" }")
2191122104 tmp_sum
2191222105 end
@@ -21916,11 +22109,12 @@
2191622109 if bp1 == ""
2191722110 bp1 = "_x"
2191822111 end
21919- declare_var(bp1, iter_elem_type(recv_type))
2192022112 tmp_cnt = new_temp
2192122113 tmp_i = new_temp
2192222114 emit(" mrb_int " + tmp_cnt + " = 0;")
2192322115 emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
22116+ push_scope
22117+ declare_var(bp1, iter_elem_type(recv_type))
2192422118 blk = @nd_block[nid]
2192522119 bexpr = "0"
2192622120 if @nd_body[blk] >= 0
@@ -21935,6 +22129,7 @@
2193522129 end
2193622130 end
2193722131 emit(" if (" + bexpr + ") " + tmp_cnt + "++;")
22132+ pop_scope
2193822133 emit(" }")
2193922134 tmp_cnt
2194022135 end
@@ -21950,7 +22145,6 @@
2195022145 elsif recv_type == "float_array"
2195122146 elem_type = "float"
2195222147 end
21953- set_var_type(bp1, elem_type)
2195422148 tmp_res = new_temp
2195522149 tmp_key = new_temp
2195622150 tmp_i = new_temp
@@ -21960,6 +22154,8 @@
2196022154 emit(" " + c_type(elem_type) + " " + bp_tmp + " = " + c_default_val(elem_type) + ";")
2196122155 emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
2196222156 emit(" " + bp_tmp + " = lv_" + bp1 + ";")
22157+ push_scope
22158+ declare_var(bp1, elem_type)
2196322159 blk = @nd_block[nid]
2196422160 bexpr = "0"
2196522161 if @nd_body[blk] >= 0
@@ -21979,6 +22175,7 @@
2197922175 end
2198022176 emit(" mrb_int _k = " + bexpr + ";")
2198122177 emit(" if (" + tmp_i + " == 0 || _k " + cmp + " " + tmp_key + ") { " + tmp_res + " = " + bp_tmp + "; " + tmp_key + " = _k; }")
22178+ pop_scope
2198222179 emit(" }")
2198322180 tmp_res
2198422181 end
@@ -21988,8 +22185,9 @@
2198822185 if bp1 == ""
2198922186 bp1 = "_x"
2199022187 end
21991- declare_var(bp1, iter_elem_type(recv_type))
21992- # Determine result type from block return
22188+ elem_t = iter_elem_type(recv_type)
22189+ push_scope
22190+ declare_var(bp1, elem_t)
2199322191 blk = @nd_block[nid]
2199422192 block_ret = "int"
2199522193 if blk >= 0
@@ -22034,6 +22232,7 @@
2203422232 emit(" " + c_type(block_ret) + " " + tmp_val + " = " + bexpr + ";")
2203522233 emit(" if (" + tmp_val + ") sp_" + pfx_dst + "_push(" + tmp_arr + ", " + tmp_val + ");")
2203622234 @indent = @indent - 1
22235+ pop_scope
2203722236 emit(" }")
2203822237 tmp_arr
2203922238 end
@@ -22044,11 +22243,12 @@
2204422243 bp1 = "_x"
2204522244 end
2204622245 elem_type = iter_elem_type(recv_type)
22047- declare_var(bp1, elem_type)
2204822246 tmp_res = new_temp
2204922247 tmp_i = new_temp
2205022248 emit(" " + c_type(elem_type) + " " + tmp_res + " = " + c_default_val(elem_type) + ";")
2205122249 emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
22250+ push_scope
22251+ declare_var(bp1, elem_type)
2205222252 blk = @nd_block[nid]
2205322253 bbody = @nd_body[blk]
2205422254 bexpr = "0"
@@ -22064,6 +22264,7 @@
2206422264 end
2206522265 end
2206622266 emit(" if (" + bexpr + ") { " + tmp_res + " = lv_" + bp1 + "; break; }")
22267+ pop_scope
2206722268 emit(" }")
2206822269 tmp_res
2206922270 end
@@ -22074,7 +22275,6 @@
2207422275 if bp1 == ""
2207522276 bp1 = "_x"
2207622277 end
22077- declare_var(bp1, iter_elem_type(recv_type))
2207822278 tmp_res = new_temp
2207922279 tmp_i = new_temp
2208022280 init_val = "FALSE"
@@ -22083,6 +22283,8 @@
2208322283 end
2208422284 emit(" mrb_bool " + tmp_res + " = " + init_val + ";")
2208522285 emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
22286+ push_scope
22287+ declare_var(bp1, iter_elem_type(recv_type))
2208622288 blk = @nd_block[nid]
2208722289 bbody = @nd_body[blk]
2208822290 bexpr = "0"
@@ -22107,6 +22309,7 @@
2210722309 # none?
2210822310 emit(" if (" + bexpr + ") { " + tmp_res + " = FALSE; break; }")
2210922311 end
22312+ pop_scope
2211022313 emit(" }")
2211122314 tmp_res
2211222315 end
@@ -22139,14 +22342,15 @@
2213922342 @needs_str_str_hash = 1
2214022343 end
2214122344 @needs_gc = 1
22142- declare_var(bp1, "string")
22143- declare_var(bp2, val_type)
2214422345 tmp = new_temp
2214522346 itmp = new_temp
2214622347 emit(" " + c_type(hash_type) + tmp + " = " + ctor + "();")
2214722348 emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")
2214822349 emit(" lv_" + bp1 + " = " + rc + "->order[" + itmp + "];")
2214922350 emit(" lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")
22351+ push_scope
22352+ declare_var(bp1, "string")
22353+ declare_var(bp2, val_type)
2215022354 blk = @nd_block[nid]
2215122355 bbody = @nd_body[blk]
2215222356 bexpr = "0"
@@ -22167,6 +22371,7 @@
2216722371 cond = "!(" + bexpr + ")"
2216822372 end
2216922373 emit(" if (" + cond + ") " + setter + "(" + tmp + ", lv_" + bp1 + ", lv_" + bp2 + ");")
22374+ pop_scope
2217022375 emit(" }")
2217122376 tmp
2217222377 end
@@ -22186,6 +22391,7 @@
2218622391 val_type = "string"
2218722392 getter = "sp_StrStrHash_get"
2218822393 end
22394+ push_scope
2218922395 declare_var(bp1, "string")
2219022396 declare_var(bp2, val_type)
2219122397 itmp = new_temp
@@ -22224,6 +22430,7 @@
2222422430 end
2222522431 emit(" if (" + bexpr + ") " + tmp_c + "++;")
2222622432 emit(" }")
22433+ pop_scope
2222722434 return tmp_c
2222822435 end
2222922436 if mname == "any?"
@@ -22245,6 +22452,7 @@
2224522452 end
2224622453 emit(" if (" + bexpr + ") { " + tmp_r + " = TRUE; break; }")
2224722454 emit(" }")
22455+ pop_scope
2224822456 return tmp_r
2224922457 end
2225022458 if mname == "all?"
@@ -22266,6 +22474,7 @@
2226622474 end
2226722475 emit(" if (!(" + bexpr + ")) { " + tmp_r + " = FALSE; break; }")
2226822476 emit(" }")
22477+ pop_scope
2226922478 return tmp_r
2227022479 end
2227122480 # find / detect — return key of first match
@@ -22288,8 +22497,10 @@
2228822497 end
2228922498 emit(" if (" + bexpr + ") { " + tmp_r + " = lv_" + bp1 + "; break; }")
2229022499 emit(" }")
22500+ pop_scope
2229122501 return tmp_r
2229222502 end
22503+ pop_scope
2229322504 "0"
2229422505 end
2229522506
@@ -22301,7 +22512,18 @@
2230122512 if bp1 == ""
2230222513 bp1 = "_x"
2230322514 end
22304- # Determine result array type from block return type
22515+ elem_type = "int"
22516+ if rt == "str_array"
22517+ elem_type = "string"
22518+ elsif rt == "float_array"
22519+ elem_type = "float"
22520+ elsif rt == "poly_array"
22521+ elem_type = "poly"
22522+ elsif is_ptr_array_type(rt) == 1
22523+ elem_type = ptr_array_elem_type(rt)
22524+ end
22525+ push_scope
22526+ declare_var(bp1, elem_type)
2230522527 blk = @nd_block[nid]
2230622528 block_ret = "int_array"
2230722529 if blk >= 0
@@ -22325,18 +22547,6 @@
2232522547 tmp_inner = new_temp
2232622548 tmp_j = new_temp
2232722549 emit(" " + c_type(block_ret) + tmp_arr + " = sp_" + pfx_dst + "_new();")
22328- # Declare block param type from receiver element type
22329- elem_type = "int"
22330- if rt == "str_array"
22331- elem_type = "string"
22332- elsif rt == "float_array"
22333- elem_type = "float"
22334- elsif rt == "poly_array"
22335- elem_type = "poly"
22336- elsif is_ptr_array_type(rt) == 1
22337- elem_type = ptr_array_elem_type(rt)
22338- end
22339- declare_var(bp1, elem_type)
2234022550 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx_src + "_length(" + rc + "); " + tmp_i + "++) {")
2234122551 emit(" lv_" + bp1 + " = sp_" + pfx_src + "_get(" + rc + ", " + tmp_i + ");")
2234222552 @indent = @indent + 1
@@ -22357,6 +22567,7 @@
2235722567 end
2235822568 @indent = @indent - 1
2235922569 emit(" }")
22570+ pop_scope
2236022571 tmp_arr
2236122572 end
2236222573
@@ -22369,6 +22580,10 @@
2236922580 bpn = get_block_param(nid, 0)
2237022581 res_type = "int"
2237122582 blk_n = @nd_block[nid]
22583+ push_scope
22584+ if bpn != ""
22585+ declare_var(bpn, "int")
22586+ end
2237222587 if blk_n >= 0
2237322588 body_n = @nd_body[blk_n]
2237422589 if body_n >= 0
@@ -22417,6 +22632,7 @@
2241722632 end
2241822633 @indent = @indent - 1
2241922634 emit(" }")
22635+ pop_scope
2242022636 return tmp_arrn
2242122637 end
2242222638 rt = infer_type(@nd_receiver[nid])
@@ -22434,19 +22650,9 @@
2243422650 if rt == "int_array" || rt == "sym_array"
2243522651 @needs_int_array = 1
2243622652 @needs_gc = 1
22437- # Check if block body returns string (for map that produces StrArray)
22438- block_ret = "int"
22653+ bp_t = elem_type_of_array(rt)
22654+ # Check if block param is used as lambda (elements are lambda pointers in IntArray)
2243922655 blk = @nd_block[nid]
22440- if blk >= 0
22441- body = @nd_body[blk]
22442- if body >= 0
22443- stmts = get_stmts(body)
22444- if stmts.length > 0
22445- block_ret = infer_type(stmts.last)
22446- end
22447- end
22448- end
22449- # Check if block param is used as lambda (elements are lambda pointers in IntArray)
2245022656 bp_is_lambda = 0
2245122657 if blk >= 0
2245222658 bp_is_lambda = param_used_as_lambda(bp1, @nd_body[blk])
@@ -22476,15 +22682,29 @@
2247622682 end
2247722683 end
2247822684 end
22685+ if bp_is_lambda == 1
22686+ bp_t = "lambda"
22687+ end
22688+ push_scope
22689+ declare_var(bp1, bp_t)
22690+ block_ret = "int"
22691+ if blk >= 0
22692+ body = @nd_body[blk]
22693+ if body >= 0
22694+ stmts = get_stmts(body)
22695+ if stmts.length > 0
22696+ block_ret = infer_type(stmts.last)
22697+ end
22698+ end
22699+ end
2247922700 if block_ret == "string"
2248022701 @needs_str_array = 1
2248122702 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
2248222703 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2248322704 if bp_is_lambda == 1
22484- declare_var(bp1, "lambda")
22485- emit(" lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");")
22705+ emit(" sp_Val * lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2248622706 else
22487- emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
22707+ emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2248822708 end
2248922709 @indent = @indent + 1
2249022710 blk2 = @nd_block[nid]
@@ -22501,15 +22721,15 @@
2250122721 end
2250222722 @indent = @indent - 1
2250322723 emit(" }")
22724+ pop_scope
2250422725 return tmp_arr
2250522726 else
2250622727 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
2250722728 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2250822729 if bp_is_lambda == 1
22509- declare_var(bp1, "lambda")
22510- emit(" lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");")
22730+ emit(" sp_Val * lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2251122731 else
22512- emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
22732+ emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2251322733 end
2251422734 @indent = @indent + 1
2251522735 blk2 = @nd_block[nid]
@@ -22526,6 +22746,7 @@
2252622746 end
2252722747 @indent = @indent - 1
2252822748 emit(" }")
22749+ pop_scope
2252922750 return tmp_arr
2253022751 end
2253122752 end
@@ -22534,6 +22755,8 @@
2253422755 # `tt = foo.map { ... }` silently became `lv_tt = 0` and the
2253522756 # subsequent iteration crashed. Issue #43.
2253622757 @needs_gc = 1
22758+ push_scope
22759+ declare_var(bp1, "string")
2253722760 block_ret = "string"
2253822761 blk = @nd_block[nid]
2253922762 if blk >= 0
@@ -22545,7 +22768,6 @@
2254522768 end
2254622769 end
2254722770 end
22548- declare_var(bp1, "string")
2254922771 if block_ret == "int"
2255022772 @needs_int_array = 1
2255122773 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
@@ -22573,6 +22795,7 @@
2257322795 end
2257422796 @indent = @indent - 1
2257522797 emit(" }")
22798+ pop_scope
2257622799 return tmp_arr
2257722800 end
2257822801 @needs_str_array = 1
@@ -22598,6 +22821,7 @@
2259822821 end
2259922822 @indent = @indent - 1
2260022823 emit(" }")
22824+ pop_scope
2260122825 return tmp_arr
2260222826 end
2260322827 "0"
@@ -22615,10 +22839,13 @@
2261522839 if rt == "int_array" || rt == "sym_array"
2261622840 @needs_int_array = 1
2261722841 @needs_gc = 1
22842+ bp_t = elem_type_of_array(rt)
2261822843 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
2261922844 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2262022845 emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2262122846 @indent = @indent + 1
22847+ push_scope
22848+ declare_var(bp1, bp_t)
2262222849 blk = @nd_block[nid]
2262322850 if blk >= 0
2262422851 body = @nd_body[blk]
@@ -22631,6 +22858,7 @@
2263122858 end
2263222859 end
2263322860 end
22861+ pop_scope
2263422862 @indent = @indent - 1
2263522863 emit(" }")
2263622864 return tmp_arr
@@ -22662,10 +22890,14 @@
2266222890 emit(" lv_" + bp1 + " = " + init_val + ";")
2266322891 rt = infer_type(@nd_receiver[nid])
2266422892 pfx = array_c_prefix(rt)
22893+ elem_t = elem_type_of_array(rt)
2266522894 tmp = new_temp
2266622895 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {")
22667- emit(" " + c_type(elem_type_of_array(rt)) + " lv_" + bp2 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
22896+ emit(" " + c_type(elem_t) + " lv_" + bp2 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
2266822897 @indent = @indent + 1
22898+ push_scope
22899+ declare_var(bp1, elem_t)
22900+ declare_var(bp2, elem_t)
2266922901 blk = @nd_block[nid]
2267022902 if blk >= 0
2267122903 body = @nd_body[blk]
@@ -22678,6 +22910,7 @@
2267822910 end
2267922911 end
2268022912 end
22913+ pop_scope
2268122914 @indent = @indent - 1
2268222915 emit(" }")
2268322916 end
@@ -22691,12 +22924,15 @@
2269122924 rt = infer_type(@nd_receiver[nid])
2269222925 if rt == "int_array" || rt == "sym_array"
2269322926 @needs_int_array = 1
22927+ bp_t = elem_type_of_array(rt)
2269422928 tmp_arr = new_temp
2269522929 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
2269622930 tmp_i = new_temp
2269722931 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2269822932 emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2269922933 @indent = @indent + 1
22934+ push_scope
22935+ declare_var(bp1, bp_t)
2270022936 blk = @nd_block[nid]
2270122937 if blk >= 0
2270222938 body = @nd_body[blk]
@@ -22709,6 +22945,7 @@
2270922945 end
2271022946 end
2271122947 end
22948+ pop_scope
2271222949 @indent = @indent - 1
2271322950 emit(" }")
2271422951 return tmp_arr
@@ -22731,6 +22968,8 @@
2273122968 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2273222969 emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2273322970 @indent = @indent + 1
22971+ push_scope
22972+ declare_var(bp1, "int")
2273422973 blk = @nd_block[nid]
2273522974 if blk >= 0
2273622975 body = @nd_body[blk]
@@ -22743,6 +22982,7 @@
2274322982 end
2274422983 end
2274522984 end
22985+ pop_scope
2274622986 @indent = @indent - 1
2274722987 emit(" }")
2274822988 end
@@ -23534,7 +23774,10 @@
2353423774 bpn = get_block_param(nid, 0)
2353523775 tmp_arrn = new_temp
2353623776 tmp_in = new_temp
23537- # infer block result type
23777+ push_scope
23778+ if bpn != ""
23779+ declare_var(bpn, "int")
23780+ end
2353823781 res_type = "int"
2353923782 blk_n = @nd_block[nid]
2354023783 if blk_n >= 0
@@ -23582,6 +23825,7 @@
2358223825 end
2358323826 @indent = @indent - 1
2358423827 emit(" }")
23828+ pop_scope
2358523829 @in_loop = old
2358623830 return
2358723831 end
@@ -23598,6 +23842,8 @@
2359823842 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2359923843 emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2360023844 @indent = @indent + 1
23845+ push_scope
23846+ declare_var(bp1, "int")
2360123847 blk = @nd_block[nid]
2360223848 if blk >= 0
2360323849 body = @nd_body[blk]
@@ -23610,6 +23856,7 @@
2361023856 end
2361123857 end
2361223858 end
23859+ pop_scope
2361323860 @indent = @indent - 1
2361423861 emit(" }")
2361523862 end
@@ -23619,6 +23866,8 @@
2361923866 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")
2362023867 emit(" const char *lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")
2362123868 @indent = @indent + 1
23869+ push_scope
23870+ declare_var(bp1, "string")
2362223871 blk = @nd_block[nid]
2362323872 if blk >= 0
2362423873 body = @nd_body[blk]
@@ -23631,6 +23880,7 @@
2363123880 end
2363223881 end
2363323882 end
23883+ pop_scope
2363423884 @indent = @indent - 1
2363523885 emit(" }")
2363623886 end
@@ -23651,10 +23901,13 @@
2365123901 tmp_arr = new_temp
2365223902 tmp_i = new_temp
2365323903 if rt == "int_array" || rt == "sym_array"
23904+ bp_t = elem_type_of_array(rt)
2365423905 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
2365523906 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2365623907 emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2365723908 @indent = @indent + 1
23909+ push_scope
23910+ declare_var(bp1, bp_t)
2365823911 blk = @nd_block[nid]
2365923912 if blk >= 0
2366023913 body = @nd_body[blk]
@@ -23667,6 +23920,7 @@
2366723920 end
2366823921 end
2366923922 end
23923+ pop_scope
2367023924 @indent = @indent - 1
2367123925 emit(" }")
2367223926 end