Commit 40b88f06
40b88f06a99eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(analyze,codegen): hash#each block params typed from key/value parts
`h.each do |k, v|` with a typed hash receiver (e.g. str_int_hash) previously had block params typed via `elem_type_of_array(recv_type)`. That helper only knows about typed arrays; for a hash receiver it fell back to "int" for both positions. Code inside the block like `puts k + ": " + v.to_s` then had walk_and_cache infer `k` as int, and the chained `+` cached as int, so `puts` lowered to `printf("%lld", (long long)(string_concat + int_to_s))` — both the cast and the raw `+` between pointers fail C compile.
Two coordinated changes:
-
analyze: block_param_type_at's each-family arm branches on is_hash_type. For hash receivers it pulls hash_key_part / hash_value_part and expands the short tag to the full type name (str→string, sym→symbol, int→int, poly→poly).
-
codegen: infer_type's CallNode cache-miss path learned that `+` on a string receiver returns string, and that `.to_s` returns string. walk_and_cache skips block bodies of proc/lambda but does walk `each` block bodies; nodes inside still cache-miss on the analyze side because of other shapes (LocalVariableRead skip-cache for narrows, etc.), so the codegen fallback has to be correct too.
Side effects:
- `make bench` goes from 50 pass / 2 error to 52 pass / 0 error: bm_csv_process and bm_io_wordcount both used `puts k + ": " + v.to_s` inside hash-iteration blocks.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 40b88f06a99e
- Parent
- 0c48839cc3ca
spinel_analyze.rbmodifiedView file
recv_t = infer_type(recv) end if mname == "each" || mname == "map" || mname == "flat_map" || mname == "filter" || mname == "select" || mname == "reject" || mname == "find" || mname == "detect" || mname == "find_index" || mname == "find_all" || mname == "count" || mname == "all?" || mname == "any?" || mname == "none?" || mname == "min_by" || mname == "max_by" || mname == "sort_by" || mname == "group_by" || mname == "partition" || mname == "uniq" || mname == "tally" || mname == "drop_while" || mname == "take_while" || mname == "filter_map" # Hash#each yields |k, v|. elem_type_of_array on a hash type # falls back to "int" for both, which then types `puts k + ":"` # inside the block as int-arithmetic and lowers to printf("%lld") # with a raw `+` between pointers. Pick the hash's key/value # part directly so block_param_type_at returns the right per- # position type. hash_key_part/hash_value_part return the # short ("str"/"sym"/"int") tags; expand to full type names. if is_hash_type(recv_t) == 1 if pi == 0 kp = hash_key_part(recv_t) return "string" if kp == "str" return "symbol" if kp == "sym" return "int" if kp == "int" return "poly" end vp = hash_value_part(recv_t) return "string" if vp == "str" return "int" if vp == "int" return "poly" end return elem_type_of_array(recv_t) end if mname == "each_with_index"spinel_codegen.rbmodifiedView file
Loading diff…
test/hash_each_block_param_types.rbaddedView file
Loading diff…
test/hash_each_block_param_types.rb.expectedaddedView file
Loading diff…