Commit e1ee0aa0

e1ee0aa0879dMerged

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

Description

Return value-level nil for str_int_hash missing keys (#801 Phase 4)

A missing-key read of a String=>Integer typed hash (h["miss"]) now returns Ruby nil at the value level (int?), not 0, including when the read is stored in a local and observed later (v = h["miss"]; v.nil?), the case the existing consumption-site rewrites could not reach.

Done narrowing-first to avoid the self-host cascade that reverted the 2026-06-03 attempt. The cascade is bounded and all-or-nothing: any single int? injection into the compiler's own typed-int-hash reads degrades the infer_type / infer_method_name_type / unify_call_types return-and-param lattice to poly. Most injection sites are empty-{} string caches the fixpoint transiently types str_int_hash before the string writes widen them; a transient int? is sticky (nullable-preservation) where plain int is not, so it latches into infer_type's return. fetch yields int/string only, so the cascade never forms.

  • Type single-arg fetch on str_int_hash/sym_int_hash as int (the checked read), instead of falling through to "".
  • Migrate the compiler's own logically-present typed-int-hash reads to fetch / has_key? (mname_to_id, parse_id_list, the rbs-ptype and ivar/method-return/ctor-yield caches, etc.) so they stay int.
  • Extend infer_nil_guard_narrow_type to narrow x = h[k]; return if x.nil? to int for typed-int-hash reads.
  • Flip the str_int_hash [] read to int? and emit sp_StrIntHash_get_opt.
  • sp_StrIntHash_new seeds default_v = SP_INT_NIL (no explicit default -> miss is nil); Hash.new(N) keeps its default via _new_with_default, so Hash.new(0)["miss"] stays 0. Hash#default on a str_int_hash is typed int? so {}.default is nil, not the raw sentinel.

sym_int_hash, int_int_hash, and int-array (#1180) keep their current behavior and follow in a later step. Self-host fixpoint holds; full test, bench, and optcarrot (checksum 59662) are green.

Refs #801

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

Details

Commit
e1ee0aa0879d
Parent
d2198f4e8704

compiler_helpers.rbmodifiedView file

@@ -284,22 +284,33 @@
284284 return ""
285285 end
286286 mname_eg = @nd_name[expr_id]
287- if mname_eg != "index" && mname_eg != "rindex" && mname_eg != "find_index"
288- return ""
289- end
290287 recv_eg = @nd_receiver[expr_id]
291288 if recv_eg < 0
292289 return ""
293290 end
294- rt_eg = infer_type(recv_eg)
295- if rt_eg == "string" || rt_eg == "mutable_str"
296- return "int"
297- end
291+ if mname_eg == "index" || mname_eg == "rindex" || mname_eg == "find_index"
292+ rt_eg = infer_type(recv_eg)
293+ if rt_eg == "string" || rt_eg == "mutable_str"
294+ return "int"
295+ end
298296 # Array#index family on int_array now returns int? (sentinel-
299297 # encoded). After `return if h.nil?` the live arm sees the value
300298 # as a plain int, same as the String#index narrow above.
301- if rt_eg == "int_array"
302- return "int"
299+ if rt_eg == "int_array"
300+ return "int"
301+ end
302+ return ""
303+ end
304+ # Maybe-missing typed-int-hash read `x = h[k]; return if x.nil?`:
305+ # the live arm proved the key present, so the read narrows from
306+ # int? back to a plain mrb_int. Mirrors the int_array arm above.
307+ # No-op until typed-int-hash `[]` returns int? (#801 Phase 4).
308+ if mname_eg == "[]"
309+ rt_he = base_type(infer_type(recv_eg))
310+ if rt_he == "str_int_hash" || rt_he == "sym_int_hash" || rt_he == "int_int_hash"
311+ return "int"
312+ end
313+ return ""
303314 end
304315 ""
305316 end
@@ -615,7 +626,10 @@
615626 # must treat the result as read-only.
616627 def parse_id_list(s)
617628 if @parse_id_cache.key?(s)
618- return @parse_id_pool[@parse_id_cache[s]]
629+ # Checked read: the guard proves the key present, so fetch keeps the
630+ # static type `int` (not the maybe-missing int? the public `[]` now
631+ # yields, #801 Phase 4) and the pool index stays non-null.
632+ return @parse_id_pool[@parse_id_cache.fetch(s)]
619633 end
620634 result = []
621635 if s != ""