Commit 3fbc0421
3fbc04216ea2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): give hash.keys.each fusion a typed inline lv_ + scope
PR #120's hash.keys.each fusion emitted the block param as a bare
assignment (lv_k = hash->order[i]), so a block param shadowing an
outer same-named local of a different type re-used the outer C
declaration:
k = "outer-string" h = {1 => "a"}.keys.each do |k| puts k * 10 end
Pre-fix, lv_k was declared as const char * at the outer scope; the
fusion assigned mrb_int into it, gcc warned, and the block body
read garbage. Mirror PR #115's pattern that already covers the
non-fused iteration path:
- Inline-declare
lv_<bp>with the key's C type (mrb_intfor int keys,const char *for string keys,sp_symfor sym keys). - Wrap the body in
push_scope/declare_var(bp, key_type)/pop_scopesoinfer_typeinside the body resolves the param to the inner key type, not the outer same-named local.
test/hash_keys_each_shadow.rb covers all three key kinds with an
outer same-named local of a different type.
Refs #120 #115
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 3fbc04216ea2
- Parent
- 7cc1842cbc5c
spinel_codegen.rbmodifiedView file
has_bp = 0 bp1 = "_x" end # Pick the key type. Sym-keyed hashes store the sym id in # `order[]` as a plain mrb_int — cast to sp_sym so block-body # dispatchers see the right type. is_sym_key = 0 if ht == "sym_int_hash" || ht == "sym_str_hash" || ht == "sym_poly_hash" is_sym_key = 1 end is_str_key = 0 if ht == "str_int_hash" || ht == "str_str_hash" || ht == "str_poly_hash" is_str_key = 1 end key_type = "int" if is_sym_key == 1 key_type = "symbol" elsif is_str_key == 1 key_type = "string" end tmp = new_temp emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < " + hrc + "->len; " + tmp + "++) {") # Inline-declare `lv_<bp1>` with the key's C type so a block # param that shadows an outer same-named local of a different # type compiles correctly (mirrors PR #115's pattern). if has_bp == 1 if ht == "sym_int_hash" || ht == "sym_str_hash" || ht == "sym_poly_hash" emit(" lv_" + bp1 + " = (sp_sym)" + hrc + "->order[" + tmp + "];") if is_sym_key == 1 emit(" sp_sym lv_" + bp1 + " = (sp_sym)" + hrc + "->order[" + tmp + "];") else emit(" lv_" + bp1 + " = " + hrc + "->order[" + tmp + "];") emit(" " + c_type(key_type) + " lv_" + bp1 + " = " + hrc + "->order[" + tmp + "];") end end @indent = @indent + 1 push_scope if has_bp == 1 declare_var(bp1, key_type) end compile_stmts_body(@nd_body[@nd_block[nid]]) pop_scope @indent = @indent - 1 emit(" }") @in_loop = oldtest/hash_keys_each_shadow.rbaddedView file
Loading diff…