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

@@ -20310,6 +20310,26 @@
2031020310 recv_t = infer_type(recv)
2031120311 end
2031220312 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"
20313+ # Hash#each yields |k, v|. elem_type_of_array on a hash type
20314+ # falls back to "int" for both, which then types `puts k + ":"`
20315+ # inside the block as int-arithmetic and lowers to printf("%lld")
20316+ # with a raw `+` between pointers. Pick the hash's key/value
20317+ # part directly so block_param_type_at returns the right per-
20318+ # position type. hash_key_part/hash_value_part return the
20319+ # short ("str"/"sym"/"int") tags; expand to full type names.
20320+ if is_hash_type(recv_t) == 1
20321+ if pi == 0
20322+ kp = hash_key_part(recv_t)
20323+ return "string" if kp == "str"
20324+ return "symbol" if kp == "sym"
20325+ return "int" if kp == "int"
20326+ return "poly"
20327+ end
20328+ vp = hash_value_part(recv_t)
20329+ return "string" if vp == "str"
20330+ return "int" if vp == "int"
20331+ return "poly"
20332+ end
2031320333 return elem_type_of_array(recv_t)
2031420334 end
2031520335 if mname == "each_with_index"