Commit ee464d52

ee464d525a9bMerged

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

Description

fix: Hash#fetch with string default on int-leaf hash

#454. `params.fetch "k", ""` on a Hash[String, Int] (or Hash[Symbol, Int]) emitted a type-mismatched ternary

(has_key ? sp_StrIntHash_get(...) : \"\")
  // mrb_int                : const char *

and failed C compile under -Werror=int-conversion.

Narrow fix: when the default is a string-typed expression and the receiver's value leaf is int, widen the static return type to "string" and route the get arm through sp_int_to_s so both arms agree on `const char *`. Both the str-keyed and sym-keyed variants are covered.

Limited scope: broader cases (poly leaf, hash-typed defaults, e.g. `params.fetch("k", {})`) cascade through downstream `is_a?(Hash)` narrowing in real-blog params — the scan_locals narrowing decision for the receiving LV slot tracks the original leaf type, so widening fetch's return there produces incompatible- pointer assignments at the LV write site. That path is deferred until scan_locals' narrowing-vs-poly interaction is properly addressed; the issue's primary repro (int leaf + string default, 13 of 19 archive errors per the issue body) is closed.

Closes #454

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
ee464d525a9b
Parent
d56852703c56

spinel_analyze.rbmodifiedView file

@@ -3749,6 +3749,25 @@
37493749 if mname == "fetch"
37503750 if recv >= 0
37513751 rt = infer_type(recv)
3752+ # int-leaf hash with string default — Ruby's `params.fetch
3753+ # "k", ""` idiom. The leaf int is convertible to string via
3754+ # sp_int_to_s, so surface the result as string; codegen-side
3755+ # emits the conversion in the get arm. Limited to int-leaf
3756+ # variants and string-typed defaults; broader (poly leaf,
3757+ # hash default, etc.) widening cascades through downstream
3758+ # `is_a?(Hash)` narrowing in real-blog params and is deferred.
3759+ if rt == "str_int_hash" || rt == "sym_int_hash"
3760+ fargs_id_f = @nd_arguments[nid]
3761+ if fargs_id_f >= 0
3762+ fargs_f = get_args(fargs_id_f)
3763+ if fargs_f.length >= 2
3764+ def_at_f = infer_type(fargs_f[1])
3765+ if def_at_f == "string" || def_at_f == "mutable_str"
3766+ return "string"
3767+ end
3768+ end
3769+ end
3770+ end
37523771 if rt == "str_str_hash" || rt == "sym_str_hash" || rt == "int_str_hash"
37533772 return "string"
37543773 end