Commit 60016a69

60016a69a438Merged

Yusuke Endoh <[email protected]> · 6 months ago · 7 files · +6 added · ~1 modified

Description

fix(codegen): propagate child#initialize ptypes to parent#initialize via super

scan_new_calls' constructor branch widens the child's #initialize ptypes from Class.new(args) call sites: a Sub.new(owner_obj) site picks up Sub#initialize's _owner param as obj_<C>. But when the child's body is a bare super, the C lowering forwards to the parent's #initialize, and the parent's ptypes never get widened — they stay at the default mrb_int. The parent's body's @owner = owner then stores the int payload (with the call site casting the typed pointer to int — itself a hard error for value-typed args), and every @owner.<method> dispatch through the resulting int-typed ivar on a child instance lands on whatever class shares the int recv's cls_id, sometimes self-recursing.

Add a new pass propagate_super_init_to_parent that runs inside the iterative inference loop (right after propagate_bare_new_to_subclass_initialize). For each class with a parent and its own #initialize, walk the body looking for super call shapes:

  • ForwardingSuperNode (bare super): forwards every param of the current method by name. Unify the parent ptypes element-wise against the child ptypes (capped at min length, so a wider parent signature stays unwidened past the forwarded prefix).

  • SuperNode (super(arg1, arg2, …)): explicit arg list. Each arg may be a LocalVariableReadNode pointing at one of the current method's params; if so, use the param's already-widened ptype to unify at the super-arg's position. Other shapes fall through to infer_type.

The walk skips into nested DefNodes (those introduce their own scope with different params) but otherwise mirrors scan_new_calls' recursion shape so a super at any depth in the body is reachable.

Walks the parent chain past intermediate classes that don't define their own #initialize, finding the first ancestor that does — so an empty marker subclass (class Mid < Base; end) between the super-caller and its semantic parent doesn't break the propagation.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
60016a69a438
Parent
febbfd30b82a

spinel_codegen.rbmodifiedView file

