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] == nilalready 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
return "SymStrHash|string|sym" elsif rt_oh == "int_str_hash" return "IntStrHash|string|int" elsif rt_oh == "int_int_hash" return "IntIntHash|int|int" end "" end # For `h[k]` on an int-VALUED typed hash (str_int / sym_int / int_int), # return the `sp_<Class>_has_key(recv, key)` C expression; its negation # is the Ruby-correct nil-ness of the read. "" for any other shape -- # string-valued and poly variants already return NULL / sp_box_nil on # miss and report nil correctly without this. Used by the consumption- # site handlers (.nil? / inspect / p / puts) so a missing int-hash key # reads as nil at those sites without making the read globally int? # (which cascades through self-host). Issue #801. def int_hash_read_has_key(read_nid) info = lhs_typed_hash_get_for_or(read_nid) if info == "" return "" end parts = info.split("|", -1) cls = parts[0] val_t = parts[1] key_kind = parts[2] if val_t != "int" return "" end rc = compile_expr(@nd_receiver[read_nid]) if key_kind == "str" key = compile_str_arg0(read_nid) elsif key_kind == "sym" key = compile_arg0_as_sym(read_nid) elsif key_kind == "int" key = compile_arg0_as_int(read_nid) else key = compile_expr(get_args(@nd_arguments[read_nid])[0]) end "sp_" + cls + "_has_key(" + rc + ", " + key + ")" end def logical_truthiness_kind(nid, t) if nid >= 0 nt = @nd_type[nid] # nil? if mname == "nil?" # `int_hash[k].nil?` -- the read is typed int, but a missing key is # nil in Ruby. Report it via has_key without making the read int?. # Issue #801. hk_nil_801 = int_hash_read_has_key(@nd_receiver[nid]) if hk_nil_801 != "" return "(!" + hk_nil_801 + ")" end if recv_type == "nil" return "TRUE" end if t == "int_str_hash" return "sp_IntStrHash_has_key" end if t == "int_int_hash" return "sp_IntIntHash_has_key" end if t == "poly_poly_hash" return "sp_PolyPolyHash_has_key" endtest/int_hash_missing_nil_check.rbaddedView file
Loading diff…
test/int_hash_missing_nil_check.rb.expectedaddedView file
Loading diff…