Commit 2bc0d7b2
2bc0d7b2399bMerged
Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): merge initialize param types from call sites and body
Foo.initialize parameter types come from two independent inference
sources that the codegen previously combined incorrectly:
existing_pt: the type accumulated byinfer_constructor_typeswhile walkingFoo.new(...)call sites.unify_call_typesalready widens this to"poly"when call sites disagree.body_pt: the type returned byinfer_init_param_type, which walks the body for@x = paramwrites and returns the ivar type (or the parent's ptype viasuper(param))."int"is the "no-info" fallback when no pattern matches.
Master unconditionally overwrote existing_pt with body_pt, so a
call site that had refined the type (e.g. Foo.new("hello") →
"string") was silently clobbered to "int" whenever the body did
not happen to write the param to an ivar.
Replace the overwrite with a four-case merge:
- body has no info (
int/nil) → keepexisting_pt - existing already widened to
poly→ keeppoly(don't narrow) - both concrete, same type → use it (default branch)
- both concrete, different types → demote to
poly
Notes on the table:
- The "existing concrete + body no-info" case is the originally
reported regression and is covered by
test/initialize_param_through_call.rb (a
Greeter.new("hello")wherenameis never assigned to an ivar). Without the fix this test errors out at compile time / produces wrong output; with the fix it compiles cleanly. - The "existing poly + body concrete" case is real in larger
programs (e.g. optcarrot):
super(param)makesinfer_init_param_typereturn the parent's concrete ptype asbody_ptwhile the child's call sites widenexisting_pttopoly. test/initialize_param_poly_call_sites.rb is a smoke test that exercises the basic poly-through-call-sites path; a tighter end-to-end repro for the super-propagation variant runs into independent codegen issues (super arg type mismatch, string literal → poly ivar assignment) and is left for a follow-up. - The "concrete A + concrete B" demotion is added defensively so
the merge logic stays correct under future inference changes;
current Spinel inference rarely produces this shape because
update_ivar_types_from_paramsandunify_call_typestend to converge to the same answer first.
Details
- Commit
- 2bc0d7b2399b
- Parent
- 0c3c5d918fc5
spinel_codegen.rbmodifiedView file
@@ -6378,7 +6378,32 @@
63786378
if mnames[j] == "initialize"63796379
k = 063806380
while k < pnames.length6381-
pt = infer_init_param_type(i, pnames[k])6381+
# Two sources of param types feed this slot:6382+
# existing_pt: from infer_constructor_types scanning Foo.new(...)6383+
# call sites (already widened to "poly" via unify_call_types6384+
# when call sites disagree).6385+
# body_pt: from scanning the initialize body for `@x = param`6386+
# ivar writes; "int" means "no info" (the fallback).6387+
# Body inference must not silently clobber call-site evidence.6388+
existing_pt = "int"6389+
if k < ptypes.length6390+
existing_pt = ptypes[k]6391+
end6392+
body_pt = infer_init_param_type(i, pnames[k])6393+
pt = body_pt6394+
if existing_pt != "int" && existing_pt != "nil"6395+
if body_pt == "int" || body_pt == "nil"6396+
# Body has no info; keep call-site type.6397+
pt = existing_pt6398+
elsif existing_pt == "poly"6399+
# Call sites already widened to poly; do not narrow.6400+
pt = "poly"6401+
elsif body_pt != existing_pt && body_pt != "poly"6402+
# Two concrete types disagree; demote to poly.6403+
@needs_rb_value = 16404+
pt = "poly"6405+
end6406+
end63826407
if k < ptypes.length63836408
ptypes[k] = pt63846409
endtest/initialize_param_poly_call_sites.rbaddedView file
Loading diff…
test/initialize_param_through_call.rbaddedView file
Loading diff…