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_int for int keys, const char * for string keys, sp_sym for sym keys).
  • Wrap the body in push_scope / declare_var(bp, key_type) / pop_scope so infer_type inside 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

@@ -22273,17 +22273,42 @@
2227322273 has_bp = 0
2227422274 bp1 = "_x"
2227522275 end
22276+ # Pick the key type. Sym-keyed hashes store the sym id in
22277+ # `order[]` as a plain mrb_int — cast to sp_sym so block-body
22278+ # dispatchers see the right type.
22279+ is_sym_key = 0
22280+ if ht == "sym_int_hash" || ht == "sym_str_hash" || ht == "sym_poly_hash"
22281+ is_sym_key = 1
22282+ end
22283+ is_str_key = 0
22284+ if ht == "str_int_hash" || ht == "str_str_hash" || ht == "str_poly_hash"
22285+ is_str_key = 1
22286+ end
22287+ key_type = "int"
22288+ if is_sym_key == 1
22289+ key_type = "symbol"
22290+ elsif is_str_key == 1
22291+ key_type = "string"
22292+ end
2227622293 tmp = new_temp
2227722294 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < " + hrc + "->len; " + tmp + "++) {")
22295+ # Inline-declare `lv_<bp1>` with the key's C type so a block
22296+ # param that shadows an outer same-named local of a different
22297+ # type compiles correctly (mirrors PR #115's pattern).
2227822298 if has_bp == 1
22279- if ht == "sym_int_hash" || ht == "sym_str_hash" || ht == "sym_poly_hash"
22280- emit(" lv_" + bp1 + " = (sp_sym)" + hrc + "->order[" + tmp + "];")
22299+ if is_sym_key == 1
22300+ emit(" sp_sym lv_" + bp1 + " = (sp_sym)" + hrc + "->order[" + tmp + "];")
2228122301 else
22282- emit(" lv_" + bp1 + " = " + hrc + "->order[" + tmp + "];")
22302+ emit(" " + c_type(key_type) + " lv_" + bp1 + " = " + hrc + "->order[" + tmp + "];")
2228322303 end
2228422304 end
2228522305 @indent = @indent + 1
22306+ push_scope
22307+ if has_bp == 1
22308+ declare_var(bp1, key_type)
22309+ end
2228622310 compile_stmts_body(@nd_body[@nd_block[nid]])
22311+ pop_scope
2228722312 @indent = @indent - 1
2228822313 emit(" }")
2228922314 @in_loop = old