Commit b0969b06

b0969b06b62dMerged

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

Description

fix(codegen): map with empty block produces correct-length array

Per Gemini's review of #210, an empty map {} block in CRuby yields nil per iteration — the result array still matches the receiver's length. Each map dispatch (Range, N.times, int_array, str_array, poly_array) gated its push on stmts.length > 0, so the accumulator stayed empty and downstream .length / [i] on the typed result were wrong.

Add emit_map_default_push that emits a type-appropriate default (0 / 0.0 / "" / sp_box_nil()) and call it on the else branch of every map dispatch. Also restructure the body-id checks so a block with body == -1 (the literal {} shape) lands in the same path as a block with empty stmts.

Two extra fixes shaken out while wiring this:

  • poly_array.map's per-iteration lv_<bp> = sp_PolyArray_get(...) was an assignment to an undeclared C variable when the synthetic _x placeholder is used (no real block param). Inline it as sp_RbVal lv_<bp> = ..., matching int_array.map's pattern.
  • poly_array.map's empty-block branch defaulted block_ret to "int" and emitted an IntArray accumulator, but infer_type predicts "poly_array" for that shape — the call site type- mismatched. Default block_ret_p to "" so the catch-all PolyArray branch fires.

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

Details

Commit
b0969b06b62d
Parent
a39c6aae6520

spinel_codegen.rbmodifiedView file

