Commit 74612b16

74612b1650c6Merged

Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

codegen: Hash#fetch(k) { default } runs block on miss

{a:1}.fetch(:missing) { 99 } previously dropped the block: the per-variant fetch arms in compile_hash_method_expr only covered the 2-arg form (fetch(k, default)) and 1-arg form (fetch(k) — raises). With a block attached the call still matched the 1-arg arm and emitted sp_<Variant>_get(...), which returns 0 (the default sentinel) instead of running the block.

Add a shared block-form fetch arm before the per-variant blocks. It emits a conditional that picks _get when the key is present and routes through compile_body_into for the block body when it's missing. compile_body_into already handles multi-statement blocks (assigns the last expression to the temp), so fetch(k) { x = 1; x + 2 } works without extra plumbing.

A small hash_fetch_block_info helper maps each variant to its _has_key / _get pair and the value's analyzer type (needed by compile_body_into for type-conversion at the last expression). Currently covers sym_int / sym_str / sym_poly / str_int / str_str / str_poly. int-keyed and poly_poly variants left for follow-up — their key-arg compilation differs (compile_arg0_as_int / box_expr_to_poly).

Also extend compile_body_into to box the last expression to sp_RbVal when the caller's return_type is poly and the expression's inferred type isn't already poly. Without this the sym_poly / str_poly arms emitted _t = ("\xff"...)[1] (a sp_RbStr *) into a sp_RbVal slot and cc rejected the assignment as incompatible — symmetric to the existing bigint↔int conversion already in that helper.

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

Details

Commit
74612b1650c6
Parent
24ec1cf637a4

spinel_codegen.rbmodifiedView file

@@ -5167,6 +5167,20 @@
51675167 ""
51685168 end
51695169
5170+ # Per-variant info for the shared Hash#fetch(k) { block } arm.
5171+ # Returns a 4-tuple [value_type, value_c_type, has_key_fn,
5172+ # get_fn] or nil for unsupported variants. The block-form
5173+ # arm uses these to emit the runtime check + dispatch.
5174+ def hash_fetch_block_info(t)
5175+ if t == "sym_int_hash"; return ["int", "mrb_int", "sp_SymIntHash_has_key", "sp_SymIntHash_get"]; end
5176+ if t == "sym_str_hash"; return ["string", "const char *", "sp_SymStrHash_has_key", "sp_SymStrHash_get"]; end
5177+ if t == "sym_poly_hash"; return ["poly", "sp_RbVal", "sp_SymPolyHash_has_key", "sp_SymPolyHash_get"]; end
5178+ if t == "str_int_hash"; return ["int", "mrb_int", "sp_StrIntHash_has_key", "sp_StrIntHash_get"]; end
5179+ if t == "str_str_hash"; return ["string", "const char *", "sp_StrStrHash_has_key", "sp_StrStrHash_get"]; end
5180+ if t == "str_poly_hash"; return ["poly", "sp_RbVal", "sp_StrPolyHash_has_key", "sp_StrPolyHash_get"]; end
5181+ nil
5182+ end
5183+
51705184 # CRuby returns nil for static mismatches (e.g. `{a: 1}.dig("a")`)
51715185 # since no key compares equal — Hash#dig short-circuits to nil here.
51725186 def hash_key_matches_recv(recv_type, key_type)
@@ -22110,6 +22124,36 @@
2211022124 return ins_h
2211122125 end
2211222126 end
22127+ # Hash#fetch(k) { |k| default } — block form. `compile_body_into`
22128+ # walks the block body emitting all statements; the final
22129+ # expression assigns into a temp that we return. The non-block
22130+ # 2-arg fetch (default value) stays in the per-variant arms
22131+ # below; this arm is for the block-default form which they
22132+ # didn't cover.
22133+ if mname == "fetch" && @nd_block[nid] >= 0
22134+ fi = hash_fetch_block_info(recv_type)
22135+ if fi != nil
22136+ args_id_fb = @nd_arguments[nid]
22137+ if args_id_fb >= 0
22138+ aargs_fb = get_args(args_id_fb)
22139+ if aargs_fb.length >= 1
22140+ key_fb = compile_expr(aargs_fb[0])
22141+ tmp_fb = new_temp
22142+ emit(" " + fi[1] + " " + tmp_fb + ";")
22143+ emit(" if (" + fi[2] + "(" + rc + ", " + key_fb + ")) {")
22144+ emit(" " + tmp_fb + " = " + fi[3] + "(" + rc + ", " + key_fb + ");")
22145+ emit(" } else {")
22146+ @indent = @indent + 1
22147+ push_scope
22148+ compile_body_into(@nd_body[@nd_block[nid]], tmp_fb, fi[0])
22149+ pop_scope
22150+ @indent = @indent - 1
22151+ emit(" }")
22152+ return tmp_fb
22153+ end
22154+ end
22155+ end
22156+ end
2211322157 if recv_type == "sym_int_hash"
2211422158 if mname == "[]"
2211522159 args_id0 = @nd_arguments[nid]
@@ -42366,6 +42410,8 @@
4236642410 elsif base_type(return_type) == "int" && last_t_cbi == "bigint"
4236742411 @needs_bigint = 1
4236842412 expr = "sp_bigint_to_int((sp_Bigint *)" + expr + ")"
42413+ elsif base_type(return_type) == "poly" && last_t_cbi != "poly" && last_t_cbi != ""
42414+ expr = box_value_to_poly(last_t_cbi, expr)
4236942415 end
4237042416 emit(" " + ret_tmp + " = " + expr + ";")
4237142417 end