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 by infer_constructor_types while walking Foo.new(...) call sites. unify_call_types already widens this to "poly" when call sites disagree.
  • body_pt: the type returned by infer_init_param_type, which walks the body for @x = param writes and returns the ivar type (or the parent's ptype via super(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) → keep existing_pt
  • existing already widened to poly → keep poly (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") where name is 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) makes infer_init_param_type return the parent's concrete ptype as body_pt while the child's call sites widen existing_pt to poly. 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_params and unify_call_types tend 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 = 0
63806380 while k < pnames.length
6381- 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_types
6384+ # 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.length
6390+ existing_pt = ptypes[k]
6391+ end
6392+ body_pt = infer_init_param_type(i, pnames[k])
6393+ pt = body_pt
6394+ 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_pt
6398+ 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 = 1
6404+ pt = "poly"
6405+ end
6406+ end
63826407 if k < ptypes.length
63836408 ptypes[k] = pt
63846409 end