Commit febbfd30

febbfd30b82aMerged

Yusuke Endoh <[email protected]> · 6 months ago · 7 files · +6 added · ~1 modified

Description

fix(codegen): collapse stale unqualified obj-name observations against qualified peers

scan_ivars / collect_ivars run during Pass 1 (class enumeration) in source order; at that point sibling classes that come later in the file haven't been registered yet. A @b = B.new(@conf) write inside the lexically-first class then resolves "B" against an incomplete class table, lands at unqualified "obj_B", and pins the slot. When a subsequent pass — with all classes registered — records the qualified "obj__B" via scan_writer_calls (whose lexical resolution does succeed), the stale and qualified forms compare unequal.

The downstream effect is a 3-way widening cascade. update_ivar_type sees old="obj_B" / new="obj_B" and falls into the "genuinely incompatible" branch, widening the slot to poly. finalize_ivar_heterogeneity sees [obj_B, obj_B] (distinct.length == 2) and locks the widening in. unify_call_types sees the same string mismatch on Class.new's arg-vs-existing-ptype flow and collapses every Class.new(@b, ...) site's callee ptypes to poly. The poly cascade reaches the callee's own ivar via @b = b, then @b.<method> dispatches via the poly arm — listing every user class that defines the method, including the receiver's own class — and the runtime can self-recurse through it.

Add a stale-unqualified-obj normalization at all three sites:

  1. update_ivar_type — when old is a registered obj_X and new is a registered obj_Y whose tail matches _X (or vice versa), and only the qualified one resolves through find_class_idx, replace the slot with the qualified form rather than widening to poly.

  2. unify_call_types — same shape applied to call-site ptype unify so a forward-ref Class.new(@b, ...) whose first scan recorded obj_B doesn't collide with a later scan's obj_<scope>_B.

  3. drop_stale_unqualified_obj_obs (new) — invoked from finalize_ivar_heterogeneity to filter the observation list before the distinct-count widening decision, so a slot that was observed once as obj_B and once as obj_<scope>_B (referring to the same class) is treated as a single observation.

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

Details

Commit
febbfd30b82a
Parent
3ee4eed9c742

spinel_codegen.rbmodifiedView file

