Commit 1ad97ddd

1ad97ddd695aMerged

Ori Pekelman <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

analyze: don't let a guessed (int-receiver) dispatch collapse a concrete setter param to poly

The forward-ref dispatch widening in scan_new_calls fires when a call's receiver is still statically int (an as-yet-unresolved ivar/local read) and mname belongs to user classes: it walks every user class defining mname at the call's arity and unifies that class's param types with the call-site args, so the int->class fallback dispatch's C signatures match.

For setters this over-widens. Every x= writer has arity 1, so the arity filter is no filter: a single recv.body = <poly> call site whose recv hasn't resolved yet (e.g. res.body = controller.body, res a dispatch param typed later in the fixpoint) unifies body='s param across every class defining body= -- including unrelated ones that only ever store a String. unify_call_types collapses String + the poly arg to poly, and the setter body @x = value then carries the poly into the ivar. The contamination is sticky (poly never narrows back).

Fix: in this branch only -- where the receiver class is a GUESS, not resolved -- lift an int-default param as before, but don't apply a unification that would collapse an already-concrete param to poly. The real receiver's obj-/poly- receiver branches (which DO know the class, or narrow via observed_class_ids_for_recv) still unify precisely once the type resolves.

Repro (roundhouse Rails blog, spinel-dev#8): ArticleRow#body/CommentRow#body (only ever assigned (row["body"]).to_s) inferred untyped; title/created_at (same shape, but no same-named poly setter elsewhere) stayed String. After the fix both recover to String, while genuinely-poly slots are preserved: Article#body (reads row["body"] raw, a str_poly_hash untyped read) and Tep::Response#body (a real poly slot) correctly stay untyped.

Self-host bootstrap converges; full suite 831 pass / 0 fail (+ 13 RBS extractor).

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

Details

Commit
1ad97ddd695a
Parent
1e7d63da5252

spinel_analyze.rbmodifiedView file

@@ -16184,7 +16184,22 @@
1618416184 while kk_fwd_c < arg_count_fwd
1618516185 at_fwd_c = infer_type(arg_ids_fwd[kk_fwd_c])
1618616186 if kk_fwd_c < ptypes_fwd_c.length
16187- ptypes_fwd_c[kk_fwd_c] = unify_call_types(ptypes_fwd_c[kk_fwd_c], at_fwd_c, arg_ids_fwd[kk_fwd_c])
16187+ prev_fwd_c = ptypes_fwd_c[kk_fwd_c]
16188+ unified_fwd_c = unify_call_types(prev_fwd_c, at_fwd_c, arg_ids_fwd[kk_fwd_c])
16189+ # The receiver type is unresolved ("int"), so this dispatch
16190+ # target is a GUESS -- every user class defining mname at this
16191+ # arity is walked, not just the real receiver's class. Lifting an
16192+ # int-default param to a concrete type is the intended
16193+ # fallback-signature fix; but letting a guessed target COLLAPSE an
16194+ # already-concrete param to "poly" contaminates unrelated
16195+ # same-named setters program-wide (a String-only `Row#body=`
16196+ # widened to poly because an unrelated `Response#body=` site passes
16197+ # a poly value through the same as-yet-int-typed local). The real
16198+ # target's obj-/poly-receiver branches above still unify precisely
16199+ # once the receiver type resolves. spinel-dev#8.
16200+ if prev_fwd_c == "int" || unified_fwd_c != "poly"
16201+ ptypes_fwd_c[kk_fwd_c] = unified_fwd_c
16202+ end
1618816203 end
1618916204 kk_fwd_c = kk_fwd_c + 1
1619016205 end