Commit 42c29318

42c29318732aMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Re-pin RBS module-method ptypes post-fixpoint, fixing env-dependent hash over-specialization

A method declared in RBS under a module (e.g. the Hash[untyped, untyped] params of a RequestDispatch helper mixed into a test class) could be emitted with a concrete hash C type (sp_SymPolyHash *) on some build hosts while correctly boxed to sp_RbVal on others -- identical source, arch, gcc and LTO, differing only by host. The {}-default str_int_hash callers then passed sp_StrIntHash * into the sp_SymPolyHash * param, the reported incompatible-pointer mismatch.

Root cause: the RBS seed for a module method reaches the including class via apply_rbs_seeds' module-include path, which depends on module_name_exists / find_class_idx classifying the module header. When that classification is dropped, the @cls_meth_rbs_ptypes entry is never created, so arbitrate_rbs_vs_inferred_methods has nothing to restore and the call-site-inferred concrete hash type sticks. Confirmed locally by forcing the module header to misclassify (is_module=0): with --rbs the param deterministically flips to sp_SymPolyHash *. valgrind on the analyze pass is clean, consistent with a logic/ordering drop rather than an uninitialised read; the exact host-specific trigger on the GH runner is not reproduced locally.

Fix: record every seed meth line's declared ptypes/returns in a module-keyed shadow table unconditionally (gated only on the header name, so it survives module/class misclassification), and in arbitrate_rbs_vs_inferred_methods re-pin them onto every including class post-fixpoint -- when reconcile_class_includes has attached the include-copy and cls_find_method_direct is reliable. This makes the RBS pin independent of the fragile seed-time classification. No-op without --rbs (shadow stays empty); the apply_rbs_seeds branch-merge is behavior-preserving for working seeds (get/post/patch/delete still emit sp_RbVal).

Verified on the roundhouse repro: with the module header forced to misclassify, dispatch_request's params flips sp_SymPolyHash * -> sp_RbVal once the backstop is active, and a plain-gcc compile no longer reports the SymPolyHash/StrIntHash mismatch. gate green (843 tests, optcarrot 59662, self-host fixpoint OK). Note: the gate does not exercise the --rbs inference path (rbs-test covers only the extractor), so this has no automated guard.

The deterministic over-specialization spinel still emits for this shape without --rbs is a separate inference-layer issue (cross-key-family hash param unification), left open as a sibling.

Refs #1303

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

Details

Commit
42c29318732a
Parent
058b8f2a4450

spinel_analyze.rbmodifiedView file

