Commit 822964c2

822964c207e0Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · ~2 modified · -2 deleted

Description

fix(analyze,rbs): RBS Hash[untyped,untyped] seeds module params as poly

Two changes that together let module M; def f: (Hash[untyped, untyped]) -> ... reach the analyzer-tracked param type for a class that include Ms.

  1. spinel_rbs_extract: hash_tag_for_kv now returns "poly" (sp_RbVal) when either side is untyped, instead of failing to map (NULL). Previously the whole meth line was skipped, so the class-method param defaulted to mrb_int. Hash[String, untyped] keeps its existing fall-through to str_poly_hash; only the untyped-untyped pair (and any untyped-leftover that doesn't match a sharper variant) maps to bare poly. RBS_TYPES_BASES_ANY also gets a direct map_type entry so it can appear elsewhere.

  2. apply_rbs_seeds: when the seed file declares a class line for a name that resolves as a module rather than a class (spinel's extractor labels both as class because RBS does not surface the distinction in a uniform way), walk @cls_includes and seed each class that includes the module. Without this, module Dispatch; def stringify_keys: ... end only seeded class Dispatch (which has no methods in spinel's table) and the include-attached copy on every includer retained the collect_all int defaults.

A new @rbs_seeded_cls_meth set is also wired through seed_class_method and consulted by narrow_param_hash_types_from_ body_writes so RBS-declared signatures are not overridden by the Option-B heuristic that pins iterated poly_poly_hash params to str_str_hash when the body's writes give no value-type signal.

Deletes test/str_poly_hash_widen_keeps_slot_type.rb -- it shipped with cdb355e but the assertion it makes is unsound: cdb355e widens the local slot to sp_StrPolyHash * but the dup expression on the RHS still produces sp_StrStrHash * with a different struct layout. The test's puts merged.length happens to read at an offset both structs share, but reading a value back (merged["article_id"]) returns 0 instead of "1" because vals is a different element type. The dup-widening codegen remains the open half of #613.

Tests: 588 pass / make optcarrot OK (fps + checksum 59662).

Refs #613

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
822964c207e0
Parent
5cdf3e62ee5f

spinel_analyze.rbmodifiedView file

@@ -607,6 +607,30 @@
607607 # has built the class/method tables. Empty when no seed file was
608608 # passed -- inference behaves exactly as before.
609609 @rbs_seed_lines = "".split(",")
610+ # Set of "ci_midx" tokens for class instance methods whose ptypes
611+ # were RBS-seeded. Heuristic narrowing passes (notably the
612+ # `narrow_param_hash_types_from_body_writes` Option-B fallback
613+ # that pins an iterated `poly_poly_hash` param to `str_str_hash`
614+ # when no concrete writes signal a value type) consult this set
615+ # and skip the narrowing -- the explicit RBS declaration is
616+ # authoritative over heuristic inference. Issue #613.
617+ @rbs_seeded_cls_meth = "".split(";")
618+ end
619+
620+ def rbs_mark_cls_meth_seeded(ci, midx)
621+ @rbs_seeded_cls_meth.push(ci.to_s + "_" + midx.to_s)
622+ end
623+
624+ def rbs_is_cls_meth_seeded?(ci, midx)
625+ key = ci.to_s + "_" + midx.to_s
626+ j = 0
627+ while j < @rbs_seeded_cls_meth.length
628+ if @rbs_seeded_cls_meth[j] == key
629+ return 1
630+ end
631+ j = j + 1
632+ end
633+ 0
610634 end
611635
612636 # Backslash-n for C string literals - bootstrap-safe (avoids escape level issues)
@@ -20001,6 +20025,7 @@
2000120025 end
2000220026 current_ci = -1
2000320027 current_cls_name = ""
20028+ current_is_module = 0
2000420029 i = 0
2000520030 while i < @rbs_seed_lines.length
2000620031 raw = @rbs_seed_lines[i]
@@ -20010,6 +20035,15 @@
2001020035 elsif parts[0] == "class" && parts.length >= 2
2001120036 current_cls_name = parts[1]
2001220037 current_ci = find_class_idx(current_cls_name)
20038+ # The extractor labels both classes and modules as `class`
20039+ # because RBS doesn't surface the distinction in its AST in a
20040+ # way the C-level walker uses. When the name doesn't resolve
20041+ # to a class index, check the module table and flag the block
20042+ # so `meth` lines seed every class that includes this module.
20043+ current_is_module = 0
20044+ if current_ci < 0 && module_name_exists(current_cls_name) == 1
20045+ current_is_module = 1
20046+ end
2001320047 i = i + 1
2001420048 elsif parts[0] == "ivar" && parts.length >= 3 && current_ci >= 0
2001520049 seed_class_ivar(current_ci, parts[1], parts[2])
@@ -20023,6 +20057,36 @@
2002320057 end
2002420058 seed_class_method(current_ci, mname, ret, ptypes_token)
2002520059 i = i + 1
20060+ elsif parts[0] == "meth" && parts.length >= 3 && current_is_module == 1
20061+ # Module method seed: a class that includes this module ends
20062+ # up with the method in its @cls_meth_* after
20063+ # reconcile_class_includes. Seed each such class so the same
20064+ # RBS ptypes / return reach the per-class storage. Without
20065+ # this, `module Dispatch; def stringify_keys: (Hash[...]) -> ...`
20066+ # would only seed `class Dispatch` (which has no methods in
20067+ # spinel's table) and the include-attached copy stayed at
20068+ # collect_all's int defaults. Issue #613.
20069+ mname_m = parts[1]
20070+ ret_m = parts[2]
20071+ ptypes_token_m = "-"
20072+ if parts.length >= 4
20073+ ptypes_token_m = parts[3]
20074+ end
20075+ ck = 0
20076+ while ck < @cls_includes.length
20077+ incs = @cls_includes[ck].split(";")
20078+ ik = 0
20079+ while ik < incs.length
20080+ if incs[ik] == current_cls_name
20081+ seed_class_method(ck, mname_m, ret_m, ptypes_token_m)
20082+ ik = incs.length
20083+ else
20084+ ik = ik + 1
20085+ end
20086+ end
20087+ ck = ck + 1
20088+ end
20089+ i = i + 1
2002620090 elsif parts[0] == "cmeth" && parts.length >= 3 && current_cls_name != ""
2002720091 mname = parts[1]
2002820092 ret = parts[2]
@@ -20062,6 +20126,7 @@
2006220126 if ptypes_token != "-" && ptypes_token != ""
2006320127 ptypes = ptypes_token.split(",")
2006420128 cls_meth_ptypes_put(ci, midx, ptypes)
20129+ rbs_mark_cls_meth_seeded(ci, midx)
2006520130 end
2006620131 if ret != "-" && ret != ""
2006720132 rets = @cls_meth_returns[ci].split(";")
@@ -26543,7 +26608,8 @@
2654326608 m_changed = 0
2654426609 pk2 = 0
2654526610 while pk2 < cm_pnames.length
26546- if pk2 < cm_ptypes.length && is_hash_type(cm_ptypes[pk2]) == 1
26611+ if pk2 < cm_ptypes.length && is_hash_type(cm_ptypes[pk2]) == 1 &&
26612+ rbs_is_cls_meth_seeded?(ci, mj) == 0
2654726613 new_t = infer_param_hash_from_writes(bid_c, cm_pnames[pk2], cm_ptypes[pk2])
2654826614 if new_t != "" && new_t != cm_ptypes[pk2]
2654926615 cm_ptypes[pk2] = new_t