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
if mname == "fetch" if recv >= 0 rt = infer_type(recv) # int-leaf hash with string default — Ruby's `params.fetch # "k", ""` idiom. The leaf int is convertible to string via # sp_int_to_s, so surface the result as string; codegen-side # emits the conversion in the get arm. Limited to int-leaf # variants and string-typed defaults; broader (poly leaf, # hash default, etc.) widening cascades through downstream # `is_a?(Hash)` narrowing in real-blog params and is deferred. if rt == "str_int_hash" || rt == "sym_int_hash" fargs_id_f = @nd_arguments[nid] if fargs_id_f >= 0 fargs_f = get_args(fargs_id_f) if fargs_f.length >= 2 def_at_f = infer_type(fargs_f[1]) if def_at_f == "string" || def_at_f == "mutable_str" return "string" end end end end if rt == "str_str_hash" || rt == "sym_str_hash" || rt == "int_str_hash" return "string" endspinel_codegen.rbmodifiedView file
Loading diff…
test/hash_fetch_string_default_on_int_leaf.rbaddedView file
Loading diff…