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
@needs_int_array = 1 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();") emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {") emit(" lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");") # Declare lv_<bp> with the block-local type inside the for body # so it C-shadows any outer same-named local (Ruby block-local # parameter scope: i in `foo.map { |i| ... }` is independent of # any outer i). emit(" const char *lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");") @indent = @indent + 1 if blk >= 0 body3 = @nd_body[blk] @needs_str_array = 1 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();") emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {") emit(" lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");") # Block-local C decl, see note in the int-block branch above. emit(" const char *lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");") @indent = @indent + 1 if blk >= 0 body3 = @nd_body[blk]test/bm_str_array_map.rbmodifiedView file
Loading diff…