Commit 4bd54491

4bd54491f634Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Merge pull request #115 from dak2/fix/block-param-shadow-inference

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

Details

Commit
4bd54491f634
Parent
c1b9d1054ca9
Merge parent
0598908af5fa

spinel_codegen.rbmodifiedView file

@@ -2025,7 +2025,9 @@
20252025 return "int"
20262026 end
20272027 if mname == "then" || mname == "yield_self"
2028- # Return type is the block's return type
2028+ # Return type is the block's return type. Bind the block param to
2029+ # the receiver's type so infer_type sees the inner shadow, not any
2030+ # outer same-named local of a different type.
20292031 if recv >= 0
20302032 blk = @nd_block[nid]
20312033 if blk >= 0
@@ -2033,7 +2035,16 @@
20332035 if bbody >= 0
20342036 bbs = get_stmts(bbody)
20352037 if bbs.length > 0
2036- return infer_type(bbs.last)
2038+ bp = get_block_param(nid, 0)
2039+ if bp == ""
2040+ bp = "_x"
2041+ end
2042+ recv_t = infer_type(recv)
2043+ push_scope
2044+ declare_var(bp, recv_t)
2045+ rt = infer_type(bbs.last)
2046+ pop_scope
2047+ return rt
20372048 end
20382049 end
20392050 end
@@ -2572,14 +2583,21 @@
25722583 end
25732584 if mname == "map"
25742585 if recv >= 0
2575- # Determine result array type from block return type
2586+ # Declare bp inside a scope so infer_type sees the inner element type, not a shadowed outer local.
25762587 blk = @nd_block[nid]
25772588 if blk >= 0
25782589 bbody = @nd_body[blk]
25792590 if bbody >= 0
25802591 bbs = get_stmts(bbody)
25812592 if bbs.length > 0
2593+ recv_t = infer_type(recv)
2594+ bp1 = get_block_param(nid, 0)
2595+ push_scope
2596+ if bp1 != ""
2597+ declare_var(bp1, iter_elem_type(recv_t))
2598+ end
25822599 bret = infer_type(bbs.last)
2600+ pop_scope
25832601 if bret == "string"
25842602 return "str_array"
25852603 end
@@ -15395,7 +15413,6 @@
1539515413 bp1 = "_x"
1539615414 end
1539715415 et = elem_type_of_array(recv_type)
15398- declare_var(bp1, et)
1539915416 tmp_t = new_temp
1540015417 tmp_f = new_temp
1540115418 tmp_res = new_temp
@@ -15403,7 +15420,9 @@
1540315420 emit(" " + c_type(recv_type) + " " + tmp_t + " = sp_" + pfx + "_new();")
1540415421 emit(" " + c_type(recv_type) + " " + tmp_f + " = sp_" + pfx + "_new();")
1540515422 emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_" + pfx + "_length(" + rc + "); " + itmp + "++) {")
15406- emit(" lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + itmp + ");")
15423+ emit(" " + c_type(et) + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + itmp + ");")
15424+ push_scope
15425+ declare_var(bp1, et)
1540715426 blk = @nd_block[nid]
1540815427 bexpr = "0"
1540915428 if @nd_body[blk] >= 0
@@ -15418,6 +15437,7 @@
1541815437 end
1541915438 end
1542015439 emit(" if (" + bexpr + ") sp_" + pfx + "_push(" + tmp_t + ", lv_" + bp1 + "); else sp_" + pfx + "_push(" + tmp_f + ", lv_" + bp1 + ");")
15440+ pop_scope
1542115441 emit(" }")
1542215442 emit(" " + name + " *" + tmp_res + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(tt) + ");")
1542315443 emit(" " + tmp_res + "->_0 = " + tmp_t + ";")
@@ -16022,11 +16042,12 @@
1602216042 if @nd_block[nid] >= 0
1602316043 blk = @nd_block[nid]
1602416044 bp = get_block_param(nid, 0)
16025- declare_var(bp, "int")
1602616045 tmp = new_temp
1602716046 emit(" sp_StrIntHash *" + tmp + " = sp_StrIntHash_new();")
1602816047 emit(" for (mrb_int _i = 0; _i < " + rc + "->len; _i++) {")
1602916048 emit(" mrb_int lv_" + bp + " = sp_StrIntHash_get(" + rc + ", " + rc + "->order[_i]);")
16049+ push_scope
16050+ declare_var(bp, "int")
1603016051 bbody = @nd_body[blk]
1603116052 bexpr = "0"
1603216053 if bbody >= 0
@@ -16036,6 +16057,7 @@
1603616057 end
1603716058 end
1603816059 emit(" sp_StrIntHash_set(" + tmp + ", " + rc + "->order[_i], " + bexpr + ");")
16060+ pop_scope
1603916061 emit(" }")
1604016062 return tmp
1604116063 end
@@ -19539,7 +19561,6 @@
1953919561 if bp == ""
1954019562 bp = "_c"
1954119563 end
19542- declare_var(bp, "string")
1954319564 tmp = new_temp
1954419565 src = rc
1954519566 if rt == "mutable_str"
@@ -19554,9 +19575,12 @@
1955419575 emit(" char *" + char_buf + " = sp_str_alloc_raw(" + cn_tmp + " + 1);")
1955519576 emit(" memcpy(" + char_buf + ", " + src_tmp + " + " + tmp + ", " + cn_tmp + ");")
1955619577 emit(" " + char_buf + "[" + cn_tmp + "] = 0;")
19557- emit(" lv_" + bp + " = " + char_buf + ";")
19578+ emit(" const char *lv_" + bp + " = " + char_buf + ";")
1955819579 @indent = @indent + 1
19580+ push_scope
19581+ declare_var(bp, "string")
1955919582 compile_stmts_body(@nd_body[@nd_block[nid]])
19583+ pop_scope
1956019584 @indent = @indent - 1
1956119585 emit(" " + tmp + " += " + cn_tmp + ";")
1956219586 emit(" }")
@@ -19574,7 +19598,6 @@
1957419598 if bp == ""
1957519599 bp = "_b"
1957619600 end
19577- declare_var(bp, "int")
1957819601 tmp = new_temp
1957919602 src = rc
1958019603 if rt == "mutable_str"
@@ -19583,9 +19606,12 @@
1958319606 src_tmp = new_temp
1958419607 emit(" const char *" + src_tmp + " = " + src + ";")
1958519608 emit(" for (mrb_int " + tmp + " = 0; " + src_tmp + "[" + tmp + "]; " + tmp + "++) {")
19586- emit(" lv_" + bp + " = (unsigned char)" + src_tmp + "[" + tmp + "];")
19609+ emit(" mrb_int lv_" + bp + " = (unsigned char)" + src_tmp + "[" + tmp + "];")
1958719610 @indent = @indent + 1
19611+ push_scope
19612+ declare_var(bp, "int")
1958819613 compile_stmts_body(@nd_body[@nd_block[nid]])
19614+ pop_scope
1958919615 @indent = @indent - 1
1959019616 emit(" }")
1959119617 return 1
@@ -19602,7 +19628,6 @@
1960219628 if bp == ""
1960319629 bp = "_l"
1960419630 end
19605- declare_var(bp, "string")
1960619631 @needs_str_array = 1
1960719632 tmp_arr = new_temp
1960819633 tmp_i = new_temp
@@ -19612,9 +19637,12 @@
1961219637 end
1961319638 emit(" sp_StrArray *" + tmp_arr + " = sp_str_split(" + src + ", \"\\n\");")
1961419639 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < " + tmp_arr + "->len; " + tmp_i + "++) {")
19615- emit(" lv_" + bp + " = " + tmp_arr + "->data[" + tmp_i + "];")
19640+ emit(" const char *lv_" + bp + " = " + tmp_arr + "->data[" + tmp_i + "];")
1961619641 @indent = @indent + 1
19642+ push_scope
19643+ declare_var(bp, "string")
1961719644 compile_stmts_body(@nd_body[@nd_block[nid]])
19645+ pop_scope
1961819646 @indent = @indent - 1
1961919647 emit(" }")
1962019648 return 1
@@ -19652,13 +19680,18 @@
1965219680 bp2 = "_b"
1965319681 end
1965419682 pfx = array_c_prefix(rt)
19655- et = c_type(elem_type_of_array(rt))
19683+ elem_t = elem_type_of_array(rt)
19684+ et = c_type(elem_t)
1965619685 tmp_i = new_temp
1965719686 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")
1965819687 emit(" " + et + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")
1965919688 emit(" " + et + " lv_" + bp2 + " = sp_" + pfx + "_get(" + arg + ", " + tmp_i + ");")
1966019689 @indent = @indent + 1
19690+ push_scope
19691+ declare_var(bp1, elem_t)
19692+ declare_var(bp2, elem_t)
1966119693 compile_stmts_body(@nd_body[@nd_block[nid]])
19694+ pop_scope
1966219695 @indent = @indent - 1
1966319696 emit(" }")
1966419697 @in_loop = old
@@ -19699,7 +19732,10 @@
1969919732 end
1970019733 emit(" for (lv_" + bp1 + " = " + rc + "; lv_" + bp1 + " <= " + limit_val + "; lv_" + bp1 + " += " + step_val + ") {")
1970119734 @indent = @indent + 1
19735+ push_scope
19736+ declare_var(bp1, "int")
1970219737 compile_stmts_body(@nd_body[@nd_block[nid]])
19738+ pop_scope
1970319739 @indent = @indent - 1
1970419740 emit(" }")
1970519741 if synth == 1
@@ -19722,13 +19758,17 @@
1972219758 bp1 = "_x"
1972319759 end
1972419760 pfx = array_c_prefix(rt)
19761+ et = elem_type_of_array(rt)
1972519762 tmp_c = new_temp
1972619763 tmp_i = new_temp
1972719764 emit(" for (mrb_int " + tmp_c + " = 0; " + tmp_c + " < " + n + "; " + tmp_c + "++)")
1972819765 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")
19729- emit(" lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")
19766+ emit(" " + c_type(et) + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")
1973019767 @indent = @indent + 1
19768+ push_scope
19769+ declare_var(bp1, et)
1973119770 compile_stmts_body(@nd_body[@nd_block[nid]])
19771+ pop_scope
1973219772 @indent = @indent - 1
1973319773 emit(" }")
1973419774 @in_loop = old
@@ -19757,12 +19797,14 @@
1975719797 tmp_i = new_temp
1975819798 emit(" sp_StrArray *" + tmp_arr + " = sp_re_scan(sp_re_pat_" + ridx.to_s + ", " + rc + ");")
1975919799 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < " + tmp_arr + "->len; " + tmp_i + "++) {")
19760- set_var_type(bp, "string")
19761- emit(" lv_" + bp + " = " + tmp_arr + "->data[" + tmp_i + "];")
19800+ emit(" const char *lv_" + bp + " = " + tmp_arr + "->data[" + tmp_i + "];")
19801+ push_scope
19802+ declare_var(bp, "string")
1976219803 blk = @nd_block[nid]
1976319804 if @nd_body[blk] >= 0
1976419805 compile_stmts_body(@nd_body[blk])
1976519806 end
19807+ pop_scope
1976619808 emit(" }")
1976719809 return 1
1976819810 end
@@ -20118,11 +20160,12 @@
2011820160 if @nd_block[nid] >= 0
2011920161 lblk = @nd_block[nid]
2012020162 lbp = get_block_param(nid, 0)
20121- declare_var(lbp, "string")
2012220163 ltmp = new_temp
2012320164 emit(" { char " + ltmp + "[4096];")
2012420165 emit(" while (fgets(" + ltmp + ", sizeof(" + ltmp + "), " + ftmp + ")) {")
2012520166 emit(" const char *lv_" + lbp + " = " + ltmp + ";")
20167+ push_scope
20168+ declare_var(lbp, "string")
2012620169 # Compile block body
2012720170 lbbody = @nd_body[lblk]
2012820171 if lbbody >= 0
@@ -20133,6 +20176,7 @@
2013320176 lbk = lbk + 1
2013420177 end
2013520178 end
20179+ pop_scope
2013620180 emit(" } }")
2013720181 return
2013820182 end
@@ -21565,15 +21609,28 @@
2156521609 tmp_j = new_temp
2156621610 tmp_len = new_temp
2156721611 pfx = array_c_prefix(rt)
21568- declare_var(bp1, rt)
2156921612 @needs_gc = 1
2157021613 emit(" mrb_int " + tmp_len + " = sp_" + pfx + "_length(" + rc + ");")
2157121614 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < " + tmp_len + "; " + tmp_i + " += " + n + ") {")
21572- emit(" lv_" + bp1 + " = sp_" + pfx + "_new();")
21615+ # When bp1 shadows an outer same-named local of a different C type, the
21616+ # function-level lv_<bp1> has the outer type. Emit a block-scoped fresh
21617+ # declaration with its own GC root so the slice survives allocations in
21618+ # the user block body.
21619+ outer_t = find_var_type(bp1)
21620+ if outer_t != "" && outer_t != rt
21621+ emit(" SP_GC_SAVE();")
21622+ emit(" " + c_type(rt) + " lv_" + bp1 + " = sp_" + pfx + "_new();")
21623+ emit(" SP_GC_ROOT(lv_" + bp1 + ");")
21624+ else
21625+ emit(" lv_" + bp1 + " = sp_" + pfx + "_new();")
21626+ end
2157321627 emit(" for (mrb_int " + tmp_j + " = 0; " + tmp_j + " < " + n + " && " + tmp_i + " + " + tmp_j + " < " + tmp_len + "; " + tmp_j + "++)")
2157421628 emit(" sp_" + pfx + "_push(lv_" + bp1 + ", sp_" + pfx + "_get(" + rc + ", " + tmp_i + " + " + tmp_j + "));")
2157521629 @indent = @indent + 1
21630+ push_scope
21631+ declare_var(bp1, rt)
2157621632 compile_stmts_body(@nd_body[@nd_block[nid]])
21633+ pop_scope
2157721634 @indent = @indent - 1
2157821635 emit(" }")
2157921636 @in_loop = old
@@ -21593,15 +21650,26 @@
2159321650 tmp_j = new_temp
2159421651 tmp_len = new_temp
2159521652 pfx = array_c_prefix(rt)
21596- declare_var(bp1, rt)
2159721653 @needs_gc = 1
2159821654 emit(" mrb_int " + tmp_len + " = sp_" + pfx + "_length(" + rc + ");")
2159921655 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " + " + n + " <= " + tmp_len + "; " + tmp_i + "++) {")
21600- emit(" lv_" + bp1 + " = sp_" + pfx + "_new();")
21656+ # See compile_each_slice_block: shadow case needs a fresh typed slot with
21657+ # its own GC root.
21658+ outer_t = find_var_type(bp1)
21659+ if outer_t != "" && outer_t != rt
21660+ emit(" SP_GC_SAVE();")
21661+ emit(" " + c_type(rt) + " lv_" + bp1 + " = sp_" + pfx + "_new();")
21662+ emit(" SP_GC_ROOT(lv_" + bp1 + ");")
21663+ else
21664+ emit(" lv_" + bp1 + " = sp_" + pfx + "_new();")
21665+ end
2160121666 emit(" for (mrb_int " + tmp_j + " = 0; " + tmp_j + " < " + n + "; " + tmp_j + "++)")
2160221667 emit(" sp_" + pfx + "_push(lv_" + bp1 + ", sp_" + pfx + "_get(" + rc + ", " + tmp_i + " + " + tmp_j + "));")
2160321668 @indent = @indent + 1
21669+ push_scope
21670+ declare_var(bp1, rt)
2160421671 compile_stmts_body(@nd_body[@nd_block[nid]])
21672+ pop_scope
2160521673 @indent = @indent - 1
2160621674 emit(" }")
2160721675 @in_loop = old
@@ -21652,7 +21720,10 @@
2165221720 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")
2165321721 emit(" " + c_type(elem_type_of_array(rt)) + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")
2165421722 @indent = @indent + 1
21723+ push_scope
21724+ declare_var(bp1, elem_type_of_array(rt))
2165521725 compile_stmts_body(@nd_body[@nd_block[nid]])
21726+ pop_scope
2165621727 @indent = @indent - 1
2165721728 emit(" }")
2165821729 emit(" " + result + " = lv_" + bp2 + ";")
@@ -21682,7 +21753,11 @@
2168221753 emit(" " + c_type(elem_type_of_array(rt)) + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
2168321754 emit(" mrb_int lv_" + bp2 + " = " + tmp + ";")
2168421755 @indent = @indent + 1
21756+ push_scope
21757+ declare_var(bp1, elem_type_of_array(rt))
21758+ declare_var(bp2, "int")
2168521759 compile_stmts_body(@nd_body[@nd_block[nid]])
21760+ pop_scope
2168621761 @indent = @indent - 1
2168721762 emit(" }")
2168821763 @in_loop = old
@@ -21709,7 +21784,12 @@
2170921784 emit(" " + c_type(elem_type_of_array(rt)) + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
2171021785 end
2171121786 @indent = @indent + 1
21787+ push_scope
21788+ if has_bp == 1
21789+ declare_var(bp1, elem_type_of_array(rt))
21790+ end
2171221791 compile_stmts_body(@nd_body[@nd_block[nid]])
21792+ pop_scope
2171321793 @indent = @indent - 1
2171421794 emit(" }")
2171521795 end
@@ -21720,7 +21800,12 @@
2172021800 emit(" lv_" + bp1 + " = (sp_sym)sp_IntArray_get(" + rc + ", " + tmp + ");")
2172121801 end
2172221802 @indent = @indent + 1
21803+ push_scope
21804+ if has_bp == 1
21805+ declare_var(bp1, "symbol")
21806+ end
2172321807 compile_stmts_body(@nd_body[@nd_block[nid]])
21808+ pop_scope
2172421809 @indent = @indent - 1
2172521810 emit(" }")
2172621811 end
@@ -21734,7 +21819,12 @@
2173421819 emit(" lv_" + bp1 + " = " + bp_tmp + ";")
2173521820 end
2173621821 @indent = @indent + 1
21822+ push_scope
21823+ if has_bp == 1
21824+ declare_var(bp1, elem_type)
21825+ end
2173721826 compile_stmts_body(@nd_body[@nd_block[nid]])
21827+ pop_scope
2173821828 @indent = @indent - 1
2173921829 emit(" }")
2174021830 end
@@ -21746,7 +21836,13 @@
2174621836 emit(" lv_" + bp2 + " = sp_StrIntHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2174721837 end
2174821838 @indent = @indent + 1
21839+ push_scope
21840+ declare_var(bp1, "string")
21841+ if bp2 != ""
21842+ declare_var(bp2, "int")
21843+ end
2174921844 compile_stmts_body(@nd_body[@nd_block[nid]])
21845+ pop_scope
2175021846 @indent = @indent - 1
2175121847 emit(" }")
2175221848 end
@@ -21758,7 +21854,13 @@
2175821854 emit(" lv_" + bp2 + " = sp_IntStrHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2175921855 end
2176021856 @indent = @indent + 1
21857+ push_scope
21858+ declare_var(bp1, "int")
21859+ if bp2 != ""
21860+ declare_var(bp2, "string")
21861+ end
2176121862 compile_stmts_body(@nd_body[@nd_block[nid]])
21863+ pop_scope
2176221864 @indent = @indent - 1
2176321865 emit(" }")
2176421866 end
@@ -21770,7 +21872,13 @@
2177021872 emit(" lv_" + bp2 + " = sp_StrStrHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2177121873 end
2177221874 @indent = @indent + 1
21875+ push_scope
21876+ declare_var(bp1, "string")
21877+ if bp2 != ""
21878+ declare_var(bp2, "string")
21879+ end
2177321880 compile_stmts_body(@nd_body[@nd_block[nid]])
21881+ pop_scope
2177421882 @indent = @indent - 1
2177521883 emit(" }")
2177621884 end
@@ -21782,7 +21890,13 @@
2178221890 emit(" lv_" + bp2 + " = sp_SymIntHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2178321891 end
2178421892 @indent = @indent + 1
21893+ push_scope
21894+ declare_var(bp1, "symbol")
21895+ if bp2 != ""
21896+ declare_var(bp2, "int")
21897+ end
2178521898 compile_stmts_body(@nd_body[@nd_block[nid]])
21899+ pop_scope
2178621900 @indent = @indent - 1
2178721901 emit(" }")
2178821902 end
@@ -21794,7 +21908,13 @@
2179421908 emit(" lv_" + bp2 + " = sp_SymStrHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2179521909 end
2179621910 @indent = @indent + 1
21911+ push_scope
21912+ declare_var(bp1, "symbol")
21913+ if bp2 != ""
21914+ declare_var(bp2, "string")
21915+ end
2179721916 compile_stmts_body(@nd_body[@nd_block[nid]])
21917+ pop_scope
2179821918 @indent = @indent - 1
2179921919 emit(" }")
2180021920 end
@@ -21806,7 +21926,13 @@
2180621926 emit(" lv_" + bp2 + " = sp_SymPolyHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2180721927 end
2180821928 @indent = @indent + 1
21929+ push_scope
21930+ declare_var(bp1, "symbol")
21931+ if bp2 != ""
21932+ declare_var(bp2, "poly")
21933+ end
2180921934 compile_stmts_body(@nd_body[@nd_block[nid]])
21935+ pop_scope
2181021936 @indent = @indent - 1
2181121937 emit(" }")
2181221938 end
@@ -21818,7 +21944,13 @@
2181821944 emit(" lv_" + bp2 + " = sp_StrPolyHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
2181921945 end
2182021946 @indent = @indent + 1
21947+ push_scope
21948+ declare_var(bp1, "string")
21949+ if bp2 != ""
21950+ declare_var(bp2, "poly")
21951+ end
2182121952 compile_stmts_body(@nd_body[@nd_block[nid]])
21953+ pop_scope
2182221954 @indent = @indent - 1
2182321955 emit(" }")
2182421956 end
@@ -21828,7 +21960,12 @@
2182821960 emit(" sp_Range " + tmp2 + " = " + rc + ";")
2182921961 emit(" for (lv_" + bp1 + " = " + tmp2 + ".first; lv_" + bp1 + " <= " + tmp2 + ".last; lv_" + bp1 + "++) {")
2183021962 @indent = @indent + 1
21963+ push_scope
21964+ if has_bp == 1
21965+ declare_var(bp1, "int")
21966+ end
2183121967 compile_stmts_body(@nd_body[@nd_block[nid]])
21968+ pop_scope
2183221969 @indent = @indent - 1
2183321970 emit(" }")
2183421971 end
@@ -21837,7 +21974,12 @@
2183721974 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_PolyArray_length(" + rc + "); " + tmp + "++) {")
2183821975 emit(" lv_" + bp1 + " = sp_PolyArray_get(" + rc + ", " + tmp + ");")
2183921976 @indent = @indent + 1
21977+ push_scope
21978+ if has_bp == 1
21979+ declare_var(bp1, "poly")
21980+ end
2184021981 compile_stmts_body(@nd_body[@nd_block[nid]])
21982+ pop_scope
2184121983 @indent = @indent - 1
2184221984 emit(" }")
2184321985 end
@@ -21855,7 +21997,12 @@
2185521997 emit(" lv_" + bp1 + " = " + tmp + ";")
2185621998 end
2185721999 @indent = @indent + 1
22000+ push_scope
22001+ if bp1 != ""
22002+ declare_var(bp1, "int")
22003+ end
2185822004 compile_stmts_body(@nd_body[@nd_block[nid]])
22005+ pop_scope
2185922006 @indent = @indent - 1
2186022007 emit(" }")
2186122008 @in_loop = old
@@ -21873,7 +22020,12 @@
2187322020 emit(" lv_" + bp1 + " = " + tmp + ";")
2187422021 end
2187522022 @indent = @indent + 1
22023+ push_scope
22024+ if bp1 != ""
22025+ declare_var(bp1, "int")
22026+ end
2187622027 compile_stmts_body(@nd_body[@nd_block[nid]])
22028+ pop_scope
2187722029 @indent = @indent - 1
2187822030 emit(" }")
2187922031 @in_loop = old
@@ -21891,24 +22043,34 @@
2189122043 emit(" lv_" + bp1 + " = " + tmp + ";")
2189222044 end
2189322045 @indent = @indent + 1
22046+ push_scope
22047+ if bp1 != ""
22048+ declare_var(bp1, "int")
22049+ end
2189422050 compile_stmts_body(@nd_body[@nd_block[nid]])
22051+ pop_scope
2189522052 @indent = @indent - 1
2189622053 emit(" }")
2189722054 @in_loop = old
2189822055 end
2189922056
2190022057 def compile_tap_expr(nid)
21901- # Execute block with receiver bound to block param, return receiver
22058+ # Execute block with receiver bound to block param, return receiver.
22059+ # Open a C block so the param is a fresh local that shadows any
22060+ # outer same-named lv_<bp> without clobbering its value or type.
2190222061 rt = infer_type(@nd_receiver[nid])
2190322062 rc = compile_expr_gc_rooted(@nd_receiver[nid])
2190422063 bp = get_block_param(nid, 0)
2190522064 if bp == ""
2190622065 bp = "_x"
2190722066 end
21908- declare_var(bp, rt)
2190922067 tmp = new_temp
2191022068 emit(" " + c_type(rt) + " " + tmp + " = " + rc + ";")
21911- emit(" lv_" + bp + " = " + tmp + ";")
22069+ emit(" {")
22070+ emit(" " + c_type(rt) + " lv_" + bp + " = " + tmp + ";")
22071+ @indent = @indent + 1
22072+ push_scope
22073+ declare_var(bp, rt)
2191222074 blk = @nd_block[nid]
2191322075 bbody = @nd_body[blk]
2191422076 if bbody >= 0
@@ -21919,33 +22081,62 @@
2191922081 k = k + 1
2192022082 end
2192122083 end
22084+ pop_scope
22085+ @indent = @indent - 1
22086+ emit(" }")
2192222087 tmp
2192322088 end
2192422089
2192522090 def compile_then_expr(nid)
21926- # Execute block with receiver bound to block param, return block result
22091+ # Execute block with receiver bound to block param, return block result.
22092+ # Open a C block so the param is a fresh local that shadows any outer
22093+ # same-named lv_<bp>. The block's last-expression value is funneled
22094+ # through a result tmp declared in the enclosing scope so callers can
22095+ # still consume it after the C block closes.
2192722096 rt = infer_type(@nd_receiver[nid])
2192822097 rc = compile_expr_gc_rooted(@nd_receiver[nid])
2192922098 bp = get_block_param(nid, 0)
2193022099 if bp == ""
2193122100 bp = "_x"
2193222101 end
21933- declare_var(bp, rt)
21934- emit(" lv_" + bp + " = " + rc + ";")
2193522102 blk = @nd_block[nid]
2193622103 bbody = @nd_body[blk]
22104+
22105+ # Peek at the last expression's type under the inner binding so the
22106+ # result tmp is declared with the type that infer_type sees inside
22107+ # the block, not the type of any outer same-named local.
22108+ ret_t = "int"
22109+ bs = []
2193722110 if bbody >= 0
2193822111 bs = get_stmts(bbody)
22112+ if bs.length > 0
22113+ push_scope
22114+ declare_var(bp, rt)
22115+ ret_t = infer_type(bs.last)
22116+ pop_scope
22117+ end
22118+ end
22119+
22120+ result_tmp = new_temp
22121+ emit(" " + c_type(ret_t) + " " + result_tmp + " = " + c_default_val(ret_t) + ";")
22122+ emit(" {")
22123+ emit(" " + c_type(rt) + " lv_" + bp + " = " + rc + ";")
22124+ @indent = @indent + 1
22125+ push_scope
22126+ declare_var(bp, rt)
22127+ if bs.length > 0
2193922128 k = 0
2194022129 while k < bs.length - 1
2194122130 compile_stmt(bs[k])
2194222131 k = k + 1
2194322132 end
21944- if bs.length > 0
21945- return compile_expr(bs.last)
21946- end
22133+ last_expr = compile_expr(bs.last)
22134+ emit(" " + result_tmp + " = " + last_expr + ";")
2194722135 end
21948- "0"
22136+ pop_scope
22137+ @indent = @indent - 1
22138+ emit(" }")
22139+ result_tmp
2194922140 end
2195022141
2195122142 # Emit the loop-open lines for iterating over a receiver expression.
@@ -21979,11 +22170,12 @@
2197922170 if bp1 == ""
2198022171 bp1 = "_x"
2198122172 end
21982- declare_var(bp1, iter_elem_type(recv_type))
2198322173 tmp_sum = new_temp
2198422174 tmp_i = new_temp
2198522175 emit(" mrb_int " + tmp_sum + " = 0;")
2198622176 emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
22177+ push_scope
22178+ declare_var(bp1, iter_elem_type(recv_type))
2198722179 blk = @nd_block[nid]
2198822180 bexpr = "0"
2198922181 if @nd_body[blk] >= 0
@@ -21998,6 +22190,7 @@
2199822190 end
2199922191 end
2200022192 emit(" " + tmp_sum + " += " + bexpr + ";")
22193+ pop_scope
2200122194 emit(" }")
2200222195 tmp_sum
2200322196 end
@@ -22007,11 +22200,12 @@
2200722200 if bp1 == ""
2200822201 bp1 = "_x"
2200922202 end
22010- declare_var(bp1, iter_elem_type(recv_type))
2201122203 tmp_cnt = new_temp
2201222204 tmp_i = new_temp
2201322205 emit(" mrb_int " + tmp_cnt + " = 0;")
2201422206 emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
22207+ push_scope
22208+ declare_var(bp1, iter_elem_type(recv_type))
2201522209 blk = @nd_block[nid]
2201622210 bexpr = "0"
2201722211 if @nd_body[blk] >= 0
@@ -22026,6 +22220,7 @@
2202622220 end
2202722221 end
2202822222 emit(" if (" + bexpr + ") " + tmp_cnt + "++;")
22223+ pop_scope
2202922224 emit(" }")
2203022225 tmp_cnt
2203122226 end
@@ -22041,7 +22236,6 @@
2204122236 elsif recv_type == "float_array"
2204222237 elem_type = "float"
2204322238 end
22044- set_var_type(bp1, elem_type)
2204522239 tmp_res = new_temp
2204622240 tmp_key = new_temp
2204722241 tmp_i = new_temp
@@ -22051,6 +22245,8 @@
2205122245 emit(" " + c_type(elem_type) + " " + bp_tmp + " = " + c_default_val(elem_type) + ";")
2205222246 emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
2205322247 emit(" " + bp_tmp + " = lv_" + bp1 + ";")
22248+ push_scope
22249+ declare_var(bp1, elem_type)
2205422250 blk = @nd_block[nid]
2205522251 bexpr = "0"
2205622252 if @nd_body[blk] >= 0
@@ -22070,6 +22266,7 @@
2207022266 end
2207122267 emit(" mrb_int _k = " + bexpr + ";")
2207222268 emit(" if (" + tmp_i + " == 0 || _k " + cmp + " " + tmp_key + ") { " + tmp_res + " = " + bp_tmp + "; " + tmp_key + " = _k; }")
22269+ pop_scope
2207322270 emit(" }")
2207422271 tmp_res
2207522272 end
@@ -22079,8 +22276,9 @@
2207922276 if bp1 == ""
2208022277 bp1 = "_x"
2208122278 end
22082- declare_var(bp1, iter_elem_type(recv_type))
22083- # Determine result type from block return
22279+ elem_t = iter_elem_type(recv_type)
22280+ push_scope
22281+ declare_var(bp1, elem_t)
2208422282 blk = @nd_block[nid]
2208522283 block_ret = "int"
2208622284 if blk >= 0
@@ -22125,6 +22323,7 @@
2212522323 emit(" " + c_type(block_ret) + " " + tmp_val + " = " + bexpr + ";")
2212622324 emit(" if (" + tmp_val + ") sp_" + pfx_dst + "_push(" + tmp_arr + ", " + tmp_val + ");")
2212722325 @indent = @indent - 1
22326+ pop_scope
2212822327 emit(" }")
2212922328 tmp_arr
2213022329 end
@@ -22135,11 +22334,12 @@
2213522334 bp1 = "_x"
2213622335 end
2213722336 elem_type = iter_elem_type(recv_type)
22138- declare_var(bp1, elem_type)
2213922337 tmp_res = new_temp
2214022338 tmp_i = new_temp
2214122339 emit(" " + c_type(elem_type) + " " + tmp_res + " = " + c_default_val(elem_type) + ";")
2214222340 emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
22341+ push_scope
22342+ declare_var(bp1, elem_type)
2214322343 blk = @nd_block[nid]
2214422344 bbody = @nd_body[blk]
2214522345 bexpr = "0"
@@ -22155,6 +22355,7 @@
2215522355 end
2215622356 end
2215722357 emit(" if (" + bexpr + ") { " + tmp_res + " = lv_" + bp1 + "; break; }")
22358+ pop_scope
2215822359 emit(" }")
2215922360 tmp_res
2216022361 end
@@ -22165,7 +22366,6 @@
2216522366 if bp1 == ""
2216622367 bp1 = "_x"
2216722368 end
22168- declare_var(bp1, iter_elem_type(recv_type))
2216922369 tmp_res = new_temp
2217022370 tmp_i = new_temp
2217122371 init_val = "FALSE"
@@ -22174,6 +22374,8 @@
2217422374 end
2217522375 emit(" mrb_bool " + tmp_res + " = " + init_val + ";")
2217622376 emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
22377+ push_scope
22378+ declare_var(bp1, iter_elem_type(recv_type))
2217722379 blk = @nd_block[nid]
2217822380 bbody = @nd_body[blk]
2217922381 bexpr = "0"
@@ -22198,6 +22400,7 @@
2219822400 # none?
2219922401 emit(" if (" + bexpr + ") { " + tmp_res + " = FALSE; break; }")
2220022402 end
22403+ pop_scope
2220122404 emit(" }")
2220222405 tmp_res
2220322406 end
@@ -22230,14 +22433,15 @@
2223022433 @needs_str_str_hash = 1
2223122434 end
2223222435 @needs_gc = 1
22233- declare_var(bp1, "string")
22234- declare_var(bp2, val_type)
2223522436 tmp = new_temp
2223622437 itmp = new_temp
2223722438 emit(" " + c_type(hash_type) + tmp + " = " + ctor + "();")
2223822439 emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")
2223922440 emit(" lv_" + bp1 + " = " + rc + "->order[" + itmp + "];")
2224022441 emit(" lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")
22442+ push_scope
22443+ declare_var(bp1, "string")
22444+ declare_var(bp2, val_type)
2224122445 blk = @nd_block[nid]
2224222446 bbody = @nd_body[blk]
2224322447 bexpr = "0"
@@ -22258,6 +22462,7 @@
2225822462 cond = "!(" + bexpr + ")"
2225922463 end
2226022464 emit(" if (" + cond + ") " + setter + "(" + tmp + ", lv_" + bp1 + ", lv_" + bp2 + ");")
22465+ pop_scope
2226122466 emit(" }")
2226222467 tmp
2226322468 end
@@ -22277,6 +22482,7 @@
2227722482 val_type = "string"
2227822483 getter = "sp_StrStrHash_get"
2227922484 end
22485+ push_scope
2228022486 declare_var(bp1, "string")
2228122487 declare_var(bp2, val_type)
2228222488 itmp = new_temp
@@ -22315,6 +22521,7 @@
2231522521 end
2231622522 emit(" if (" + bexpr + ") " + tmp_c + "++;")
2231722523 emit(" }")
22524+ pop_scope
2231822525 return tmp_c
2231922526 end
2232022527 if mname == "any?"
@@ -22336,6 +22543,7 @@
2233622543 end
2233722544 emit(" if (" + bexpr + ") { " + tmp_r + " = TRUE; break; }")
2233822545 emit(" }")
22546+ pop_scope
2233922547 return tmp_r
2234022548 end
2234122549 if mname == "all?"
@@ -22357,6 +22565,7 @@
2235722565 end
2235822566 emit(" if (!(" + bexpr + ")) { " + tmp_r + " = FALSE; break; }")
2235922567 emit(" }")
22568+ pop_scope
2236022569 return tmp_r
2236122570 end
2236222571 # find / detect — return key of first match
@@ -22379,8 +22588,10 @@
2237922588 end
2238022589 emit(" if (" + bexpr + ") { " + tmp_r + " = lv_" + bp1 + "; break; }")
2238122590 emit(" }")
22591+ pop_scope
2238222592 return tmp_r
2238322593 end
22594+ pop_scope
2238422595 "0"
2238522596 end
2238622597
@@ -22392,7 +22603,18 @@
2239222603 if bp1 == ""
2239322604 bp1 = "_x"
2239422605 end
22395- # Determine result array type from block return type
22606+ elem_type = "int"
22607+ if rt == "str_array"
22608+ elem_type = "string"
22609+ elsif rt == "float_array"
22610+ elem_type = "float"
22611+ elsif rt == "poly_array"
22612+ elem_type = "poly"
22613+ elsif is_ptr_array_type(rt) == 1
22614+ elem_type = ptr_array_elem_type(rt)
22615+ end
22616+ push_scope
22617+ declare_var(bp1, elem_type)
2239622618 blk = @nd_block[nid]
2239722619 block_ret = "int_array"
2239822620 if blk >= 0
@@ -22416,18 +22638,6 @@
2241622638 tmp_inner = new_temp
2241722639 tmp_j = new_temp
2241822640 emit(" " + c_type(block_ret) + tmp_arr + " = sp_" + pfx_dst + "_new();")
22419- # Declare block param type from receiver element type
22420- elem_type = "int"
22421- if rt == "str_array"
22422- elem_type = "string"
22423- elsif rt == "float_array"
22424- elem_type = "float"
22425- elsif rt == "poly_array"
22426- elem_type = "poly"
22427- elsif is_ptr_array_type(rt) == 1
22428- elem_type = ptr_array_elem_type(rt)
22429- end
22430- declare_var(bp1, elem_type)
2243122641 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx_src + "_length(" + rc + "); " + tmp_i + "++) {")
2243222642 emit(" lv_" + bp1 + " = sp_" + pfx_src + "_get(" + rc + ", " + tmp_i + ");")
2243322643 @indent = @indent + 1
@@ -22448,6 +22658,7 @@
2244822658 end
2244922659 @indent = @indent - 1
2245022660 emit(" }")
22661+ pop_scope
2245122662 tmp_arr
2245222663 end
2245322664
@@ -22460,6 +22671,10 @@
2246022671 bpn = get_block_param(nid, 0)
2246122672 res_type = "int"
2246222673 blk_n = @nd_block[nid]
22674+ push_scope
22675+ if bpn != ""
22676+ declare_var(bpn, "int")
22677+ end
2246322678 if blk_n >= 0
2246422679 body_n = @nd_body[blk_n]
2246522680 if body_n >= 0
@@ -22508,6 +22723,7 @@
2250822723 end
2250922724 @indent = @indent - 1
2251022725 emit(" }")
22726+ pop_scope
2251122727 return tmp_arrn
2251222728 end
2251322729 rt = infer_type(@nd_receiver[nid])
@@ -22525,19 +22741,9 @@
2252522741 if rt == "int_array" || rt == "sym_array"
2252622742 @needs_int_array = 1
2252722743 @needs_gc = 1
22528- # Check if block body returns string (for map that produces StrArray)
22529- block_ret = "int"
22744+ bp_t = elem_type_of_array(rt)
22745+ # Check if block param is used as lambda (elements are lambda pointers in IntArray)
2253022746 blk = @nd_block[nid]
22531- if blk >= 0
22532- body = @nd_body[blk]
22533- if body >= 0
22534- stmts = get_stmts(body)
22535- if stmts.length > 0
22536- block_ret = infer_type(stmts.last)
22537- end
22538- end
22539- end
22540- # Check if block param is used as lambda (elements are lambda pointers in IntArray)
2254122747 bp_is_lambda = 0
2254222748 if blk >= 0
2254322749 bp_is_lambda = param_used_as_lambda(bp1, @nd_body[blk])
@@ -22567,15 +22773,29 @@
2256722773 end
2256822774 end
2256922775 end
22776+ if bp_is_lambda == 1
22777+ bp_t = "lambda"
22778+ end
22779+ push_scope
22780+ declare_var(bp1, bp_t)
22781+ block_ret = "int"
22782+ if blk >= 0
22783+ body = @nd_body[blk]
22784+ if body >= 0
22785+ stmts = get_stmts(body)
22786+ if stmts.length > 0
22787+ block_ret = infer_type(stmts.last)
22788+ end
22789+ end
22790+ end
2257022791 if block_ret == "string"
2257122792 @needs_str_array = 1
2257222793 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
2257322794 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2257422795 if bp_is_lambda == 1
22575- declare_var(bp1, "lambda")
22576- emit(" lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");")
22796+ emit(" sp_Val * lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2257722797 else
22578- emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
22798+ emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2257922799 end
2258022800 @indent = @indent + 1
2258122801 blk2 = @nd_block[nid]
@@ -22592,15 +22812,15 @@
2259222812 end
2259322813 @indent = @indent - 1
2259422814 emit(" }")
22815+ pop_scope
2259522816 return tmp_arr
2259622817 else
2259722818 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
2259822819 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2259922820 if bp_is_lambda == 1
22600- declare_var(bp1, "lambda")
22601- emit(" lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");")
22821+ emit(" sp_Val * lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2260222822 else
22603- emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
22823+ emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2260422824 end
2260522825 @indent = @indent + 1
2260622826 blk2 = @nd_block[nid]
@@ -22617,6 +22837,7 @@
2261722837 end
2261822838 @indent = @indent - 1
2261922839 emit(" }")
22840+ pop_scope
2262022841 return tmp_arr
2262122842 end
2262222843 end
@@ -22625,6 +22846,8 @@
2262522846 # `tt = foo.map { ... }` silently became `lv_tt = 0` and the
2262622847 # subsequent iteration crashed. Issue #43.
2262722848 @needs_gc = 1
22849+ push_scope
22850+ declare_var(bp1, "string")
2262822851 block_ret = "string"
2262922852 blk = @nd_block[nid]
2263022853 if blk >= 0
@@ -22636,7 +22859,6 @@
2263622859 end
2263722860 end
2263822861 end
22639- declare_var(bp1, "string")
2264022862 if block_ret == "int"
2264122863 @needs_int_array = 1
2264222864 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
@@ -22664,6 +22886,7 @@
2266422886 end
2266522887 @indent = @indent - 1
2266622888 emit(" }")
22889+ pop_scope
2266722890 return tmp_arr
2266822891 end
2266922892 @needs_str_array = 1
@@ -22689,6 +22912,7 @@
2268922912 end
2269022913 @indent = @indent - 1
2269122914 emit(" }")
22915+ pop_scope
2269222916 return tmp_arr
2269322917 end
2269422918 "0"
@@ -22706,10 +22930,13 @@
2270622930 if rt == "int_array" || rt == "sym_array"
2270722931 @needs_int_array = 1
2270822932 @needs_gc = 1
22933+ bp_t = elem_type_of_array(rt)
2270922934 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
2271022935 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2271122936 emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2271222937 @indent = @indent + 1
22938+ push_scope
22939+ declare_var(bp1, bp_t)
2271322940 blk = @nd_block[nid]
2271422941 if blk >= 0
2271522942 body = @nd_body[blk]
@@ -22722,6 +22949,7 @@
2272222949 end
2272322950 end
2272422951 end
22952+ pop_scope
2272522953 @indent = @indent - 1
2272622954 emit(" }")
2272722955 return tmp_arr
@@ -22753,10 +22981,14 @@
2275322981 emit(" lv_" + bp1 + " = " + init_val + ";")
2275422982 rt = infer_type(@nd_receiver[nid])
2275522983 pfx = array_c_prefix(rt)
22984+ elem_t = elem_type_of_array(rt)
2275622985 tmp = new_temp
2275722986 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {")
22758- emit(" " + c_type(elem_type_of_array(rt)) + " lv_" + bp2 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
22987+ emit(" " + c_type(elem_t) + " lv_" + bp2 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
2275922988 @indent = @indent + 1
22989+ push_scope
22990+ declare_var(bp1, elem_t)
22991+ declare_var(bp2, elem_t)
2276022992 blk = @nd_block[nid]
2276122993 if blk >= 0
2276222994 body = @nd_body[blk]
@@ -22769,6 +23001,7 @@
2276923001 end
2277023002 end
2277123003 end
23004+ pop_scope
2277223005 @indent = @indent - 1
2277323006 emit(" }")
2277423007 end
@@ -22782,12 +23015,15 @@
2278223015 rt = infer_type(@nd_receiver[nid])
2278323016 if rt == "int_array" || rt == "sym_array"
2278423017 @needs_int_array = 1
23018+ bp_t = elem_type_of_array(rt)
2278523019 tmp_arr = new_temp
2278623020 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
2278723021 tmp_i = new_temp
2278823022 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2278923023 emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2279023024 @indent = @indent + 1
23025+ push_scope
23026+ declare_var(bp1, bp_t)
2279123027 blk = @nd_block[nid]
2279223028 if blk >= 0
2279323029 body = @nd_body[blk]
@@ -22800,6 +23036,7 @@
2280023036 end
2280123037 end
2280223038 end
23039+ pop_scope
2280323040 @indent = @indent - 1
2280423041 emit(" }")
2280523042 return tmp_arr
@@ -22822,6 +23059,8 @@
2282223059 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2282323060 emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2282423061 @indent = @indent + 1
23062+ push_scope
23063+ declare_var(bp1, "int")
2282523064 blk = @nd_block[nid]
2282623065 if blk >= 0
2282723066 body = @nd_body[blk]
@@ -22834,6 +23073,7 @@
2283423073 end
2283523074 end
2283623075 end
23076+ pop_scope
2283723077 @indent = @indent - 1
2283823078 emit(" }")
2283923079 end
@@ -23625,7 +23865,10 @@
2362523865 bpn = get_block_param(nid, 0)
2362623866 tmp_arrn = new_temp
2362723867 tmp_in = new_temp
23628- # infer block result type
23868+ push_scope
23869+ if bpn != ""
23870+ declare_var(bpn, "int")
23871+ end
2362923872 res_type = "int"
2363023873 blk_n = @nd_block[nid]
2363123874 if blk_n >= 0
@@ -23673,6 +23916,7 @@
2367323916 end
2367423917 @indent = @indent - 1
2367523918 emit(" }")
23919+ pop_scope
2367623920 @in_loop = old
2367723921 return
2367823922 end
@@ -23689,6 +23933,8 @@
2368923933 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2369023934 emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2369123935 @indent = @indent + 1
23936+ push_scope
23937+ declare_var(bp1, "int")
2369223938 blk = @nd_block[nid]
2369323939 if blk >= 0
2369423940 body = @nd_body[blk]
@@ -23701,6 +23947,7 @@
2370123947 end
2370223948 end
2370323949 end
23950+ pop_scope
2370423951 @indent = @indent - 1
2370523952 emit(" }")
2370623953 end
@@ -23710,6 +23957,8 @@
2371023957 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")
2371123958 emit(" const char *lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")
2371223959 @indent = @indent + 1
23960+ push_scope
23961+ declare_var(bp1, "string")
2371323962 blk = @nd_block[nid]
2371423963 if blk >= 0
2371523964 body = @nd_body[blk]
@@ -23722,6 +23971,7 @@
2372223971 end
2372323972 end
2372423973 end
23974+ pop_scope
2372523975 @indent = @indent - 1
2372623976 emit(" }")
2372723977 end
@@ -23742,10 +23992,13 @@
2374223992 tmp_arr = new_temp
2374323993 tmp_i = new_temp
2374423994 if rt == "int_array" || rt == "sym_array"
23995+ bp_t = elem_type_of_array(rt)
2374523996 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
2374623997 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2374723998 emit(" mrb_int lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
2374823999 @indent = @indent + 1
24000+ push_scope
24001+ declare_var(bp1, bp_t)
2374924002 blk = @nd_block[nid]
2375024003 if blk >= 0
2375124004 body = @nd_body[blk]
@@ -23758,6 +24011,7 @@
2375824011 end
2375924012 end
2376024013 end
24014+ pop_scope
2376124015 @indent = @indent - 1
2376224016 emit(" }")
2376324017 end