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
fetchon 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
{}.defaultis 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
return "" end mname_eg = @nd_name[expr_id] if mname_eg != "index" && mname_eg != "rindex" && mname_eg != "find_index" return "" end recv_eg = @nd_receiver[expr_id] if recv_eg < 0 return "" end rt_eg = infer_type(recv_eg) if rt_eg == "string" || rt_eg == "mutable_str" return "int" end if mname_eg == "index" || mname_eg == "rindex" || mname_eg == "find_index" rt_eg = infer_type(recv_eg) if rt_eg == "string" || rt_eg == "mutable_str" return "int" end # Array#index family on int_array now returns int? (sentinel- # encoded). After `return if h.nil?` the live arm sees the value # as a plain int, same as the String#index narrow above. if rt_eg == "int_array" return "int" if rt_eg == "int_array" return "int" end return "" end # Maybe-missing typed-int-hash read `x = h[k]; return if x.nil?`: # the live arm proved the key present, so the read narrows from # int? back to a plain mrb_int. Mirrors the int_array arm above. # No-op until typed-int-hash `[]` returns int? (#801 Phase 4). if mname_eg == "[]" rt_he = base_type(infer_type(recv_eg)) if rt_he == "str_int_hash" || rt_he == "sym_int_hash" || rt_he == "int_int_hash" return "int" end return "" end "" end # must treat the result as read-only. def parse_id_list(s) if @parse_id_cache.key?(s) return @parse_id_pool[@parse_id_cache[s]] # Checked read: the guard proves the key present, so fetch keeps the # static type `int` (not the maybe-missing int? the public `[]` now # yields, #801 Phase 4) and the pool index stays non-null. return @parse_id_pool[@parse_id_cache.fetch(s)] end result = [] if s != ""lib/sp_runtime.hmodifiedView file
Loading diff…
spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/str_int_hash_missing_key_nil.rbaddedView file
Loading diff…
test/str_int_hash_missing_key_nil.rb.expectedaddedView file
Loading diff…