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.
-
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
methline 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. -
apply_rbs_seeds: when the seed file declares a
classline for a name that resolves as a module rather than a class (spinel's extractor labels both asclassbecause 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: ... endonly seededclass Dispatch(which has no methods in spinel's table) and the include-attached copy on everyincluder 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
# has built the class/method tables. Empty when no seed file was # passed -- inference behaves exactly as before. @rbs_seed_lines = "".split(",") # Set of "ci_midx" tokens for class instance methods whose ptypes # were RBS-seeded. Heuristic narrowing passes (notably the # `narrow_param_hash_types_from_body_writes` Option-B fallback # that pins an iterated `poly_poly_hash` param to `str_str_hash` # when no concrete writes signal a value type) consult this set # and skip the narrowing -- the explicit RBS declaration is # authoritative over heuristic inference. Issue #613. @rbs_seeded_cls_meth = "".split(";") end def rbs_mark_cls_meth_seeded(ci, midx) @rbs_seeded_cls_meth.push(ci.to_s + "_" + midx.to_s) end def rbs_is_cls_meth_seeded?(ci, midx) key = ci.to_s + "_" + midx.to_s j = 0 while j < @rbs_seeded_cls_meth.length if @rbs_seeded_cls_meth[j] == key return 1 end j = j + 1 end 0 end # Backslash-n for C string literals - bootstrap-safe (avoids escape level issues) end current_ci = -1 current_cls_name = "" current_is_module = 0 i = 0 while i < @rbs_seed_lines.length raw = @rbs_seed_lines[i] elsif parts[0] == "class" && parts.length >= 2 current_cls_name = parts[1] current_ci = find_class_idx(current_cls_name) # The extractor labels both classes and modules as `class` # because RBS doesn't surface the distinction in its AST in a # way the C-level walker uses. When the name doesn't resolve # to a class index, check the module table and flag the block # so `meth` lines seed every class that includes this module. current_is_module = 0 if current_ci < 0 && module_name_exists(current_cls_name) == 1 current_is_module = 1 end i = i + 1 elsif parts[0] == "ivar" && parts.length >= 3 && current_ci >= 0 seed_class_ivar(current_ci, parts[1], parts[2]) end seed_class_method(current_ci, mname, ret, ptypes_token) i = i + 1 elsif parts[0] == "meth" && parts.length >= 3 && current_is_module == 1 # Module method seed: a class that includes this module ends # up with the method in its @cls_meth_* after # reconcile_class_includes. Seed each such class so the same # RBS ptypes / return reach the per-class storage. Without # this, `module Dispatch; def stringify_keys: (Hash[...]) -> ...` # would only seed `class Dispatch` (which has no methods in # spinel's table) and the include-attached copy stayed at # collect_all's int defaults. Issue #613. mname_m = parts[1] ret_m = parts[2] ptypes_token_m = "-" if parts.length >= 4 ptypes_token_m = parts[3] end ck = 0 while ck < @cls_includes.length incs = @cls_includes[ck].split(";") ik = 0 while ik < incs.length if incs[ik] == current_cls_name seed_class_method(ck, mname_m, ret_m, ptypes_token_m) ik = incs.length else ik = ik + 1 end end ck = ck + 1 end i = i + 1 elsif parts[0] == "cmeth" && parts.length >= 3 && current_cls_name != "" mname = parts[1] ret = parts[2] if ptypes_token != "-" && ptypes_token != "" ptypes = ptypes_token.split(",") cls_meth_ptypes_put(ci, midx, ptypes) rbs_mark_cls_meth_seeded(ci, midx) end if ret != "-" && ret != "" rets = @cls_meth_returns[ci].split(";") m_changed = 0 pk2 = 0 while pk2 < cm_pnames.length if pk2 < cm_ptypes.length && is_hash_type(cm_ptypes[pk2]) == 1 if pk2 < cm_ptypes.length && is_hash_type(cm_ptypes[pk2]) == 1 && rbs_is_cls_meth_seeded?(ci, mj) == 0 new_t = infer_param_hash_from_writes(bid_c, cm_pnames[pk2], cm_ptypes[pk2]) if new_t != "" && new_t != cm_ptypes[pk2] cm_ptypes[pk2] = new_tspinel_rbs_extract.cmodifiedView file
Loading diff…
test/str_poly_hash_widen_keeps_slot_type.rbdeleted
Loading diff…
test/str_poly_hash_widen_keeps_slot_type.rb.expecteddeleted
Loading diff…