@@ -7866,6 +7866,34 @@
78667866 if names[k] == iname
78677867 if k < types.length
78687868 old = types[k]
7869+ # Stale unqualified obj-name normalization. collect_ivars
7870+ # registers ivar types during Pass 1 (class enumeration), in
7871+ # source order; at that point sibling classes that come later
7872+ # in the file haven't been registered yet. A
7873+ # `@cpu = CPU.new(@conf)` write inside the lexically-first
7874+ # class then resolves "CPU" against an incomplete class
7875+ # table, lands at unqualified "obj_CPU", and pins the slot.
7876+ # When a subsequent pass — with all classes registered —
7877+ # records the qualified "obj_<scope>_CPU" via scan_writer_calls
7878+ # (whose lexical resolution does succeed), the stale and
7879+ # qualified forms compare unequal and the heterogeneity
7880+ # branch below widens to "poly". Detect the relationship
7881+ # (old's bare name matches the trailing segment of new's
7882+ # name AND old's bare name doesn't refer to any registered
7883+ # class) and accept the qualified form as a refinement
7884+ # rather than a disagreement.
7885+ if is_obj_type(old) == 1 && is_obj_type(new_type) == 1 && old != new_type
7886+ old_bare_uit = old[4, old.length - 4]
7887+ new_bare_uit = new_type[4, new_type.length - 4]
7888+ if find_class_idx(old_bare_uit) < 0 && find_class_idx(new_bare_uit) >= 0
7889+ suffix_uit = "_" + old_bare_uit
7890+ if new_bare_uit.length > suffix_uit.length && new_bare_uit[(new_bare_uit.length - suffix_uit.length), suffix_uit.length] == suffix_uit
7891+ types[k] = new_type
7892+ @cls_ivar_types[ci] = types.join(";")
7893+ old = new_type
7894+ end
7895+ end
7896+ end
78697897 # Heterogeneous int/nil + obj → poly when the prior write
78707898 # was a *definite* int/nil literal. The previous "int wins"
78717899 # / "nil wins" overwrite silently cast the int payload to a
@@ -10008,6 +10036,32 @@
1000810036 if old_pt == at
1000910037 return old_pt
1001010038 end
10039+ # Stale unqualified obj-name normalization. Same shape as the
10040+ # update_ivar_type fix: a Pass 1 ivar / param scan that ran
10041+ # before all sibling classes were registered may have stamped
10042+ # an unqualified `obj_<bare>` on a slot, which a later pass
10043+ # then sees alongside the qualified `obj_<scope>_<bare>`.
10044+ # Without this normalization the two strings compare unequal
10045+ # and the trailing `incompatible → poly` tail collapses the
10046+ # ptype, then propagates poly across every Class.new call site
10047+ # that reads the slot. Detect the relationship and accept the
10048+ # qualified form.
10049+ if is_obj_type(old_pt) == 1 && is_obj_type(at) == 1
10050+ old_bare_uct = old_pt[4, old_pt.length - 4]
10051+ new_bare_uct = at[4, at.length - 4]
10052+ if find_class_idx(old_bare_uct) < 0 && find_class_idx(new_bare_uct) >= 0
10053+ suffix_uct = "_" + old_bare_uct
10054+ if new_bare_uct.length > suffix_uct.length && new_bare_uct[(new_bare_uct.length - suffix_uct.length), suffix_uct.length] == suffix_uct
10055+ return at
10056+ end
10057+ end
10058+ if find_class_idx(new_bare_uct) < 0 && find_class_idx(old_bare_uct) >= 0
10059+ suffix_uct = "_" + new_bare_uct
10060+ if old_bare_uct.length > suffix_uct.length && old_bare_uct[(old_bare_uct.length - suffix_uct.length), suffix_uct.length] == suffix_uct
10061+ return old_pt
10062+ end
10063+ end
10064+ end
1001110065 arg_is_literal = 0
1001210066 if arg_id >= 0 && is_literal_value_expr(arg_id) == 1
1001310067 arg_is_literal = 1
@@ -12417,6 +12471,65 @@
1241712471 finalize_ivar_heterogeneity
1241812472 end
1241912473
12474+ # Drop "obj_<bare>" observations from `obs` when (a) <bare> doesn't
12475+ # resolve to any registered class, AND (b) some other observation in
12476+ # the list is "obj_<scope>_<bare>" for the same trailing <bare> and
12477+ # <scope>_<bare> IS registered. Caller passes the raw split list of
12478+ # observed types; we return a filtered list with stale unqualified
12479+ # obj-names removed (their qualified peer carries the same slot type
12480+ # information). Used by finalize_ivar_heterogeneity so the
12481+ # distinct-count poly-widen decision doesn't flip a single-class
12482+ # ivar to poly just because Pass 1 recorded the bare name and a
12483+ # later pass recorded the qualified one.
12484+ def drop_stale_unqualified_obj_obs(obs)
12485+ out = "".split(",")
12486+ # Type-prime locals as strings so spinel-self-compile picks the
12487+ # right C type (sp_str_sub_range / sp_str_concat returns
12488+ # `const char *`; without the empty-string seed scan_locals
12489+ # falls back to `mrb_int`, which then fails the `-Wint-conversion`
12490+ # bootstrap when this method is invoked from finalize_ivar_heterogeneity).
12491+ o = ""
12492+ p = ""
12493+ ob = ""
12494+ pb = ""
12495+ sfx = ""
12496+ tail = ""
12497+ oi = 0
12498+ while oi < obs.length
12499+ o = obs[oi]
12500+ keep = 1
12501+ if is_obj_type(o) == 1
12502+ ob = o[4, o.length - 4]
12503+ if find_class_idx(ob) < 0
12504+ oj = 0
12505+ while oj < obs.length
12506+ if oj != oi
12507+ p = obs[oj]
12508+ if is_obj_type(p) == 1
12509+ pb = p[4, p.length - 4]
12510+ if find_class_idx(pb) >= 0
12511+ sfx = "_" + ob
12512+ if pb.length > sfx.length
12513+ tail = pb[(pb.length - sfx.length), sfx.length]
12514+ if tail == sfx
12515+ keep = 0
12516+ end
12517+ end
12518+ end
12519+ end
12520+ end
12521+ oj = oj + 1
12522+ end
12523+ end
12524+ end
12525+ if keep == 1
12526+ out.push(o)
12527+ end
12528+ oi = oi + 1
12529+ end
12530+ out
12531+ end
12532+
1242012533 def finalize_ivar_heterogeneity
1242112534 ci = 0
1242212535 while ci < @cls_names.length
@@ -12428,6 +12541,17 @@
1242812541 while ivk < names.length
1242912542 if ivk < types.length && ivk < obs.length && types[ivk] != "poly"
1243012543 distinct = obs[ivk].split(",")
12544+ # Collapse stale unqualified obj-name observations against
12545+ # their qualified form before the distinct-count widening
12546+ # decision. A first-pass `@x = Foo.new(...)` recorded inside
12547+ # a class whose sibling `Foo` hadn't been registered yet
12548+ # stamped "obj_Foo" into the observation list; a later pass
12549+ # (with all classes registered) records the proper
12550+ # "obj_<scope>_Foo". They refer to the same class, but the
12551+ # raw distinct-count below would treat them as 2 types and
12552+ # widen the slot to poly. Mirror the update_ivar_type /
12553+ # unify_call_types normalization.
12554+ distinct = drop_stale_unqualified_obj_obs(distinct)
1243112555 if distinct.length >= 2
1243212556 # When every observed type is itself an array AND the
1243312557 # current type is already `poly_array` (set by an earlier
@@ -15327,11 +15451,6 @@
1532715451 infer_function_body_call_types
1532815452 infer_class_body_call_types
1532915453 infer_ivar_types_from_writers
15330- # After scan_locals has populated @meth_param_empty via the
15331- # per-call-site forward propagation, promote int_array
15332- # params to concrete typed-arrays where bodies push known
15333- # types. The next iteration's scan_locals back-propagates
15334- # those promoted types to caller-side locals.
1533515454 infer_param_array_type_from_body
1533615455 narrow_param_types_from_body_method_calls
1533715456 detect_poly_params