Commit 7440fe07

7440fe0779d5Merged

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

Description

Report missing int-hash keys as nil at nil-check sites

Int-valued typed hashes (str_int / sym_int / int_int) return a stored mrb_int, so a missing key read as 0 instead of nil (#801). Making the read int? is the Ruby-correct fix but cascades pervasively through the self-hosting compiler's own int-hash reads (the infer_type cluster collapses to poly) -- confirmed intractable again this session.

Instead, keep the read typed int and add nil-awareness at the direct consumption sites, emitting a has_key guard there:

  • h[k].nil? -> !has_key(h, k) (new int_hash_read_has_key helper).
  • h[k] || x / h[k] == nil already did this for str_int/sym_int; extend the two tables (lhs_typed_hash_get_for_or, hash_has_key_fn) to int_int so all three int-valued variants behave consistently.

The compiler's own reads aren't at these nil-check sites (or are already key?-guarded), so this stays int everywhere it feeds type inference -- self-host clean (bootstrap + optcarrot 59662). The rendering sites (p / puts / inspect of a missing key) are a separate follow-up.

Refs #801

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
7440fe0779d5
Parent
fce6225a1cf0

spinel_codegen.rbmodifiedView file

@@ -13938,10 +13938,45 @@
1393813938 return "SymStrHash|string|sym"
1393913939 elsif rt_oh == "int_str_hash"
1394013940 return "IntStrHash|string|int"
13941+ elsif rt_oh == "int_int_hash"
13942+ return "IntIntHash|int|int"
1394113943 end
1394213944 ""
1394313945 end
1394413946
13947+ # For `h[k]` on an int-VALUED typed hash (str_int / sym_int / int_int),
13948+ # return the `sp_<Class>_has_key(recv, key)` C expression; its negation
13949+ # is the Ruby-correct nil-ness of the read. "" for any other shape --
13950+ # string-valued and poly variants already return NULL / sp_box_nil on
13951+ # miss and report nil correctly without this. Used by the consumption-
13952+ # site handlers (.nil? / inspect / p / puts) so a missing int-hash key
13953+ # reads as nil at those sites without making the read globally int?
13954+ # (which cascades through self-host). Issue #801.
13955+ def int_hash_read_has_key(read_nid)
13956+ info = lhs_typed_hash_get_for_or(read_nid)
13957+ if info == ""
13958+ return ""
13959+ end
13960+ parts = info.split("|", -1)
13961+ cls = parts[0]
13962+ val_t = parts[1]
13963+ key_kind = parts[2]
13964+ if val_t != "int"
13965+ return ""
13966+ end
13967+ rc = compile_expr(@nd_receiver[read_nid])
13968+ if key_kind == "str"
13969+ key = compile_str_arg0(read_nid)
13970+ elsif key_kind == "sym"
13971+ key = compile_arg0_as_sym(read_nid)
13972+ elsif key_kind == "int"
13973+ key = compile_arg0_as_int(read_nid)
13974+ else
13975+ key = compile_expr(get_args(@nd_arguments[read_nid])[0])
13976+ end
13977+ "sp_" + cls + "_has_key(" + rc + ", " + key + ")"
13978+ end
13979+
1394513980 def logical_truthiness_kind(nid, t)
1394613981 if nid >= 0
1394713982 nt = @nd_type[nid]
@@ -28487,6 +28522,13 @@
2848728522
2848828523 # nil?
2848928524 if mname == "nil?"
28525+ # `int_hash[k].nil?` -- the read is typed int, but a missing key is
28526+ # nil in Ruby. Report it via has_key without making the read int?.
28527+ # Issue #801.
28528+ hk_nil_801 = int_hash_read_has_key(@nd_receiver[nid])
28529+ if hk_nil_801 != ""
28530+ return "(!" + hk_nil_801 + ")"
28531+ end
2849028532 if recv_type == "nil"
2849128533 return "TRUE"
2849228534 end
@@ -30990,6 +31032,9 @@
3099031032 if t == "int_str_hash"
3099131033 return "sp_IntStrHash_has_key"
3099231034 end
31035+ if t == "int_int_hash"
31036+ return "sp_IntIntHash_has_key"
31037+ end
3099331038 if t == "poly_poly_hash"
3099431039 return "sp_PolyPolyHash_has_key"
3099531040 end