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
return tmp_arr end end if rt == "str_array" # str_array.map { |s| ... } produced no result branch before, so # `tt = foo.map { ... }` silently became `lv_tt = 0` and the # subsequent iteration crashed. Issue #43. @needs_gc = 1 block_ret = "string" blk = @nd_block[nid] if blk >= 0 body = @nd_body[blk] if body >= 0 stmts = get_stmts(body) if stmts.length > 0 block_ret = infer_type(stmts.last) end end end declare_var(bp1, "string") if block_ret == "int" @needs_int_array = 1 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();") emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {") emit(" lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");") @indent = @indent + 1 if blk >= 0 body3 = @nd_body[blk] if body3 >= 0 stmts3 = get_stmts(body3) if stmts3.length > 0 k = 0 while k < stmts3.length - 1 compile_stmt(stmts3[k]) k = k + 1 end val = compile_expr(stmts3.last) emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");") end end end @indent = @indent - 1 emit(" }") return tmp_arr end @needs_str_array = 1 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();") emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {") emit(" lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");") @indent = @indent + 1 if blk >= 0 body3 = @nd_body[blk] if body3 >= 0 stmts3 = get_stmts(body3) if stmts3.length > 0 k = 0 while k < stmts3.length - 1 compile_stmt(stmts3[k]) k = k + 1 end val = compile_expr(stmts3.last) emit(" sp_StrArray_push(" + tmp_arr + ", " + val + ");") end end end @indent = @indent - 1 emit(" }") return tmp_arr end "0" endtest/bm_str_array_map.rbaddedView file
Loading diff…