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_scope
3819638196 return tmp_arr
3819738197 end
38198+ # Issue #830: str_array.map { |s| <float expr> } -- the
38199+ # canonical shape is `str.split.map(&:to_f)` which lowers
38200+ # to `{ |s| s.to_f }`. Without this branch the accumulator
38201+ # is sp_StrArray and the push attempts to store a double
38202+ # 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 + 1
38209+ if blk >= 0
38210+ body3 = @nd_body[blk]
38211+ stmts3 = body3 >= 0 ? get_stmts(body3) : []
38212+ if stmts3.length > 0
38213+ k = 0
38214+ while k < stmts3.length - 1
38215+ compile_stmt(stmts3[k])
38216+ k = k + 1
38217+ end
38218+ val = compile_expr(stmts3.last)
38219+ emit(" sp_FloatArray_push(" + tmp_arr + ", " + val + ");")
38220+ else
38221+ emit_map_default_push(tmp_arr, "float_array")
38222+ end
38223+ end
38224+ @indent = @indent - 1
38225+ emit(" }")
38226+ pop_scope
38227+ return tmp_arr
38228+ end
3819838229 @needs_str_array = 1
3819938230 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
3820038231 emit(" SP_GC_ROOT(" + tmp_arr + ");")