Commit 40788c85

40788c85cf7aMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

Type Hash#fetch block param and result by the hash key/value shape

hash.fetch(key) { |k| ... } yields the looked-up (missing) key to the block. The block param was scope-typed as the default int, so on a sym- or string-keyed hash the body (k.to_s, k + "!") mis-dispatched as Integer operations and produced garbage. The result type was also fixed to the hash value type, so an int-value hash with a string- returning block printed the int temp via %lld.

Type the fetch block param as the hash's KEY type (string / symbol; int-keyed hashes keep the correct int default) in both block-param scope-typing sites: block_param_type_at (top-level / walk_and_cache) and scan_locals (method bodies). For an int-VALUE hash whose block returns a string, surface the fetch result as string and coerce the present-branch get via sp_int_to_s, mirroring the existing 2-arg fetch(k, "default") int->string convention. Restricted to the one convertible mismatch; poly- and string-value hashes keep their prior result types.

Self-host safe: the compiler sources contain no fetch-with-block on a typed hash (grep-verified), so the re-typing cannot reach the inference fixpoint.

Closes #1339

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

Details

Commit
40788c85cf7a
Parent
947d6fbe502b

spinel_analyze.rbmodifiedView file

@@ -5351,6 +5351,35 @@
53515351 if mname == "fetch"
53525352 if recv >= 0
53535353 rt = infer_type(recv)
5354+ # `hash.fetch(k) { |key| ... }` block form on an int-VALUE hash: on a
5355+ # miss the result is the block's value, which may be a string (e.g.
5356+ # `h.fetch(k) { |key| key.to_s }` returns a string from an int-value
5357+ # hash). Bind the block param to the hash's key type, and when the
5358+ # block body's last-expr is a string, surface the result as string
5359+ # (codegen coerces the present-branch int via sp_int_to_s, mirroring
5360+ # the 2-arg `fetch(k, "default")` convention). Restricted to int-value
5361+ # variants + string block bodies — the only convertible mismatch; poly
5362+ # and string-value hashes keep their existing result types below.
5363+ if @nd_block[nid] >= 0 && (rt == "str_int_hash" || rt == "sym_int_hash" || rt == "int_int_hash")
5364+ blk_fb = @nd_block[nid]
5365+ bbody_fb = @nd_body[blk_fb]
5366+ if bbody_fb >= 0
5367+ bbs_fb = get_stmts(bbody_fb)
5368+ if bbs_fb.length > 0
5369+ key_t_fb = "int"
5370+ key_t_fb = "string" if rt.start_with?("str_")
5371+ key_t_fb = "symbol" if rt.start_with?("sym_")
5372+ bp_fb = get_block_param(nid, 0)
5373+ push_scope
5374+ declare_var(bp_fb, key_t_fb) if bp_fb != "" && bp_fb != "_"
5375+ brt_fb = infer_type(bbs_fb.last)
5376+ pop_scope
5377+ if brt_fb == "string"
5378+ return "string"
5379+ end
5380+ end
5381+ end
5382+ end
53545383 # Typed-array fetch(i): the element is proven-present (Array#fetch
53555384 # raises IndexError on a miss, it never returns nil), so the result
53565385 # is the plain element type, not the nullable `[]` element type
@@ -33060,6 +33089,13 @@
3306033089 # consumer.
3306133090 elsif bk == 1 && @nd_receiver[nid] >= 0 && @nd_type[@nd_receiver[nid]] == "CallNode" && @nd_name[@nd_receiver[nid]] == "with_index" && @nd_block[@nd_receiver[nid]] < 0
3306233091 types.push("int")
33092+ # `hash.fetch(k) { |key| ... }` yields the missing key. The block
33093+ # param is the hash's KEY type, not the default int. int-keyed
33094+ # hashes keep the int default (correct). Mirrors block_param_type_at.
33095+ elsif mname == "fetch" && bk == 0 && is_hash_type(recv_type) == 1 && recv_type.start_with?("str_")
33096+ types.push("string")
33097+ elsif mname == "fetch" && bk == 0 && is_hash_type(recv_type) == 1 && recv_type.start_with?("sym_")
33098+ types.push("symbol")
3306333099 elsif mname == "each" || mname == "each_pair" || mname == "map" || mname == "select" || mname == "filter" || mname == "reject" || mname == "find" || mname == "detect" || mname == "any?" || mname == "all?" || mname == "none?" || mname == "one?" || mname == "count" || mname == "min" || mname == "max" || mname == "sum" || mname == "min_by" || mname == "max_by" || mname == "sort_by" || mname == "flat_map" || mname == "filter_map" || mname == "cycle" || mname == "partition"
3306433100 # Element iteration: infer block param from collection type
3306533101 if recv_type == "str_array"
@@ -35451,6 +35487,15 @@
3545135487 if pi == 1 && recv >= 0 && @nd_type[recv] == "CallNode" && @nd_name[recv] == "with_index" && @nd_block[recv] < 0
3545235488 return "int"
3545335489 end
35490+ # `hash.fetch(k) { |key| ... }` yields the looked-up (missing) key
35491+ # to the block. The single block param is the hash's KEY type, not
35492+ # the default int. Without this, a sym/string-keyed hash types the
35493+ # param as int and the body (`key.to_s`, `key + "!"`) mis-dispatches
35494+ # as Integer ops. int-keyed hashes keep the int default (correct).
35495+ if mname == "fetch" && pi == 0 && is_hash_type(recv_t) == 1
35496+ return "string" if recv_t.start_with?("str_")
35497+ return "symbol" if recv_t.start_with?("sym_")
35498+ end
3545435499 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"
3545535500 # `<arr>.group_by(blk).each do |k, rows|` -- the fused emit
3545635501 # in codegen unboxes the per-key accumulator to a typed