Commit 667e591d

667e591d6776Merged

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

Description

fix: Array#zip non-block returns array of pairs, fix type propagation

zip without block now returns a proper array of 2-element arrays (int_array_ptr_array / str_array_ptr_array) instead of copying the receiver. Added a third scan_locals pass to resolve dependent types — block params that depend on container types refined in the second pass (e.g., zip result → each block param).

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

Details

Commit
667e591d6776
Parent
0de0c0193e99

spinel_codegen.rbmodifiedView file

@@ -1997,7 +1997,16 @@
19971997 return "str_int_hash"
19981998 end
19991999 if mname == "zip"
2000- return "int_array"
2000+ if recv >= 0
2001+ rt = infer_type(recv)
2002+ if rt == "str_array"
2003+ return "str_array_ptr_array"
2004+ end
2005+ if rt == "float_array"
2006+ return "float_array_ptr_array"
2007+ end
2008+ end
2009+ return "int_array_ptr_array"
20012010 end
20022011 if mname == "reject"
20032012 if recv >= 0
@@ -9735,6 +9744,10 @@
97359744 ltypes[k] = ltypes2[j]
97369745 set_var_type(lnames[k], ltypes2[j])
97379746 end
9747+ if ltypes[k] == "int_array_ptr_array" && ltypes2[j] != "int_array_ptr_array" && is_ptr_array_type(ltypes2[j]) == 1
9748+ ltypes[k] = ltypes2[j]
9749+ set_var_type(lnames[k], ltypes2[j])
9750+ end
97389751 if ltypes[k] == "str_int_hash" && ltypes2[j] == "str_str_hash"
97399752 ltypes[k] = ltypes2[j]
97409753 set_var_type(lnames[k], ltypes2[j])
@@ -9745,8 +9758,41 @@
97459758 j = j + 1
97469759 end
97479760
9748- # Third pass: upgrade locals passed to lambda-param functions
9761+ # Third pass: re-scan to resolve dependent types (e.g., block params of array-of-array)
9762+ lnames3 = "".split(",")
9763+ ltypes3 = "".split(",")
9764+ stmts.each { |sid|
9765+ if @nd_type[sid] != "DefNode"
9766+ if @nd_type[sid] != "ClassNode"
9767+ if @nd_type[sid] != "ConstantWriteNode"
9768+ if @nd_type[sid] != "ModuleNode"
9769+ scan_locals(sid, lnames3, ltypes3, empty_params)
9770+ end
9771+ end
9772+ end
9773+ end
9774+ }
97499775 j = 0
9776+ while j < lnames3.length
9777+ k = 0
9778+ while k < lnames.length
9779+ if lnames[k] == lnames3[j]
9780+ if ltypes[k] == "int_array" && ltypes3[j] != "int_array" && ltypes3[j] != "int"
9781+ ltypes[k] = ltypes3[j]
9782+ set_var_type(lnames[k], ltypes3[j])
9783+ end
9784+ if ltypes[k] == "int" && ltypes3[j] != "int" && ltypes3[j] != "nil"
9785+ ltypes[k] = ltypes3[j]
9786+ set_var_type(lnames[k], ltypes3[j])
9787+ end
9788+ end
9789+ k = k + 1
9790+ end
9791+ j = j + 1
9792+ end
9793+
9794+ # Fourth pass: upgrade locals passed to lambda-param functions
9795+ j = 0
97509796 while j < lnames.length
97519797 if ltypes[j] == "int"
97529798 # Check all main-level statements for lambda usage
@@ -12308,6 +12354,24 @@
1230812354 if recv_type == "str_int_hash" || recv_type == "str_str_hash"
1230912355 return ""
1231012356 end
12357+ # zip without block: return array of pairs
12358+ if mname == "zip" && @nd_block[nid] < 0
12359+ @needs_ptr_array = 1
12360+ @needs_gc = 1
12361+ pfx = array_c_prefix(recv_type)
12362+ arg = compile_arg0(nid)
12363+ tmp = new_temp
12364+ itmp = new_temp
12365+ pair_tmp = new_temp
12366+ emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")
12367+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_" + pfx + "_length(" + rc + "); " + itmp + "++) {")
12368+ emit(" " + c_type(recv_type) + " " + pair_tmp + " = sp_" + pfx + "_new();")
12369+ emit(" sp_" + pfx + "_push(" + pair_tmp + ", sp_" + pfx + "_get(" + rc + ", " + itmp + "));")
12370+ emit(" sp_" + pfx + "_push(" + pair_tmp + ", sp_" + pfx + "_get(" + arg + ", " + itmp + "));")
12371+ emit(" sp_PtrArray_push(" + tmp + ", " + pair_tmp + ");")
12372+ emit(" }")
12373+ return tmp
12374+ end
1231112375 # first(n) / last(n) with argument: return new array
1231212376 if mname == "first" && @nd_arguments[nid] >= 0
1231312377 aargs = get_args(@nd_arguments[nid])
@@ -12516,13 +12580,6 @@
1251612580 end
1251712581 if mname == "dup"
1251812582 return "sp_IntArray_dup(" + rc + ")"
12519- end
12520- if mname == "zip"
12521- # zip returns array of pairs; for length-only usage, return array with same length
12522- tmp = new_temp
12523- emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
12524- emit(" for (mrb_int _i = 0; _i < sp_IntArray_length(" + rc + "); _i++) sp_IntArray_push(" + tmp + ", sp_IntArray_get(" + rc + ", _i));")
12525- return tmp
1252612583 end
1252712584 if mname == "count"
1252812585 if @nd_block[nid] >= 0
@@ -16981,11 +17038,13 @@
1698117038 emit(" }")
1698217039 end
1698317040 if is_ptr_array_type(rt) == 1
17041+ elem_type = ptr_array_elem_type(rt)
1698417042 tmp = new_temp
17043+ bp_tmp = new_temp
1698517044 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_PtrArray_length(" + rc + "); " + tmp + "++) {")
1698617045 if has_bp == 1
16987- elem_type = ptr_array_elem_type(rt)
16988- emit(" lv_" + bp1 + " = (" + c_type(elem_type) + ")sp_PtrArray_get(" + rc + ", " + tmp + ");")
17046+ emit(" " + c_type(elem_type) + " " + bp_tmp + " = (" + c_type(elem_type) + ")sp_PtrArray_get(" + rc + ", " + tmp + ");")
17047+ emit(" lv_" + bp1 + " = " + bp_tmp + ";")
1698917048 end
1699017049 @indent = @indent + 1
1699117050 compile_stmts_body(@nd_body[@nd_block[nid]])