Commit a931fe6d

a931fe6d7309Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): promote-aware block param in emit_iter_open / sum block

Block-iteration helpers (compile_array_sum_block, emit_iter_open) hardcoded the block param's C-side declared type to the array's element type. Once promote mode widened the param's slot to bigint at the outer scope, the C-level lv_x was declared as sp_Bigint * by declare_method_locals but the inner block compile saw x as the array's int element type and lowered x * 2 to sp_int_mul(lv_x, 2LL) — passing a bigint pointer to an mrb_int macro.

emit_iter_open now wraps the iterator value with sp_bigint_new_ int(...) when the LHS slot is bigint. compile_array_sum_block re-declares the block param at its outer-scope type when that type is bigint, so the block body lowers arithmetic through sp_bigint_*, and the final accumulator pulls back to mrb_int via sp_bigint_to_int before the +=.

Promote-mode test count: 450 pass / 43 fail / 125 error -> 452 pass / 43 fail / 123 error. Default raise mode unchanged at 618 / 0 / 0.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
a931fe6d7309
Parent
fd76e6658c1d

spinel_codegen.rbmodifiedView file

@@ -31839,16 +31839,39 @@
3183931839 # the loop counter (position or value for range); elem_var gets the
3184031840 # current element.
3184131841 def emit_iter_open(rc, recv_type, elem_var, idx_var)
31842+ # `--int-overflow=promote` may have widened the block param's
31843+ # declared slot to sp_Bigint *, but the underlying iterator
31844+ # (sp_IntArray_get, range counter, ...) yields a raw mrb_int.
31845+ # Detect the widening via find_var_type on the stripped-`lv_`
31846+ # name and wrap the get with sp_bigint_new_int so the assign
31847+ # is typed-clean.
31848+ iter_elem_t = iter_elem_type(recv_type)
31849+ needs_big_wrap = 0
31850+ if elem_var.start_with?("lv_") && iter_elem_t == "int"
31851+ slot_t_iter = find_var_type(elem_var[3, elem_var.length - 3])
31852+ if slot_t_iter == "bigint"
31853+ needs_big_wrap = 1
31854+ @needs_bigint = 1
31855+ end
31856+ end
3184231857 if recv_type == "range"
3184331858 rtmp = new_temp
3184431859 emit(" sp_Range " + rtmp + " = " + rc + ";")
3184531860 emit(" for (mrb_int " + idx_var + " = " + rtmp + ".first; " + idx_var + " <= " + rtmp + ".last; " + idx_var + "++) {")
31846- emit(" " + elem_var + " = " + idx_var + ";")
31861+ if needs_big_wrap == 1
31862+ emit(" " + elem_var + " = sp_bigint_new_int(" + idx_var + ");")
31863+ else
31864+ emit(" " + elem_var + " = " + idx_var + ";")
31865+ end
3184731866 return
3184831867 end
3184931868 pfx = array_c_prefix(recv_type)
3185031869 emit(" for (mrb_int " + idx_var + " = 0; " + idx_var + " < sp_" + pfx + "_length(" + rc + "); " + idx_var + "++) {")
31851- emit(" " + elem_var + " = sp_" + pfx + "_get(" + rc + ", " + idx_var + ");")
31870+ if needs_big_wrap == 1
31871+ emit(" " + elem_var + " = sp_bigint_new_int(sp_" + pfx + "_get(" + rc + ", " + idx_var + "));")
31872+ else
31873+ emit(" " + elem_var + " = sp_" + pfx + "_get(" + rc + ", " + idx_var + ");")
31874+ end
3185231875 end
3185331876
3185431877 # Element type of an iterable (for block param type inference).
@@ -31873,7 +31896,15 @@
3187331896 end
3187431897 emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
3187531898 push_scope
31876- declare_var(bp1, iter_elem_type(recv_type))
31899+ # promote-mode-widened block param: keep the inner scope in
31900+ # agreement with the C-level decl so `x * 2` lowers via
31901+ # sp_bigint_mul, not sp_int_mul. find_var_type before the
31902+ # push_scope reads the outer (declare_method_locals) entry.
31903+ bp1_t_sum = find_var_type(bp1)
31904+ if bp1_t_sum != "bigint"
31905+ bp1_t_sum = iter_elem_type(recv_type)
31906+ end
31907+ declare_var(bp1, bp1_t_sum)
3187731908 blk = @nd_block[nid]
3187831909 bexpr = "0"
3187931910 if @nd_body[blk] >= 0
@@ -31887,7 +31918,12 @@
3188731918 bexpr = compile_expr(bs.last)
3188831919 end
3188931920 end
31890- emit(" " + tmp_sum + " += " + bexpr + ";")
31921+ if bp1_t_sum == "bigint"
31922+ @needs_bigint = 1
31923+ emit(" " + tmp_sum + " += sp_bigint_to_int((sp_Bigint *)" + bexpr + ");")
31924+ else
31925+ emit(" " + tmp_sum + " += " + bexpr + ";")
31926+ end
3189131927 pop_scope
3189231928 emit(" }")
3189331929 tmp_sum