Commit 27ea6b11

27ea6b1158c8Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

analyze,codegen: Hash#transform_keys with sym → string block (sym_int_hash → str_int_hash)

sym_int_hash had no transform_keys arm; the unresolved-call path emitted `(mrb_int)0` into a sym_int_hash * slot which crashed at C compile. Add the arm — probe the block return type and either keep the sym-keyed variant (sym return) or build a str-keyed twin (string return).

analyze tracks the same swap: `sym → str_int_hash`, `sym → str_str_hash`, `sym → str_poly_hash` for the corresponding sym-keyed receivers. Same-type blocks fall through to the recv type.

Fixes #945

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

Details

Commit
27ea6b1158c8
Parent
d2f690be29e5

spinel_analyze.rbmodifiedView file

@@ -5458,7 +5458,31 @@
54585458 end
54595459 if mname == "transform_keys"
54605460 if recv >= 0
5461- return infer_type(recv)
5461+ rt_tk = infer_type(recv)
5462+ # Probe block return to detect key-type swap. sym → string swaps
5463+ # sym_*_hash to str_*_hash; same key type keeps the receiver shape.
5464+ blk_tk_a = @nd_block[nid]
5465+ if blk_tk_a >= 0
5466+ bbody_tk_a = @nd_body[blk_tk_a]
5467+ if bbody_tk_a >= 0
5468+ bs_tk_a = get_stmts(bbody_tk_a)
5469+ if bs_tk_a.length > 0
5470+ bp_tk_a = get_block_param(nid, 0)
5471+ push_scope
5472+ if bp_tk_a != ""
5473+ declare_var(bp_tk_a, hash_key_part(rt_tk) == "sym" ? "symbol" : "string")
5474+ end
5475+ bret_tk_a = infer_type(bs_tk_a.last)
5476+ pop_scope
5477+ if bret_tk_a == "string"
5478+ return "str_int_hash" if rt_tk == "sym_int_hash" || rt_tk == "str_int_hash"
5479+ return "str_str_hash" if rt_tk == "sym_str_hash" || rt_tk == "str_str_hash"
5480+ return "str_poly_hash" if rt_tk == "sym_poly_hash" || rt_tk == "str_poly_hash"
5481+ end
5482+ end
5483+ end
5484+ end
5485+ return rt_tk
54625486 end
54635487 return "str_int_hash"
54645488 end