Commit 5de7de3d

5de7de3d6c5fMerged

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

Description

fix(codegen): typed hash[k] || rhs returns rhs on missing key (#660)

Typed hash variants (sp_StrIntHash, sp_StrStrHash, sp_SymIntHash, sp_SymStrHash, sp_IntStrHash) materialize the value type's zero (0 / "") as the missing-key default. Combined with the || truthy- lhs ternary lowering, the rhs arm was unreachable: 0 and "" are both truthy in Ruby, so the always-present zero short-circuited the fallback. Diverges from MRI's nil-default Hash#[] semantics.

Fix: compile_or detects hash[k] on a typed-hash recv and rewrites to has_key(h, k) ? get(h, k) : rhs -- the lhs is evaluated only when the key is actually present. Poly-hash variants (StrPolyHash, SymPolyHash, PolyPolyHash) already return sp_box_nil on miss, so they keep the legacy truthy lowering.

Per-recv-kind key coercion (str / sym / int) routes through the same helper the standalone h[k] call uses (compile_str_arg0 / compile_arg0_as_sym / compile_arg0_as_int) so a sym->str widening shape lands the same emit as the non-|| path.

Closes #660.

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

Details

Commit
5de7de3d6c5f
Parent
3042b6324115

spinel_codegen.rbmodifiedView file

@@ -11645,6 +11645,43 @@
1164511645 "((" + expr + "), TRUE)"
1164611646 end
1164711647
11648+ # Issue #660 helper. If left_nid is `hash[k]` on a typed-hash recv
11649+ # with a non-poly value type, return "<Class>|<value_type>" so
11650+ # compile_or can rewrite to `has_key ? get : rhs`. Returns ""
11651+ # for shapes that should keep the legacy truthy lowering (poly
11652+ # hash variants already return sp_box_nil on miss; non-hash
11653+ # receivers have their own truthy semantics).
11654+ def lhs_typed_hash_get_for_or(left_nid)
11655+ if left_nid < 0 || @nd_type[left_nid] != "CallNode"
11656+ return ""
11657+ end
11658+ if @nd_name[left_nid] != "[]"
11659+ return ""
11660+ end
11661+ recv_nid_oh = @nd_receiver[left_nid]
11662+ args_id_oh = @nd_arguments[left_nid]
11663+ if recv_nid_oh < 0 || args_id_oh < 0
11664+ return ""
11665+ end
11666+ aargs_oh = get_args(args_id_oh)
11667+ if aargs_oh.length != 1
11668+ return ""
11669+ end
11670+ rt_oh = base_type(infer_type(recv_nid_oh))
11671+ if rt_oh == "str_int_hash"
11672+ return "StrIntHash|int|str"
11673+ elsif rt_oh == "str_str_hash"
11674+ return "StrStrHash|string|str"
11675+ elsif rt_oh == "sym_int_hash"
11676+ return "SymIntHash|int|sym"
11677+ elsif rt_oh == "sym_str_hash"
11678+ return "SymStrHash|string|sym"
11679+ elsif rt_oh == "int_str_hash"
11680+ return "IntStrHash|string|int"
11681+ end
11682+ ""
11683+ end
11684+
1164811685 def or_result_type(nid)
1164911686 lt = infer_type(@nd_left[nid])
1165011687 rt = infer_type(@nd_right[nid])
@@ -12370,6 +12407,50 @@
1237012407 lt = infer_type(left_nid)
1237112408 rt = infer_type(right_nid)
1237212409 ot = or_result_type(nid)
12410+ # Issue #660: `hash[k] || rhs` where the hash is a typed
12411+ # (non-poly) variant lowers the lhs to `sp_*Hash_get(h, k)` which
12412+ # returns the value type's zero (0 / "") on missing key. The truthy
12413+ # check on that zero is always TRUE (`((expr), TRUE)`), so the rhs
12414+ # arm is unreachable -- diverging from MRI's nil-default semantics.
12415+ #
12416+ # Special-case the shape: when the lhs is a CallNode `[]` on a
12417+ # typed-hash receiver, gate the ternary on `has_key`, not on the
12418+ # value's truthiness. The lhs is evaluated only when the key is
12419+ # present, so the call is single-eval safe. Poly-hash variants
12420+ # already return sp_box_nil on miss; skip them.
12421+ hash_or = lhs_typed_hash_get_for_or(left_nid)
12422+ if hash_or != ""
12423+ recv_nid_or = @nd_receiver[left_nid]
12424+ rc_or = compile_expr(recv_nid_or)
12425+ parts_or = hash_or.split("|")
12426+ cls_or = parts_or[0]
12427+ val_t_or = parts_or[1]
12428+ key_kind_or = parts_or[2]
12429+ # Reuse the per-recv-kind key coercer helpers so a sym->str
12430+ # widening (StrIntHash key from a SymbolNode arg) lands the
12431+ # same emit as the standalone `h[k]` call.
12432+ if key_kind_or == "str"
12433+ key_or = compile_str_arg0(left_nid)
12434+ elsif key_kind_or == "sym"
12435+ key_or = compile_arg0_as_sym(left_nid)
12436+ elsif key_kind_or == "int"
12437+ key_or = compile_arg0_as_int(left_nid)
12438+ else
12439+ args_id_or_fallback = @nd_arguments[left_nid]
12440+ aa_or_fallback = get_args(args_id_or_fallback)
12441+ key_or = compile_expr(aa_or_fallback[0])
12442+ end
12443+ right_expr = compile_expr(right_nid)
12444+ if ot == "poly"
12445+ @needs_rb_value = 1
12446+ lhs_box_or = box_value_to_poly(val_t_or, "sp_" + cls_or + "_get(" + rc_or + ", " + key_or + ")")
12447+ right_box_or = base_type(rt) == "poly" ? right_expr : box_value_to_poly(rt, right_expr)
12448+ else
12449+ lhs_box_or = "sp_" + cls_or + "_get(" + rc_or + ", " + key_or + ")"
12450+ right_box_or = right_expr
12451+ end
12452+ return "(sp_" + cls_or + "_has_key(" + rc_or + ", " + key_or + ") ? " + lhs_box_or + " : " + right_box_or + ")"
12453+ end
1237312454 if ot != "bool" || lt != "bool" || rt != "bool"
1237412455 if ot == "poly"
1237512456 @needs_rb_value = 1