@@ -331,6 +331,18 @@
331331 @cls_cmeth_rbs_returns = {}
332332 @meth_rbs_ptypes = {}
333333 @meth_rbs_returns = {}
334+ # Module-method RBS shadow, keyed "<ModuleName>\t<method>". Filled
335+ # for every seed `meth` line regardless of whether the header
336+ # resolved as a class/module at seed time, so
337+ # arbitrate_rbs_vs_inferred_methods can re-pin the declared ptypes
338+ # onto every including class post-fixpoint -- a backstop for the
339+ # non-deterministic seed-time drop where module_name_exists /
340+ # find_class_idx misclassifies the module header (the env-dependent
341+ # `Hash[untyped,untyped]` param over-specialization). A header that
342+ # is a real class (not included anywhere) simply never matches an
343+ # include list, so the extra entries are harmless.
344+ @module_meth_rbs_ptypes = {}
345+ @module_meth_rbs_returns = {}
334346 # Per-(class, cmeth) local scope tables. @nd_scope_names is
335347 # keyed by AST body id only; an inherited class method's body
336348 # is shared across subclasses, so the per-bid table gets
@@ -25546,16 +25558,30 @@
2554625558 elsif parts[0] == "ivar" && parts.length >= 3 && current_ci >= 0
2554725559 seed_class_ivar(current_ci, parts[1], parts[2])
2554825560 i = i + 1
25549- elsif parts[0] == "meth" && parts.length >= 3 && current_ci >= 0
25561+ elsif parts[0] == "meth" && parts.length >= 3 && current_cls_name != ""
2555025562 mname = parts[1]
2555125563 ret = parts[2]
2555225564 ptypes_token = "-"
2555325565 if parts.length >= 4
2555425566 ptypes_token = parts[3]
2555525567 end
25556- seed_class_method(current_ci, mname, ret, ptypes_token)
25557- i = i + 1
25558- elsif parts[0] == "meth" && parts.length >= 3 && current_is_module == 1
25568+ # Record the declared signature under the header name
25569+ # unconditionally, before the class-vs-module dispatch below.
25570+ # arbitrate_rbs_vs_inferred_methods re-pins from this shadow
25571+ # post-fixpoint, so the RBS pin survives even when the seed-time
25572+ # classification here is wrong on this host (module_name_exists /
25573+ # find_class_idx non-determinism). Class headers never match an
25574+ # include list, so their entries stay inert.
25575+ mkey = current_cls_name + "\t" + mname
25576+ if ptypes_token != "-" && ptypes_token != ""
25577+ @module_meth_rbs_ptypes[mkey] = ptypes_token
25578+ end
25579+ if ret != "-" && ret != ""
25580+ @module_meth_rbs_returns[mkey] = ret
25581+ end
25582+ if current_ci >= 0
25583+ seed_class_method(current_ci, mname, ret, ptypes_token)
25584+ elsif current_is_module == 1
2555925585 # Module method seed: a class that includes this module ends
2556025586 # up with the method in its @cls_meth_* after
2556125587 # reconcile_class_includes. Seed each such class so the same
@@ -25564,25 +25590,20 @@
2556425590 # would only seed `class Dispatch` (which has no methods in
2556525591 # spinel's table) and the include-attached copy stayed at
2556625592 # collect_all's int defaults. Issue #613.
25567- mname_m = parts[1]
25568- ret_m = parts[2]
25569- ptypes_token_m = "-"
25570- if parts.length >= 4
25571- ptypes_token_m = parts[3]
25572- end
25573- ck = 0
25574- while ck < @cls_includes.length
25575- incs = @cls_includes[ck].split(";", -1)
25576- ik = 0
25577- while ik < incs.length
25578- if incs[ik] == current_cls_name
25579- seed_class_method(ck, mname_m, ret_m, ptypes_token_m)
25580- ik = incs.length
25581- else
25582- ik = ik + 1
25593+ ck = 0
25594+ while ck < @cls_includes.length
25595+ incs = @cls_includes[ck].split(";", -1)
25596+ ik = 0
25597+ while ik < incs.length
25598+ if incs[ik] == current_cls_name
25599+ seed_class_method(ck, mname, ret, ptypes_token)
25600+ ik = incs.length
25601+ else
25602+ ik = ik + 1
25603+ end
2558325604 end
25605+ ck = ck + 1
2558425606 end
25585- ck = ck + 1
2558625607 end
2558725608 i = i + 1
2558825609 elsif parts[0] == "cmeth" && parts.length >= 3 && current_cls_name != ""
@@ -26286,6 +26307,62 @@
2628626307 @cls_meth_return_cache = {}
2628726308 end
2628826309 }
26310+ # Module-method backstop. Re-pin each RBS-declared module method
26311+ # onto every class that includes the module, looking the method up
26312+ # now (post-fixpoint) when reconcile_class_includes has attached the
26313+ # include-copy and cls_find_method_direct's cache is stable. This
26314+ # makes the RBS pin independent of the seed-time include-seed, which
26315+ # is dropped non-deterministically on some hosts when
26316+ # module_name_exists / find_class_idx misclassify the module header
26317+ # -- the cause of the env-dependent `Hash[untyped,untyped]` param
26318+ # over-specialization (sp_SymPolyHash* vs the str_int_hash callers).
26319+ @module_meth_rbs_ptypes.each_pair { |mk, rbs_pt|
26320+ mparts = mk.split("\t", -1)
26321+ if mparts.length >= 2
26322+ modname = mparts[0]
26323+ methname = mparts[1]
26324+ ck = 0
26325+ while ck < @cls_includes.length
26326+ incs = @cls_includes[ck].split(";", -1)
26327+ if not_in(modname, incs) == 0 && ck < @cls_meth_ptypes.length
26328+ midx = cls_find_method_direct(ck, methname)
26329+ if midx >= 0
26330+ all_pt = @cls_meth_ptypes[ck].split("|", -1)
26331+ if midx < all_pt.length && all_pt[midx] != rbs_pt
26332+ all_pt[midx] = rbs_pt
26333+ @cls_meth_ptypes[ck] = all_pt.join("|")
26334+ @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1
26335+ end
26336+ @cls_meth_rbs_ptypes[ck.to_s + "|" + midx.to_s] = rbs_pt
26337+ end
26338+ end
26339+ ck = ck + 1
26340+ end
26341+ end
26342+ }
26343+ @module_meth_rbs_returns.each_pair { |mk, rbs_ret|
26344+ mparts = mk.split("\t", -1)
26345+ if mparts.length >= 2
26346+ modname = mparts[0]
26347+ methname = mparts[1]
26348+ ck = 0
26349+ while ck < @cls_includes.length
26350+ incs = @cls_includes[ck].split(";", -1)
26351+ if not_in(modname, incs) == 0 && ck < @cls_meth_returns.length
26352+ midx = cls_find_method_direct(ck, methname)
26353+ if midx >= 0
26354+ rets = @cls_meth_returns[ck].split(";", -1)
26355+ if midx < rets.length && rets[midx] != rbs_ret
26356+ rets[midx] = rbs_ret
26357+ @cls_meth_returns[ck] = rets.join(";")
26358+ @cls_meth_return_cache = {}
26359+ end
26360+ end
26361+ end
26362+ ck = ck + 1
26363+ end
26364+ end
26365+ }
2628926366 # Class methods (cmeth).
2629026367 @cls_cmeth_rbs_ptypes.each_pair { |k, rbs_pt|
2629126368 parts = k.split("|", -1)