Commit b6bb784c

b6bb784c010dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 2 files · ~2 modified

Description

fix(codegen): make str_array.map block param truly block-local

Follow-up to 24ab93f. The previous fix emitted

lv_<bp> = sp_StrArray_get(rc, i);

inside the map's for loop, reusing whatever lv_ the surrounding function had already declared. When the same parameter name was already bound to a differently-typed local outside the block (e.g. `3.times do |i| ... end` followed by `foo.map { |i| i.upcase }`), the assignment failed -Werror=int-conversion and the block was no longer Ruby-block-local.

Declare lv_ with its inferred element type inside the for body so it C-shadows the outer same-named local for the duration of the loop scope. The extended bm_str_array_map.rb covers the original issue #43 reproducer with `i` reused across an outer times block and the inner map block.

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

Details

Commit
b6bb784c010d
Parent
9eba312702ce

spinel_codegen.rbmodifiedView file

@@ -20972,7 +20972,11 @@
2097220972 @needs_int_array = 1
2097320973 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
2097420974 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")
20975- emit(" lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")
20975+ # Declare lv_<bp> with the block-local type inside the for body
20976+ # so it C-shadows any outer same-named local (Ruby block-local
20977+ # parameter scope: i in `foo.map { |i| ... }` is independent of
20978+ # any outer i).
20979+ emit(" const char *lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")
2097620980 @indent = @indent + 1
2097720981 if blk >= 0
2097820982 body3 = @nd_body[blk]
@@ -20996,7 +21000,8 @@
2099621000 @needs_str_array = 1
2099721001 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
2099821002 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")
20999- emit(" lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")
21003+ # Block-local C decl, see note in the int-block branch above.
21004+ emit(" const char *lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")
2100021005 @indent = @indent + 1
2100121006 if blk >= 0
2100221007 body3 = @nd_body[blk]