Commit 15b216b6

15b216b66030Merged

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

Description

codegen: fix min/max comparator emit at -O0 — shadow lv_ in block scope

CI red on master after a5b63f6 — sort_min_max_block_comparator failed to compile at -O0 with int↔string type mismatch on lv_a/lv_b. The emit was relying on emit_iter_open's bare `lv_a = sp_StrArray_get(...)` assignment, but the function-scope lv_a slot was typed as mrb_int by other call sites of the same block param name; -O3 elided the dead type-mismatched read, masking the bug locally.

Inline the for-loop emit with a fresh ` lv_ = sp__get(...)` declaration so the loop body shadows the outer slot. -O0 now compiles cleanly; output unchanged.

Refs #980

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

Details

Commit
15b216b66030
Parent
4af1b4df8b8f

spinel_codegen.rbmodifiedView file

@@ -40521,8 +40521,16 @@
4052140521 # best and update when the comparator says the new element wins.
4052240522 # 1-param block is the legacy min_by-style scoring function.
4052340523 if bp2 != ""
40524+ # Declare lv_<bp1> / lv_<bp2> fresh inside the loop body so we
40525+ # shadow any function-scope slot (the same `a` / `b` names may
40526+ # have been widened to a different type by other call sites).
40527+ # Without the shadow, emit_iter_open's bare assignment hits a
40528+ # type mismatch at -O0 (the optimizer elides at -O3, masking
40529+ # the bug from local testing).
4052440530 emit(" " + c_type(elem_type) + " " + tmp_res + " = " + c_default_val(elem_type) + ";")
40525- emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
40531+ pfx_mm = array_c_prefix(recv_type)
40532+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx_mm + "_length(" + rc + "); " + tmp_i + "++) {")
40533+ emit(" " + c_type(elem_type) + " lv_" + bp1 + " = sp_" + pfx_mm + "_get(" + rc + ", " + tmp_i + ");")
4052640534 push_scope
4052740535 declare_var(bp1, elem_type)
4052840536 declare_var(bp2, elem_type)