@@ -26950,6 +26950,25 @@
2695026950 tmp_arr
2695126951 end
2695226952
26953+ # Issue #210 follow-up: an empty `map {}` block yields nil per
26954+ # iteration in CRuby — the result array's length still matches the
26955+ # receiver. Without an explicit push the typed accumulator stays
26956+ # short and downstream `.length` / `[i]` on the result is wrong.
26957+ # Push a type-appropriate default (0 / 0.0 / "" / sp_box_nil) so
26958+ # length is preserved across all map dispatches.
26959+ def emit_map_default_push(target, container_type)
26960+ if container_type == "str_array"
26961+ emit(" sp_StrArray_push(" + target + ", \"\");")
26962+ elsif container_type == "float_array"
26963+ emit(" sp_FloatArray_push(" + target + ", 0.0);")
26964+ elsif container_type == "poly_array"
26965+ @needs_rb_value = 1
26966+ emit(" sp_PolyArray_push(" + target + ", sp_box_nil());")
26967+ else
26968+ emit(" sp_IntArray_push(" + target + ", 0);")
26969+ end
26970+ end
26971+
2695326972 def compile_map_expr(nid)
2695426973 # N.times.map { |i| ... } -> loop 0..N-1 building an array
2695526974 recv_n = @nd_receiver[nid]
@@ -26995,23 +27014,24 @@
2699527014 @indent = @indent + 1
2699627015 if blk_n >= 0
2699727016 body_n2 = @nd_body[blk_n]
26998- if body_n2 >= 0
26999- stmts_n2 = get_stmts(body_n2)
27000- if stmts_n2.length > 0
27001- k = 0
27002- while k < stmts_n2.length - 1
27003- compile_stmt(stmts_n2[k])
27004- k = k + 1
27005- end
27006- lastv = compile_expr(stmts_n2.last)
27007- if res_type == "string"
27008- emit(" sp_StrArray_push(" + tmp_arrn + ", " + lastv + ");")
27009- elsif res_type == "float"
27010- emit(" sp_FloatArray_push(" + tmp_arrn + ", " + lastv + ");")
27011- else
27012- emit(" sp_IntArray_push(" + tmp_arrn + ", " + lastv + ");")
27013- end
27017+ stmts_n2 = body_n2 >= 0 ? get_stmts(body_n2) : []
27018+ n_container = res_type == "string" ? "str_array" : (res_type == "float" ? "float_array" : "int_array")
27019+ if stmts_n2.length > 0
27020+ k = 0
27021+ while k < stmts_n2.length - 1
27022+ compile_stmt(stmts_n2[k])
27023+ k = k + 1
27024+ end
27025+ lastv = compile_expr(stmts_n2.last)
27026+ if res_type == "string"
27027+ emit(" sp_StrArray_push(" + tmp_arrn + ", " + lastv + ");")
27028+ elsif res_type == "float"
27029+ emit(" sp_FloatArray_push(" + tmp_arrn + ", " + lastv + ");")
27030+ else
27031+ emit(" sp_IntArray_push(" + tmp_arrn + ", " + lastv + ");")
2701427032 end
27033+ else
27034+ emit_map_default_push(tmp_arrn, n_container)
2701527035 end
2701627036 end
2701727037 @indent = @indent - 1
@@ -27116,23 +27136,24 @@
2711627136 @indent = @indent + 1
2711727137 if blk_r >= 0
2711827138 body_r2 = @nd_body[blk_r]
27119- if body_r2 >= 0
27120- stmts_r2 = get_stmts(body_r2)
27121- k_r = 0
27122- while k_r < stmts_r2.length - 1
27123- compile_stmt(stmts_r2[k_r])
27124- k_r = k_r + 1
27139+ stmts_r2 = body_r2 >= 0 ? get_stmts(body_r2) : []
27140+ r_container = block_ret_r == "string" ? "str_array" : (block_ret_r == "float" ? "float_array" : "int_array")
27141+ k_r = 0
27142+ while k_r < stmts_r2.length - 1
27143+ compile_stmt(stmts_r2[k_r])
27144+ k_r = k_r + 1
27145+ end
27146+ if stmts_r2.length > 0
27147+ lastv_r = compile_expr(stmts_r2.last)
27148+ if block_ret_r == "string"
27149+ emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_r + ");")
27150+ elsif block_ret_r == "float"
27151+ emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_r + ");")
27152+ else
27153+ emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_r + ");")
2712527154 end
27126- if stmts_r2.length > 0
27127- lastv_r = compile_expr(stmts_r2.last)
27128- if block_ret_r == "string"
27129- emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_r + ");")
27130- elsif block_ret_r == "float"
27131- emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_r + ");")
27132- else
27133- emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_r + ");")
27134- end
27135- end
27155+ else
27156+ emit_map_default_push(tmp_arr, r_container)
2713627157 end
2713727158 end
2713827159 @indent = @indent - 1
@@ -27208,13 +27229,13 @@
2720827229 blk2 = @nd_block[nid]
2720927230 if blk2 >= 0
2721027231 body3 = @nd_body[blk2]
27211- if body3 >= 0
27212- stmts3 = get_stmts(body3)
27213- if stmts3.length > 0
27214- last = stmts3[stmts3.length - 1]
27215- val = compile_expr(last)
27216- emit(" sp_StrArray_push(" + tmp_arr + ", " + val + ");")
27217- end
27232+ stmts3 = body3 >= 0 ? get_stmts(body3) : []
27233+ if stmts3.length > 0
27234+ last = stmts3[stmts3.length - 1]
27235+ val = compile_expr(last)
27236+ emit(" sp_StrArray_push(" + tmp_arr + ", " + val + ");")
27237+ else
27238+ emit_map_default_push(tmp_arr, "str_array")
2721827239 end
2721927240 end
2722027241 @indent = @indent - 1
@@ -27234,13 +27255,13 @@
2723427255 blk2 = @nd_block[nid]
2723527256 if blk2 >= 0
2723627257 body3 = @nd_body[blk2]
27237- if body3 >= 0
27238- stmts3 = get_stmts(body3)
27239- if stmts3.length > 0
27240- last = stmts3[stmts3.length - 1]
27241- val = compile_expr(last)
27242- emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")
27243- end
27258+ stmts3 = body3 >= 0 ? get_stmts(body3) : []
27259+ if stmts3.length > 0
27260+ last = stmts3[stmts3.length - 1]
27261+ val = compile_expr(last)
27262+ emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")
27263+ else
27264+ emit_map_default_push(tmp_arr, "int_array")
2724427265 end
2724527266 end
2724627267 @indent = @indent - 1
@@ -27280,17 +27301,17 @@
2728027301 @indent = @indent + 1
2728127302 if blk >= 0
2728227303 body3 = @nd_body[blk]
27283- if body3 >= 0
27284- stmts3 = get_stmts(body3)
27285- if stmts3.length > 0
27286- k = 0
27287- while k < stmts3.length - 1
27288- compile_stmt(stmts3[k])
27289- k = k + 1
27290- end
27291- val = compile_expr(stmts3.last)
27292- emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")
27304+ stmts3 = body3 >= 0 ? get_stmts(body3) : []
27305+ if stmts3.length > 0
27306+ k = 0
27307+ while k < stmts3.length - 1
27308+ compile_stmt(stmts3[k])
27309+ k = k + 1
2729327310 end
27311+ val = compile_expr(stmts3.last)
27312+ emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")
27313+ else
27314+ emit_map_default_push(tmp_arr, "int_array")
2729427315 end
2729527316 end
2729627317 @indent = @indent - 1
@@ -27307,17 +27328,17 @@
2730727328 @indent = @indent + 1
2730827329 if blk >= 0
2730927330 body3 = @nd_body[blk]
27310- if body3 >= 0
27311- stmts3 = get_stmts(body3)
27312- if stmts3.length > 0
27313- k = 0
27314- while k < stmts3.length - 1
27315- compile_stmt(stmts3[k])
27316- k = k + 1
27317- end
27318- val = compile_expr(stmts3.last)
27319- emit(" sp_StrArray_push(" + tmp_arr + ", " + val + ");")
27331+ stmts3 = body3 >= 0 ? get_stmts(body3) : []
27332+ if stmts3.length > 0
27333+ k = 0
27334+ while k < stmts3.length - 1
27335+ compile_stmt(stmts3[k])
27336+ k = k + 1
2732027337 end
27338+ val = compile_expr(stmts3.last)
27339+ emit(" sp_StrArray_push(" + tmp_arr + ", " + val + ");")
27340+ else
27341+ emit_map_default_push(tmp_arr, "str_array")
2732127342 end
2732227343 end
2732327344 @indent = @indent - 1
@@ -27332,7 +27353,11 @@
2733227353 # `.length` / `[i]` on the typed accumulator dereferences NULL.
2733327354 if rt == "poly_array"
2733427355 @needs_gc = 1
27335- block_ret_p = "int"
27356+ # Empty / missing block leaves block_ret_p as "" so the
27357+ # accumulator falls into the catch-all PolyArray branch —
27358+ # matches infer_type's "poly_array" prediction for
27359+ # poly_array.map with no expression in the block.
27360+ block_ret_p = ""
2733627361 blk_p = @nd_block[nid]
2733727362 if blk_p >= 0
2733827363 body_p = @nd_body[blk_p]
@@ -27362,29 +27387,33 @@
2736227387 emit(" SP_GC_ROOT(" + tmp_arr + ");")
2736327388 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_PolyArray_length(" + rc + "); " + tmp_i + "++) {")
2736427389 declare_var(bp1, "poly")
27365- emit(" lv_" + bp1 + " = sp_PolyArray_get(" + rc + ", " + tmp_i + ");")
27390+ # Inline C declaration (shadows any outer lv_<bp1>) — covers
27391+ # the synthetic `_x` placeholder when the block has no param,
27392+ # which has no spinel-side declaration to reach this scope.
27393+ emit(" sp_RbVal lv_" + bp1 + " = sp_PolyArray_get(" + rc + ", " + tmp_i + ");")
2736627394 @indent = @indent + 1
2736727395 if blk_p >= 0
2736827396 body_p2 = @nd_body[blk_p]
27369- if body_p2 >= 0
27370- stmts_p2 = get_stmts(body_p2)
27371- k_p = 0
27372- while k_p < stmts_p2.length - 1
27373- compile_stmt(stmts_p2[k_p])
27374- k_p = k_p + 1
27375- end
27376- if stmts_p2.length > 0
27377- lastv_p = compile_expr(stmts_p2.last)
27378- if block_ret_p == "string"
27379- emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_p + ");")
27380- elsif block_ret_p == "float"
27381- emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_p + ");")
27382- elsif block_ret_p == "int" || block_ret_p == "bool"
27383- emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_p + ");")
27384- else
27385- emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret_p, lastv_p) + ");")
27386- end
27397+ stmts_p2 = body_p2 >= 0 ? get_stmts(body_p2) : []
27398+ p_container = (block_ret_p == "string") ? "str_array" : ((block_ret_p == "float") ? "float_array" : ((block_ret_p == "int" || block_ret_p == "bool") ? "int_array" : "poly_array"))
27399+ k_p = 0
27400+ while k_p < stmts_p2.length - 1
27401+ compile_stmt(stmts_p2[k_p])
27402+ k_p = k_p + 1
27403+ end
27404+ if stmts_p2.length > 0
27405+ lastv_p = compile_expr(stmts_p2.last)
27406+ if block_ret_p == "string"
27407+ emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_p + ");")
27408+ elsif block_ret_p == "float"
27409+ emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_p + ");")
27410+ elsif block_ret_p == "int" || block_ret_p == "bool"
27411+ emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_p + ");")
27412+ else
27413+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret_p, lastv_p) + ");")
2738727414 end
27415+ else
27416+ emit_map_default_push(tmp_arr, p_container)
2738827417 end
2738927418 end
2739027419 @indent = @indent - 1