@@ -10971,6 +10971,150 @@
1097110971 # resolve via the cls method's pnames/ptypes; other args fall
1097210972 # through to infer_type with @current_class_idx pinned so
1097310973 # @ivar refs in the args resolve against the right class.
10974+ # Propagate ptypes from child#initialize to parent#initialize via
10975+ # super calls. Without this, a `Sub.new(owner_obj)` site widens
10976+ # `Sub#initialize`'s `_owner` param to `obj_<C>` correctly (via
10977+ # the existing constructor branch in scan_new_calls), but the
10978+ # body's bare `super` lowers to a C call against the parent's
10979+ # `Base#initialize`, whose param ptype stays at the default
10980+ # `mrb_int` because no call-site path widens the *parent's*
10981+ # initialize through super. The C output then casts the typed
10982+ # pointer to `mrb_int` at the super-call site, the parent's body's
10983+ # `@owner = owner` records the int into the slot, and every
10984+ # subsequent `@owner.<method>` dispatch on a child instance lands
10985+ # on whatever class shares the int recv's cls_id — commonly the
10986+ # wrong one.
10987+ #
10988+ # ForwardingSuperNode (bare `super`): forwards every param of the
10989+ # current method by name. Unify the parent ptypes element-wise
10990+ # against the child ptypes (capped at min length, so a wider
10991+ # parent signature stays unwidened past the forwarded prefix).
10992+ #
10993+ # SuperNode (`super(arg1, arg2, …)`): explicit arg list. Each arg
10994+ # might be a LocalVariableReadNode that points back at one of the
10995+ # current method's params; if so, use the param's ptype to unify
10996+ # at the super arg's position. Other shapes (literals, calls)
10997+ # fall through to infer_type.
10998+ def propagate_super_init_to_parent
10999+ ci = 0
11000+ while ci < @cls_names.length
11001+ if @cls_parents[ci] != ""
11002+ parent_ci = find_class_idx(@cls_parents[ci])
11003+ # The parent chain may insert classes without their own
11004+ # #initialize (e.g. an empty marker subclass between the
11005+ # super caller and its semantic parent). Walk up until we
11006+ # find one that does.
11007+ walk_ci = parent_ci
11008+ parent_init_ci = -1
11009+ while walk_ci >= 0
11010+ if cls_find_method_direct(walk_ci, "initialize") >= 0
11011+ parent_init_ci = walk_ci
11012+ break
11013+ end
11014+ parent_str = @cls_parents[walk_ci]
11015+ if parent_str == ""
11016+ break
11017+ end
11018+ walk_ci = find_class_idx(parent_str)
11019+ end
11020+ if parent_init_ci >= 0
11021+ propagate_super_init_one(ci, parent_init_ci)
11022+ end
11023+ end
11024+ ci = ci + 1
11025+ end
11026+ end
11027+
11028+ def propagate_super_init_one(child_ci, parent_init_ci)
11029+ child_init_idx = cls_find_method_direct(child_ci, "initialize")
11030+ if child_init_idx < 0
11031+ return
11032+ end
11033+ bodies = @cls_meth_bodies[child_ci].split(";")
11034+ if child_init_idx >= bodies.length
11035+ return
11036+ end
11037+ bid = bodies[child_init_idx].to_i
11038+ if bid < 0
11039+ return
11040+ end
11041+ child_pnames = cls_meth_pnames_get(child_ci, child_init_idx)
11042+ child_ptypes = cls_meth_ptypes_get(child_ci, child_init_idx)
11043+ parent_init_idx = cls_find_method_direct(parent_init_ci, "initialize")
11044+ if parent_init_idx < 0
11045+ return
11046+ end
11047+ parent_ptypes = cls_meth_ptypes_get(parent_init_ci, parent_init_idx)
11048+ if parent_ptypes.length == 0
11049+ return
11050+ end
11051+ propagate_super_walk(bid, child_pnames, child_ptypes, parent_ptypes)
11052+ cls_meth_ptypes_put(parent_init_ci, parent_init_idx, parent_ptypes)
11053+ end
11054+
11055+ def propagate_super_walk(nid, child_pnames, child_ptypes, parent_ptypes)
11056+ if nid < 0
11057+ return
11058+ end
11059+ nt = @nd_type[nid]
11060+ if nt == "DefNode"
11061+ return
11062+ end
11063+ if nt == "ForwardingSuperNode"
11064+ # Bare `super`: forward every child param into the parent's slot
11065+ # at the same position, capped at min length.
11066+ kk = 0
11067+ lim = child_ptypes.length
11068+ if parent_ptypes.length < lim
11069+ lim = parent_ptypes.length
11070+ end
11071+ while kk < lim
11072+ ct = child_ptypes[kk]
11073+ if ct != "" && ct != "int"
11074+ parent_ptypes[kk] = unify_call_types(parent_ptypes[kk], ct, -1)
11075+ end
11076+ kk = kk + 1
11077+ end
11078+ return
11079+ end
11080+ if nt == "SuperNode"
11081+ args_id = @nd_arguments[nid]
11082+ if args_id >= 0
11083+ a_ids = get_args(args_id)
11084+ kk = 0
11085+ while kk < a_ids.length && kk < parent_ptypes.length
11086+ aid = a_ids[kk]
11087+ at = ""
11088+ if @nd_type[aid] == "LocalVariableReadNode"
11089+ vname = @nd_name[aid]
11090+ pi = 0
11091+ while pi < child_pnames.length
11092+ if child_pnames[pi] == vname && pi < child_ptypes.length
11093+ at = child_ptypes[pi]
11094+ end
11095+ pi = pi + 1
11096+ end
11097+ end
11098+ if at == ""
11099+ at = infer_type(aid)
11100+ end
11101+ if at != "" && at != "int"
11102+ parent_ptypes[kk] = unify_call_types(parent_ptypes[kk], at, aid)
11103+ end
11104+ kk = kk + 1
11105+ end
11106+ end
11107+ return
11108+ end
11109+ cs = []
11110+ push_child_ids(nid, cs)
11111+ k = 0
11112+ while k < cs.length
11113+ propagate_super_walk(cs[k], child_pnames, child_ptypes, parent_ptypes)
11114+ k = k + 1
11115+ end
11116+ end
11117+
1097411118 def propagate_bare_new_to_subclass_initialize
1097511119 ci = 0
1097611120 while ci < @cls_names.length
@@ -13303,6 +13447,15 @@
1330313447 # subsequent rounds pick up cls method widening from new call
1330413448 # sites.
1330513449 propagate_bare_new_to_subclass_initialize
13450+ # `super` inside a child's `#initialize` calls the parent's
13451+ # initialize with the child's params. Propagate the child's
13452+ # ptypes (already widened by scan_new_calls' constructor branch
13453+ # for `Child.new(args)` call sites) into the parent's ptypes so
13454+ # the parent's body sees the right param types for `@apu = apu`
13455+ # ivar widening — without this the parent's slot stays at the
13456+ # default `mrb_int` even when every `Child.new` site passes a
13457+ # typed pointer.
13458+ propagate_super_init_to_parent
1330613459 # Update ivar types from constructor params
1330713460 update_ivar_types_from_params
1330813461 # Infer setter param types from ivar types