Commit 4062f826

4062f8264c14Merged

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

Description

codegen: Hash#merge with conflict-resolution block (sym_int_hash)

`h1.merge(h2) { |k, v1, v2| ... }` now invokes the block on each colliding key instead of silently letting h2's value win. Implementation walks h2's order, looks up the key in a duped h1, binds the (key, v1, v2) triple, and writes the block's return.

Only sym_int_hash + sym_int_hash with an int-returning block is wired up here; other variant combinations still fall through to the no-block helper. Other shapes can land alongside as the inline merge body is per-variant boilerplate.

Fixes #913

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

Details

Commit
4062f8264c14
Parent
6d7b2ae1edc4

spinel_codegen.rbmodifiedView file

@@ -22360,6 +22360,60 @@
2236022360 # merge / dup / delete -- sibling of #510 (which fixed
2236122361 # sym_str_hash / sym_poly_hash) but for sym_int_hash. Issue
2236222362 # #546.
22363+ if mname == "merge" && @nd_block[nid] >= 0
22364+ # `h1.merge(h2) { |k, v1, v2| ... }` — conflict-resolution
22365+ # block form. Only support sym_int_hash + sym_int_hash with
22366+ # an int-returning block (most common shape); other variant
22367+ # combinations fall through to the block-less path below.
22368+ args_id_mb = @nd_arguments[nid]
22369+ if args_id_mb >= 0
22370+ a_mb = get_args(args_id_mb)
22371+ if a_mb.length >= 1 && infer_type(a_mb[0]) == "sym_int_hash"
22372+ arg_mb = compile_expr_gc_rooted(a_mb[0])
22373+ result_mb = new_temp
22374+ iter_mb = new_temp
22375+ bp_k_mb = get_block_param(nid, 0)
22376+ bp_k_mb = "_mk" if bp_k_mb == ""
22377+ bp_v1_mb = get_block_param(nid, 1)
22378+ bp_v1_mb = "_mv1" if bp_v1_mb == ""
22379+ bp_v2_mb = get_block_param(nid, 2)
22380+ bp_v2_mb = "_mv2" if bp_v2_mb == ""
22381+ emit(" sp_SymIntHash *" + result_mb + " = sp_SymIntHash_dup(" + rc + ");")
22382+ emit(" SP_GC_ROOT(" + result_mb + ");")
22383+ emit(" for (mrb_int " + iter_mb + " = 0; " + iter_mb + " < " + arg_mb + "->len; " + iter_mb + "++) {")
22384+ emit(" sp_sym lv_" + bp_k_mb + " = " + arg_mb + "->order[" + iter_mb + "];")
22385+ emit(" mrb_int lv_" + bp_v2_mb + " = sp_SymIntHash_get(" + arg_mb + ", lv_" + bp_k_mb + ");")
22386+ emit(" if (sp_SymIntHash_has_key(" + result_mb + ", lv_" + bp_k_mb + ")) {")
22387+ emit(" mrb_int lv_" + bp_v1_mb + " = sp_SymIntHash_get(" + result_mb + ", lv_" + bp_k_mb + ");")
22388+ @indent = @indent + 2
22389+ push_scope
22390+ declare_var(bp_k_mb, "symbol")
22391+ declare_var(bp_v1_mb, "int")
22392+ declare_var(bp_v2_mb, "int")
22393+ bbody_mb = @nd_body[@nd_block[nid]]
22394+ bexpr_mb = "0"
22395+ if bbody_mb >= 0
22396+ bs_mb = get_stmts(bbody_mb)
22397+ k_mb = 0
22398+ while k_mb < bs_mb.length - 1
22399+ compile_stmt(bs_mb[k_mb])
22400+ k_mb = k_mb + 1
22401+ end
22402+ if bs_mb.length > 0
22403+ bexpr_mb = compile_expr(bs_mb.last)
22404+ end
22405+ end
22406+ emit(" sp_SymIntHash_set(" + result_mb + ", lv_" + bp_k_mb + ", " + bexpr_mb + ");")
22407+ pop_scope
22408+ @indent = @indent - 2
22409+ emit(" } else {")
22410+ emit(" sp_SymIntHash_set(" + result_mb + ", lv_" + bp_k_mb + ", lv_" + bp_v2_mb + ");")
22411+ emit(" }")
22412+ emit(" }")
22413+ return result_mb
22414+ end
22415+ end
22416+ end
2236322417 if mname == "merge"
2236422418 args_id_m546 = @nd_arguments[nid]
2236522419 if args_id_m546 >= 0