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

@@ -5677,6 +5677,17 @@
56775677 end
56785678
56795679 def infer_init_param_type(ci, pname)
5680+ # Synthetic Struct.new(...) constructors (body id -2) have no AST
5681+ # body to scan — the implicit rule is "param pname → @pname = pname",
5682+ # so the param type must match the ivar type. update_ivar_types_from_params
5683+ # has already propagated call-site-inferred types into ivars by this point.
5684+ init_idx0 = cls_find_method_direct(ci, "initialize")
5685+ if init_idx0 >= 0
5686+ bodies0 = @cls_meth_bodies[ci].split(";")
5687+ if init_idx0 < bodies0.length && bodies0[init_idx0].to_i == -2
5688+ return cls_ivar_type(ci, "@" + pname)
5689+ end
5690+ end
56805691 # Check if param is assigned to an ivar in initialize
56815692 mnames = @cls_meth_names[ci].split(";")
56825693 bodies = @cls_meth_bodies[ci].split(";")