Commit 437ecd14
437ecd14c6adMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: synthetic Struct constructors derive param types from ivars
scan_new_calls infers ctor param types from call sites like Session.new(pid: 1, name: "foo") and writes the refined types into @cls_meth_ptypes. For user-defined initialize methods, infer_init_param_type then re-confirms the type by walking the method body's ivar writes. But for synthetic struct constructors (body id == -2) the body loop finds nothing and the function falls through to "int" — clobbering the call-site-inferred string/float/etc types.
Add an early-return path: when the initialize body is the synthetic marker, read the type straight off the matching ivar. ivar types are already populated by update_ivar_types_from_params at this point, so the result reflects the same call-site inference but without the "int" overwrite.
Clears warnings on bm_struct_inherit (2) and bm_struct_kw (4). bm_poly still has one warning but it's unrelated — a polymorphic local getting int then string assignments, not a Struct issue.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 437ecd14c6ad
- Parent
- 9e8e96162baa
spinel_codegen.rbmodifiedView file
end def infer_init_param_type(ci, pname) # Synthetic Struct.new(...) constructors (body id -2) have no AST # body to scan — the implicit rule is "param pname → @pname = pname", # so the param type must match the ivar type. update_ivar_types_from_params # has already propagated call-site-inferred types into ivars by this point. init_idx0 = cls_find_method_direct(ci, "initialize") if init_idx0 >= 0 bodies0 = @cls_meth_bodies[ci].split(";") if init_idx0 < bodies0.length && bodies0[init_idx0].to_i == -2 return cls_ivar_type(ci, "@" + pname) end end # Check if param is assigned to an ivar in initialize mnames = @cls_meth_names[ci].split(";") bodies = @cls_meth_bodies[ci].split(";")