Commit 1fe58ef6
1fe58ef621f5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
codegen: float-block return arm for str_array#map
`'3.14 6.28'.split.map(&:to_f)` failed to compile: the str_array .map branch always built an sp_StrArray accumulator regardless of the block's return type, then tried to push the atof double into a `const char *` slot.
The same branch already had an arm for int return (`map(&:to_i)`, etc.); extend it with a sibling for float so the canonical `split.map(&:to_f)` chain accumulates into an sp_FloatArray. Mirror shape of the int arm: scope-local lv_ decl inside the for body, FloatArray push, FloatArray default-push when the block is empty.
Fixes #830
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 1fe58ef621f5
- Parent
- cfd3bd793e6b
spinel_codegen.rbmodifiedView file
@@ -38195,6 +38195,37 @@
3819538195
pop_scope3819638196
return tmp_arr3819738197
end38198+
# Issue #830: str_array.map { |s| <float expr> } -- the38199+
# canonical shape is `str.split.map(&:to_f)` which lowers38200+
# to `{ |s| s.to_f }`. Without this branch the accumulator38201+
# is sp_StrArray and the push attempts to store a double38202+
# into a const char * slot.38203+
if block_ret == "float"38204+
emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")38205+
emit(" SP_GC_ROOT(" + tmp_arr + ");")38206+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")38207+
emit(" const char *lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")38208+
@indent = @indent + 138209+
if blk >= 038210+
body3 = @nd_body[blk]38211+
stmts3 = body3 >= 0 ? get_stmts(body3) : []38212+
if stmts3.length > 038213+
k = 038214+
while k < stmts3.length - 138215+
compile_stmt(stmts3[k])38216+
k = k + 138217+
end38218+
val = compile_expr(stmts3.last)38219+
emit(" sp_FloatArray_push(" + tmp_arr + ", " + val + ");")38220+
else38221+
emit_map_default_push(tmp_arr, "float_array")38222+
end38223+
end38224+
@indent = @indent - 138225+
emit(" }")38226+
pop_scope38227+
return tmp_arr38228+
end3819838229
@needs_str_array = 13819938230
emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")3820038231
emit(" SP_GC_ROOT(" + tmp_arr + ");")