Commit c6fec56e

c6fec56e9af7Merged

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

Description

fix(codegen): N.times.map { array } collects into sp_PtrArray

T.Yamada's #553: 2.times.map { gets.split.map &:to_i } failed at C compile with -Wint-conversion (sp_IntArray pointers handed to sp_IntArray_push's mrb_int second arg). compile_map_expr's times.map special path always built sp_IntArray for the outer accumulator regardless of the block's return type; only string and float were typed correctly.

When the block returns any array shape (int_array, str_array, float_array, sym_array, poly_array, or any *_ptr_array nesting), build sp_PtrArray instead. Analyze already infers <inner>_ptr_array as the static type for this shape (sibling to the regular recv.map { array } arm that's been in place since #519), and the inspect helpers (sp_IntArrayPtrArray_inspect etc.) format the result correctly. The "cannot resolve call to 'times'" warning at the top of analyze's stderr is unrelated and pre-existing; codegen handled the times.map pattern via this special path all along.

Closes #553

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

Details

Commit
c6fec56e9af7
Parent
f25fe0d072a4

spinel_codegen.rbmodifiedView file

@@ -30094,11 +30094,23 @@
3009430094 end
3009530095 tmp_arrn = new_temp
3009630096 tmp_in = new_temp
30097+ # When the block body returns an array (or any pointer-shaped
30098+ # value), the outer .map collects them into a sp_PtrArray
30099+ # rather than sp_IntArray (which would pun the IntArray
30100+ # pointers as mrb_int and trip -Wint-conversion). Issue #553:
30101+ # `2.times.map { gets.split.map &:to_i }` builds an
30102+ # array-of-IntArrays; static type is int_array_ptr_array
30103+ # and inspect knows how to render it.
30104+ nested_arr_outer = 0
3009730105 if res_type == "string"
3009830106 @needs_str_array = 1
3009930107 emit(" sp_StrArray *" + tmp_arrn + " = sp_StrArray_new();")
3010030108 elsif res_type == "float"
3010130109 emit(" sp_FloatArray *" + tmp_arrn + " = sp_FloatArray_new();")
30110+ elsif res_type == "int_array" || res_type == "str_array" || res_type == "float_array" || res_type == "sym_array" || res_type == "poly_array" || res_type.end_with?("_ptr_array")
30111+ @needs_gc = 1
30112+ nested_arr_outer = 1
30113+ emit(" sp_PtrArray *" + tmp_arrn + " = sp_PtrArray_new();")
3010230114 else
3010330115 @needs_int_array = 1
3010430116 emit(" sp_IntArray *" + tmp_arrn + " = sp_IntArray_new();")
@@ -30128,6 +30140,8 @@
3012830140 emit(" sp_StrArray_push(" + tmp_arrn + ", " + lastv + ");")
3012930141 elsif res_type == "float"
3013030142 emit(" sp_FloatArray_push(" + tmp_arrn + ", " + lastv + ");")
30143+ elsif nested_arr_outer == 1
30144+ emit(" sp_PtrArray_push(" + tmp_arrn + ", " + lastv + ");")
3013130145 else
3013230146 emit(" sp_IntArray_push(" + tmp_arrn + ", " + lastv + ");")
3013330147 end