Commit 5baef77b

5baef77b8fb8Merged

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

Description

analyze,codegen: Hash#key and #value?/#has_value? on typed hashes

Walk order[] linearly: `key(v)` returns the first matching key (sym/str sentinel on miss), `value?` / `has_value?` returns bool. Variants wired: sym_int_hash, sym_str_hash, str_int_hash, str_str_hash. Poly-value and int-keyed variants share the shape and can be added when callsites surface.

Fixes #956

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

Details

Commit
5baef77b8fb8
Parent
97d744103498

spinel_analyze.rbmodifiedView file

@@ -3638,6 +3638,20 @@
36383638 # Negation of `=~`: bool. Issue #732.
36393639 return "bool"
36403640 end
3641+ if mname == "key"
3642+ # Hash#key(value) — returns the first key whose value == value.
3643+ # For sym-keyed hashes the result is a symbol; missing-key
3644+ # returns the empty-sym sentinel.
3645+ if recv >= 0
3646+ rt_hk = infer_type(recv)
3647+ if rt_hk == "sym_int_hash" || rt_hk == "sym_str_hash" || rt_hk == "sym_poly_hash"
3648+ return "symbol"
3649+ end
3650+ if rt_hk == "str_int_hash" || rt_hk == "str_str_hash" || rt_hk == "str_poly_hash"
3651+ return "string"
3652+ end
3653+ end
3654+ end
36413655 if mname == "value?" || mname == "has_value?"
36423656 # Hash#value?(v) -- returns bool. Issue #738.
36433657 if recv >= 0