Commit 139b5d37
139b5d374568Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 13 files · +12 added · ~1 modified
Description
Merge pull request #385 from mame/pr/forward-call-class-method-inherited-init
fix(codegen): forward-ref Class.new ptype widening — qualified-name normalization + super-call propagation
Details
- Commit
- 139b5d374568
- Parent
- 45b412267afb
- Merge parent
- 60016a69a438
spinel_codegen.rbmodifiedView file
@@ -7875,6 +7875,34 @@
78757875
if names[k] == iname78767876
if k < types.length78777877
old = types[k]7878+
# Stale unqualified obj-name normalization. collect_ivars7879+
# registers ivar types during Pass 1 (class enumeration), in7880+
# source order; at that point sibling classes that come later7881+
# in the file haven't been registered yet. A7882+
# `@cpu = CPU.new(@conf)` write inside the lexically-first7883+
# class then resolves "CPU" against an incomplete class7884+
# table, lands at unqualified "obj_CPU", and pins the slot.7885+
# When a subsequent pass — with all classes registered —7886+
# records the qualified "obj_<scope>_CPU" via scan_writer_calls7887+
# (whose lexical resolution does succeed), the stale and7888+
# qualified forms compare unequal and the heterogeneity7889+
# branch below widens to "poly". Detect the relationship7890+
# (old's bare name matches the trailing segment of new's7891+
# name AND old's bare name doesn't refer to any registered7892+
# class) and accept the qualified form as a refinement7893+
# rather than a disagreement.7894+
if is_obj_type(old) == 1 && is_obj_type(new_type) == 1 && old != new_type7895+
old_bare_uit = old[4, old.length - 4]7896+
new_bare_uit = new_type[4, new_type.length - 4]7897+
if find_class_idx(old_bare_uit) < 0 && find_class_idx(new_bare_uit) >= 07898+
suffix_uit = "_" + old_bare_uit7899+
if new_bare_uit.length > suffix_uit.length && new_bare_uit[(new_bare_uit.length - suffix_uit.length), suffix_uit.length] == suffix_uit7900+
types[k] = new_type7901+
@cls_ivar_types[ci] = types.join(";")7902+
old = new_type7903+
end7904+
end7905+
end78787906
# Heterogeneous int/nil + obj → poly when the prior write78797907
# was a *definite* int/nil literal. The previous "int wins"78807908
# / "nil wins" overwrite silently cast the int payload to a@@ -10017,6 +10045,32 @@
1001710045
if old_pt == at1001810046
return old_pt1001910047
end10048+
# Stale unqualified obj-name normalization. Same shape as the10049+
# update_ivar_type fix: a Pass 1 ivar / param scan that ran10050+
# before all sibling classes were registered may have stamped10051+
# an unqualified `obj_<bare>` on a slot, which a later pass10052+
# then sees alongside the qualified `obj_<scope>_<bare>`.10053+
# Without this normalization the two strings compare unequal10054+
# and the trailing `incompatible → poly` tail collapses the10055+
# ptype, then propagates poly across every Class.new call site10056+
# that reads the slot. Detect the relationship and accept the10057+
# qualified form.10058+
if is_obj_type(old_pt) == 1 && is_obj_type(at) == 110059+
old_bare_uct = old_pt[4, old_pt.length - 4]10060+
new_bare_uct = at[4, at.length - 4]10061+
if find_class_idx(old_bare_uct) < 0 && find_class_idx(new_bare_uct) >= 010062+
suffix_uct = "_" + old_bare_uct10063+
if new_bare_uct.length > suffix_uct.length && new_bare_uct[(new_bare_uct.length - suffix_uct.length), suffix_uct.length] == suffix_uct10064+
return at10065+
end10066+
end10067+
if find_class_idx(new_bare_uct) < 0 && find_class_idx(old_bare_uct) >= 010068+
suffix_uct = "_" + new_bare_uct10069+
if old_bare_uct.length > suffix_uct.length && old_bare_uct[(old_bare_uct.length - suffix_uct.length), suffix_uct.length] == suffix_uct10070+
return old_pt10071+
end10072+
end10073+
end1002010074
arg_is_literal = 01002110075
if arg_id >= 0 && is_literal_value_expr(arg_id) == 11002210076
arg_is_literal = 1@@ -10926,6 +10980,150 @@
1092610980
# resolve via the cls method's pnames/ptypes; other args fall1092710981
# through to infer_type with @current_class_idx pinned so1092810982
# @ivar refs in the args resolve against the right class.10983+
# Propagate ptypes from child#initialize to parent#initialize via10984+
# super calls. Without this, a `Sub.new(owner_obj)` site widens10985+
# `Sub#initialize`'s `_owner` param to `obj_<C>` correctly (via10986+
# the existing constructor branch in scan_new_calls), but the10987+
# body's bare `super` lowers to a C call against the parent's10988+
# `Base#initialize`, whose param ptype stays at the default10989+
# `mrb_int` because no call-site path widens the *parent's*10990+
# initialize through super. The C output then casts the typed10991+
# pointer to `mrb_int` at the super-call site, the parent's body's10992+
# `@owner = owner` records the int into the slot, and every10993+
# subsequent `@owner.<method>` dispatch on a child instance lands10994+
# on whatever class shares the int recv's cls_id — commonly the10995+
# wrong one.10996+
#10997+
# ForwardingSuperNode (bare `super`): forwards every param of the10998+
# current method by name. Unify the parent ptypes element-wise10999+
# against the child ptypes (capped at min length, so a wider11000+
# parent signature stays unwidened past the forwarded prefix).11001+
#11002+
# SuperNode (`super(arg1, arg2, …)`): explicit arg list. Each arg11003+
# might be a LocalVariableReadNode that points back at one of the11004+
# current method's params; if so, use the param's ptype to unify11005+
# at the super arg's position. Other shapes (literals, calls)11006+
# fall through to infer_type.11007+
def propagate_super_init_to_parent11008+
ci = 011009+
while ci < @cls_names.length11010+
if @cls_parents[ci] != ""11011+
parent_ci = find_class_idx(@cls_parents[ci])11012+
# The parent chain may insert classes without their own11013+
# #initialize (e.g. an empty marker subclass between the11014+
# super caller and its semantic parent). Walk up until we11015+
# find one that does.11016+
walk_ci = parent_ci11017+
parent_init_ci = -111018+
while walk_ci >= 011019+
if cls_find_method_direct(walk_ci, "initialize") >= 011020+
parent_init_ci = walk_ci11021+
break11022+
end11023+
parent_str = @cls_parents[walk_ci]11024+
if parent_str == ""11025+
break11026+
end11027+
walk_ci = find_class_idx(parent_str)11028+
end11029+
if parent_init_ci >= 011030+
propagate_super_init_one(ci, parent_init_ci)11031+
end11032+
end11033+
ci = ci + 111034+
end11035+
end11036+
11037+
def propagate_super_init_one(child_ci, parent_init_ci)11038+
child_init_idx = cls_find_method_direct(child_ci, "initialize")11039+
if child_init_idx < 011040+
return11041+
end11042+
bodies = @cls_meth_bodies[child_ci].split(";")11043+
if child_init_idx >= bodies.length11044+
return11045+
end11046+
bid = bodies[child_init_idx].to_i11047+
if bid < 011048+
return11049+
end11050+
child_pnames = cls_meth_pnames_get(child_ci, child_init_idx)11051+
child_ptypes = cls_meth_ptypes_get(child_ci, child_init_idx)11052+
parent_init_idx = cls_find_method_direct(parent_init_ci, "initialize")11053+
if parent_init_idx < 011054+
return11055+
end11056+
parent_ptypes = cls_meth_ptypes_get(parent_init_ci, parent_init_idx)11057+
if parent_ptypes.length == 011058+
return11059+
end11060+
propagate_super_walk(bid, child_pnames, child_ptypes, parent_ptypes)11061+
cls_meth_ptypes_put(parent_init_ci, parent_init_idx, parent_ptypes)11062+
end11063+
11064+
def propagate_super_walk(nid, child_pnames, child_ptypes, parent_ptypes)11065+
if nid < 011066+
return11067+
end11068+
nt = @nd_type[nid]11069+
if nt == "DefNode"11070+
return11071+
end11072+
if nt == "ForwardingSuperNode"11073+
# Bare `super`: forward every child param into the parent's slot11074+
# at the same position, capped at min length.11075+
kk = 011076+
lim = child_ptypes.length11077+
if parent_ptypes.length < lim11078+
lim = parent_ptypes.length11079+
end11080+
while kk < lim11081+
ct = child_ptypes[kk]11082+
if ct != "" && ct != "int"11083+
parent_ptypes[kk] = unify_call_types(parent_ptypes[kk], ct, -1)11084+
end11085+
kk = kk + 111086+
end11087+
return11088+
end11089+
if nt == "SuperNode"11090+
args_id = @nd_arguments[nid]11091+
if args_id >= 011092+
a_ids = get_args(args_id)11093+
kk = 011094+
while kk < a_ids.length && kk < parent_ptypes.length11095+
aid = a_ids[kk]11096+
at = ""11097+
if @nd_type[aid] == "LocalVariableReadNode"11098+
vname = @nd_name[aid]11099+
pi = 011100+
while pi < child_pnames.length11101+
if child_pnames[pi] == vname && pi < child_ptypes.length11102+
at = child_ptypes[pi]11103+
end11104+
pi = pi + 111105+
end11106+
end11107+
if at == ""11108+
at = infer_type(aid)11109+
end11110+
if at != "" && at != "int"11111+
parent_ptypes[kk] = unify_call_types(parent_ptypes[kk], at, aid)11112+
end11113+
kk = kk + 111114+
end11115+
end11116+
return11117+
end11118+
cs = []11119+
push_child_ids(nid, cs)11120+
k = 011121+
while k < cs.length11122+
propagate_super_walk(cs[k], child_pnames, child_ptypes, parent_ptypes)11123+
k = k + 111124+
end11125+
end11126+
1092911127
def propagate_bare_new_to_subclass_initialize1093011128
ci = 01093111129
while ci < @cls_names.length@@ -12426,6 +12624,65 @@
1242612624
finalize_ivar_heterogeneity1242712625
end1242812626
12627+
# Drop "obj_<bare>" observations from `obs` when (a) <bare> doesn't12628+
# resolve to any registered class, AND (b) some other observation in12629+
# the list is "obj_<scope>_<bare>" for the same trailing <bare> and12630+
# <scope>_<bare> IS registered. Caller passes the raw split list of12631+
# observed types; we return a filtered list with stale unqualified12632+
# obj-names removed (their qualified peer carries the same slot type12633+
# information). Used by finalize_ivar_heterogeneity so the12634+
# distinct-count poly-widen decision doesn't flip a single-class12635+
# ivar to poly just because Pass 1 recorded the bare name and a12636+
# later pass recorded the qualified one.12637+
def drop_stale_unqualified_obj_obs(obs)12638+
out = "".split(",")12639+
# Type-prime locals as strings so spinel-self-compile picks the12640+
# right C type (sp_str_sub_range / sp_str_concat returns12641+
# `const char *`; without the empty-string seed scan_locals12642+
# falls back to `mrb_int`, which then fails the `-Wint-conversion`12643+
# bootstrap when this method is invoked from finalize_ivar_heterogeneity).12644+
o = ""12645+
p = ""12646+
ob = ""12647+
pb = ""12648+
sfx = ""12649+
tail = ""12650+
oi = 012651+
while oi < obs.length12652+
o = obs[oi]12653+
keep = 112654+
if is_obj_type(o) == 112655+
ob = o[4, o.length - 4]12656+
if find_class_idx(ob) < 012657+
oj = 012658+
while oj < obs.length12659+
if oj != oi12660+
p = obs[oj]12661+
if is_obj_type(p) == 112662+
pb = p[4, p.length - 4]12663+
if find_class_idx(pb) >= 012664+
sfx = "_" + ob12665+
if pb.length > sfx.length12666+
tail = pb[(pb.length - sfx.length), sfx.length]12667+
if tail == sfx12668+
keep = 012669+
end12670+
end12671+
end12672+
end12673+
end12674+
oj = oj + 112675+
end12676+
end12677+
end12678+
if keep == 112679+
out.push(o)12680+
end12681+
oi = oi + 112682+
end12683+
out12684+
end12685+
1242912686
def finalize_ivar_heterogeneity1243012687
ci = 01243112688
while ci < @cls_names.length@@ -12437,6 +12694,17 @@
1243712694
while ivk < names.length1243812695
if ivk < types.length && ivk < obs.length && types[ivk] != "poly"1243912696
distinct = obs[ivk].split(",")12697+
# Collapse stale unqualified obj-name observations against12698+
# their qualified form before the distinct-count widening12699+
# decision. A first-pass `@x = Foo.new(...)` recorded inside12700+
# a class whose sibling `Foo` hadn't been registered yet12701+
# stamped "obj_Foo" into the observation list; a later pass12702+
# (with all classes registered) records the proper12703+
# "obj_<scope>_Foo". They refer to the same class, but the12704+
# raw distinct-count below would treat them as 2 types and12705+
# widen the slot to poly. Mirror the update_ivar_type /12706+
# unify_call_types normalization.12707+
distinct = drop_stale_unqualified_obj_obs(distinct)1244012708
if distinct.length >= 21244112709
# When every observed type is itself an array AND the1244212710
# current type is already `poly_array` (set by an earlier@@ -13188,6 +13456,15 @@
1318813456
# subsequent rounds pick up cls method widening from new call1318913457
# sites.1319013458
propagate_bare_new_to_subclass_initialize13459+
# `super` inside a child's `#initialize` calls the parent's13460+
# initialize with the child's params. Propagate the child's13461+
# ptypes (already widened by scan_new_calls' constructor branch13462+
# for `Child.new(args)` call sites) into the parent's ptypes so13463+
# the parent's body sees the right param types for `@apu = apu`13464+
# ivar widening — without this the parent's slot stays at the13465+
# default `mrb_int` even when every `Child.new` site passes a13466+
# typed pointer.13467+
propagate_super_init_to_parent1319113468
# Update ivar types from constructor params1319213469
update_ivar_types_from_params1319313470
# Infer setter param types from ivar types@@ -15336,11 +15613,6 @@
1533615613
infer_function_body_call_types1533715614
infer_class_body_call_types1533815615
infer_ivar_types_from_writers15339-
# After scan_locals has populated @meth_param_empty via the15340-
# per-call-site forward propagation, promote int_array15341-
# params to concrete typed-arrays where bodies push known15342-
# types. The next iteration's scan_locals back-propagates15343-
# those promoted types to caller-side locals.1534415616
infer_param_array_type_from_body1534515617
narrow_param_types_from_body_method_calls1534615618
detect_poly_paramstest/forward_call_class_method_inherited_init.rbaddedView file
Loading diff…
test/forward_call_class_method_inherited_init.rb.expectedaddedView file
Loading diff…
test/forward_call_class_method_inherited_init_int_array.rbaddedView file
Loading diff…
test/forward_call_class_method_inherited_init_int_array.rb.expectedaddedView file
Loading diff…
test/forward_call_class_method_inherited_init_obj.rbaddedView file
Loading diff…
test/forward_call_class_method_inherited_init_obj.rb.expectedaddedView file
Loading diff…
test/forward_call_class_method_int_array.rbaddedView file
Loading diff…
test/forward_call_class_method_int_array.rb.expectedaddedView file
Loading diff…
test/forward_call_class_method_nested.rbaddedView file
Loading diff…
test/forward_call_class_method_nested.rb.expectedaddedView file
Loading diff…
test/forward_call_class_method_obj.rbaddedView file
Loading diff…
test/forward_call_class_method_obj.rb.expectedaddedView file
Loading diff…