Commit 9eba3127

9eba312702ceMerged

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

Description

fix(codegen): Array#map on str_array

Closes #43.

compile_map_expr had branches for int_array, sym_array, and the N.times.map shorthand, but no branch for str_array. So `tt = foo.map { |s| ... }` on a string array fell through to the function's tail `return "0"`, the caller assigned that literal 0 to the tt local, and the subsequent iteration over tt crashed in sp_StrArray_length(NULL).

Add a str_array branch that infers the block return type from the last expression and builds either a StrArray (string body) or an IntArray (int body), iterating the receiver with sp_StrArray_get and pushing each block result. New regression test covers the original repro pattern (.map { ... }.each { ... }), plus a string-to-int map and the empty-input edge case.

Note: the issue's exact reproducer reuses the block parameter name `i` across a `3.times do |i|` and a `foo.map { |i| ... }`, which exposes a separate Spinel limitation: block params with the same name across differently-typed blocks share a single C local (`lv_i`) and can't be re-typed. Renaming one block's param works. That scoping fix is out of scope here.

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

Details

Commit
9eba312702ce
Parent
642a2e8d6907

spinel_codegen.rbmodifiedView file

@@ -20951,6 +20951,72 @@
2095120951 return tmp_arr
2095220952 end
2095320953 end
20954+ if rt == "str_array"
20955+ # str_array.map { |s| ... } produced no result branch before, so
20956+ # `tt = foo.map { ... }` silently became `lv_tt = 0` and the
20957+ # subsequent iteration crashed. Issue #43.
20958+ @needs_gc = 1
20959+ block_ret = "string"
20960+ blk = @nd_block[nid]
20961+ if blk >= 0
20962+ body = @nd_body[blk]
20963+ if body >= 0
20964+ stmts = get_stmts(body)
20965+ if stmts.length > 0
20966+ block_ret = infer_type(stmts.last)
20967+ end
20968+ end
20969+ end
20970+ declare_var(bp1, "string")
20971+ if block_ret == "int"
20972+ @needs_int_array = 1
20973+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
20974+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")
20975+ emit(" lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")
20976+ @indent = @indent + 1
20977+ if blk >= 0
20978+ body3 = @nd_body[blk]
20979+ if body3 >= 0
20980+ stmts3 = get_stmts(body3)
20981+ if stmts3.length > 0
20982+ k = 0
20983+ while k < stmts3.length - 1
20984+ compile_stmt(stmts3[k])
20985+ k = k + 1
20986+ end
20987+ val = compile_expr(stmts3.last)
20988+ emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")
20989+ end
20990+ end
20991+ end
20992+ @indent = @indent - 1
20993+ emit(" }")
20994+ return tmp_arr
20995+ end
20996+ @needs_str_array = 1
20997+ emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
20998+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")
20999+ emit(" lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")
21000+ @indent = @indent + 1
21001+ if blk >= 0
21002+ body3 = @nd_body[blk]
21003+ if body3 >= 0
21004+ stmts3 = get_stmts(body3)
21005+ if stmts3.length > 0
21006+ k = 0
21007+ while k < stmts3.length - 1
21008+ compile_stmt(stmts3[k])
21009+ k = k + 1
21010+ end
21011+ val = compile_expr(stmts3.last)
21012+ emit(" sp_StrArray_push(" + tmp_arr + ", " + val + ");")
21013+ end
21014+ end
21015+ end
21016+ @indent = @indent - 1
21017+ emit(" }")
21018+ return tmp_arr
21019+ end
2095421020 "0"
2095521021 end
2095621022