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
end19991999
if mname == "zip"2000-
return "int_array"2000+
if recv >= 02001+
rt = infer_type(recv)2002+
if rt == "str_array"2003+
return "str_array_ptr_array"2004+
end2005+
if rt == "float_array"2006+
return "float_array_ptr_array"2007+
end2008+
end2009+
return "int_array_ptr_array"20012010
end20022011
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
end9747+
if ltypes[k] == "int_array_ptr_array" && ltypes2[j] != "int_array_ptr_array" && is_ptr_array_type(ltypes2[j]) == 19748+
ltypes[k] = ltypes2[j]9749+
set_var_type(lnames[k], ltypes2[j])9750+
end97389751
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 + 197469759
end97479760
9748-
# Third pass: upgrade locals passed to lambda-param functions9761+
# 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+
end9771+
end9772+
end9773+
end9774+
}97499775
j = 09776+
while j < lnames3.length9777+
k = 09778+
while k < lnames.length9779+
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+
end9784+
if ltypes[k] == "int" && ltypes3[j] != "int" && ltypes3[j] != "nil"9785+
ltypes[k] = ltypes3[j]9786+
set_var_type(lnames[k], ltypes3[j])9787+
end9788+
end9789+
k = k + 19790+
end9791+
j = j + 19792+
end9793+
9794+
# Fourth pass: upgrade locals passed to lambda-param functions9795+
j = 097509796
while j < lnames.length97519797
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
end12357+
# zip without block: return array of pairs12358+
if mname == "zip" && @nd_block[nid] < 012359+
@needs_ptr_array = 112360+
@needs_gc = 112361+
pfx = array_c_prefix(recv_type)12362+
arg = compile_arg0(nid)12363+
tmp = new_temp12364+
itmp = new_temp12365+
pair_tmp = new_temp12366+
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 tmp12374+
end1231112375
# first(n) / last(n) with argument: return new array1231212376
if mname == "first" && @nd_arguments[nid] >= 01231312377
aargs = get_args(@nd_arguments[nid])@@ -12516,13 +12580,6 @@
1251612580
end1251712581
if mname == "dup"1251812582
return "sp_IntArray_dup(" + rc + ")"12519-
end12520-
if mname == "zip"12521-
# zip returns array of pairs; for length-only usage, return array with same length12522-
tmp = new_temp12523-
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 tmp1252612583
end1252712584
if mname == "count"1252812585
if @nd_block[nid] >= 0@@ -16981,11 +17038,13 @@
1698117038
emit(" }")1698217039
end1698317040
if is_ptr_array_type(rt) == 117041+
elem_type = ptr_array_elem_type(rt)1698417042
tmp = new_temp17043+
bp_tmp = new_temp1698517044
emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_PtrArray_length(" + rc + "); " + tmp + "++) {")1698617045
if has_bp == 116987-
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
end1699017049
@indent = @indent + 11699117050
compile_stmts_body(@nd_body[@nd_block[nid]])