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

@@ -3808,6 +3808,16 @@
38083808 if def_at_f == "string" || def_at_f == "mutable_str"
38093809 return "string"
38103810 end
3811+ # Hash-typed default (`fetch "k", {}`). Sequel to the
3812+ # string-default case above: the two ternary arms are int (from
3813+ # the get) and a hash pointer (from the default literal), with
3814+ # no shared primitive type. Box both to sp_RbVal and surface
3815+ # the call's result as poly. Limited to the int-leaf hash
3816+ # variants for now; broader receivers can extend the pattern.
3817+ if is_hash_type(def_at_f) == 1
3818+ @needs_rb_value = 1
3819+ return "poly"
3820+ end
38113821 end
38123822 end
38133823 end