Commit 385a3aa6

385a3aa68af6Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #96 from mame/fix/initialize-param-type-clobber

fix(codegen): merge initialize param types from call sites and body

Details

Commit
385a3aa68af6
Parent
45ac5ee6dce8
Merge parent
2bc0d7b2399b

spinel_codegen.rbmodifiedView file

@@ -6456,7 +6456,32 @@
64566456 if mnames[j] == "initialize"
64576457 k = 0
64586458 while k < pnames.length
6459- pt = infer_init_param_type(i, pnames[k])
6459+ # Two sources of param types feed this slot:
6460+ # existing_pt: from infer_constructor_types scanning Foo.new(...)
6461+ # call sites (already widened to "poly" via unify_call_types
6462+ # when call sites disagree).
6463+ # body_pt: from scanning the initialize body for `@x = param`
6464+ # ivar writes; "int" means "no info" (the fallback).
6465+ # Body inference must not silently clobber call-site evidence.
6466+ existing_pt = "int"
6467+ if k < ptypes.length
6468+ existing_pt = ptypes[k]
6469+ end
6470+ body_pt = infer_init_param_type(i, pnames[k])
6471+ pt = body_pt
6472+ if existing_pt != "int" && existing_pt != "nil"
6473+ if body_pt == "int" || body_pt == "nil"
6474+ # Body has no info; keep call-site type.
6475+ pt = existing_pt
6476+ elsif existing_pt == "poly"
6477+ # Call sites already widened to poly; do not narrow.
6478+ pt = "poly"
6479+ elsif body_pt != existing_pt && body_pt != "poly"
6480+ # Two concrete types disagree; demote to poly.
6481+ @needs_rb_value = 1
6482+ pt = "poly"
6483+ end
6484+ end
64606485 if k < ptypes.length
64616486 ptypes[k] = pt
64626487 end