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_NIL when 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

@@ -4278,6 +4278,13 @@
42784278 @needs_rb_value = 1
42794279 return "poly"
42804280 end
4281+ # nil default (`fetch "k", nil`). Lower to int? so a downstream
4282+ # `.nil?` distinguishes missing-key from a legit 0 value (#682).
4283+ # The codegen-side fetch arm pairs this by emitting SP_INT_NIL
4284+ # in the default slot.
4285+ if @nd_type[fargs_f[1]] == "NilNode"
4286+ return "int?"
4287+ end
42814288 end
42824289 end
42834290 end