Commit cdcfafbd
cdcfafbd0378Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
fix: Hash#fetch with hash default on int-leaf hash
#463. Sequel to #454 which closed the string-default case via
sp_int_to_s conversion. The hash-default case (fetch "k", {})
can't unify the two ternary arms to a single primitive: the get
returns int and the default literal is a hash pointer.
Approach: box both arms to sp_RbVal and surface the return type as poly so the caller's receiving slot widens.
- analyze infer_call_type for
fetch: when receiver is int- leaf hash and the second arg's type is itself a hash, return poly. - codegen str_int_hash fetch arm: when default's static type
is a hash, emit
(has_key ? sp_box_int(get) : box_value_to_poly(default)).
Limited to int-leaf hash receivers; broader cases (poly-leaf
fetch, int default on str-leaf, etc.) deferred. Real-blog 2
archive errors (ArticleParams.from_raw / CommentParams.from_raw
fetch "article", {}) close.
Closes #463
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- cdcfafbd0378
- Parent
- 4e8f9556869c
spinel_analyze.rbmodifiedView file
if def_at_f == "string" || def_at_f == "mutable_str" return "string" end # Hash-typed default (`fetch "k", {}`). Sequel to the # string-default case above: the two ternary arms are int (from # the get) and a hash pointer (from the default literal), with # no shared primitive type. Box both to sp_RbVal and surface # the call's result as poly. Limited to the int-leaf hash # variants for now; broader receivers can extend the pattern. if is_hash_type(def_at_f) == 1 @needs_rb_value = 1 return "poly" end end end endspinel_codegen.rbmodifiedView file
Loading diff…
test/hash_fetch_hash_default.rbaddedView file
Loading diff…