Commit 8a566c53
8a566c535e15Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 6 files · +2 added · ~4 modified
Description
fix(codegen,analyze): fetch(k, nil) on int-leaf hash returns int? with SP_INT_NIL
hash.fetch(:k, nil).nil? reported "found" instead of "missing"
when the key was absent: the missing-key arm of the ternary fell
back to the value-type zero (mrb_int 0), which is indistinguishable
from a legit hash value 0 -- and the downstream .nil? then
constant-folded to FALSE since mrb_int can't be nil.
Surface "int?" (the existing nullable-int slot with SP_INT_NIL
sentinel) for <int_leaf_hash>.fetch(k, nil):
- analyze: infer_call_type returns "int?" for sym_int_hash / str_int_hash + NilNode default. Already-handled string and hash defaults stay on their current widening paths.
- codegen: sym_int_hash / str_int_hash fetch arms emit
has_key ? get : SP_INT_NILwhen the default is NilNode. - The downstream
.nil?dispatch routes through the existing scalar-nullable arm (sp_int_is_nil) since recv_type is now int?.
Updates fetch_nil_default_int_lv test: previously expected "found" (documenting the wrong-but-don't-crash behavior from #674); now expects "missing" matching MRI. Adds fetch_nil_default_distinguishes_zero covering both int-leaf hash variants and the zero-value-key-present case.
Tests: default 456/0/0, promote 456/0/0, optcarrot checksum 59662.
Closes #682
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 8a566c535e15
- Parent
- 4da7f778cdd2
spinel_analyze.rbmodifiedView file
@needs_rb_value = 1 return "poly" end # nil default (`fetch "k", nil`). Lower to int? so a downstream # `.nil?` distinguishes missing-key from a legit 0 value (#682). # The codegen-side fetch arm pairs this by emitting SP_INT_NIL # in the default slot. if @nd_type[fargs_f[1]] == "NilNode" return "int?" end end end endspinel_codegen.rbmodifiedView file
Loading diff…
test/fetch_nil_default_distinguishes_zero.rbaddedView file
Loading diff…
test/fetch_nil_default_distinguishes_zero.rb.expectedaddedView file
Loading diff…
test/fetch_nil_default_int_lv.rbmodifiedView file
Loading diff…
test/fetch_nil_default_int_lv.rb.expectedmodifiedView file
Loading diff…