Commit 27ea6b11
27ea6b1158c8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 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
end54595459
if mname == "transform_keys"54605460
if recv >= 05461-
return infer_type(recv)5461+
rt_tk = infer_type(recv)5462+
# Probe block return to detect key-type swap. sym → string swaps5463+
# sym_*_hash to str_*_hash; same key type keeps the receiver shape.5464+
blk_tk_a = @nd_block[nid]5465+
if blk_tk_a >= 05466+
bbody_tk_a = @nd_body[blk_tk_a]5467+
if bbody_tk_a >= 05468+
bs_tk_a = get_stmts(bbody_tk_a)5469+
if bs_tk_a.length > 05470+
bp_tk_a = get_block_param(nid, 0)5471+
push_scope5472+
if bp_tk_a != ""5473+
declare_var(bp_tk_a, hash_key_part(rt_tk) == "sym" ? "symbol" : "string")5474+
end5475+
bret_tk_a = infer_type(bs_tk_a.last)5476+
pop_scope5477+
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+
end5482+
end5483+
end5484+
end5485+
return rt_tk54625486
end54635487
return "str_int_hash"54645488
endspinel_codegen.rbmodifiedView file
Loading diff…
test/hash_transform_keys_cross.rbaddedView file
Loading diff…
test/hash_transform_keys_cross.rb.expectedaddedView file
Loading diff…