Commit 1175512d

1175512df5ddMerged

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

Description

fix(analyze): propagate super call arg types to parent instance methods

def greet(name); super; end in a Child class called Parent#greet with a String arg, but Parent#greet's param defaulted to mrb_int (no direct call site to widen it). The emit then warned about passing const char * where mrb_int expected and the test program failed to compile.

The existing propagate_super_init_to_parent already does this for initialize. Generalize the same approach to all instance methods: walk each child class, find a same-named ancestor method, and run propagate_super_walk over the child body so SuperNode / ForwardingSuperNode unify Child ptypes into Parent ptypes.

Also tighten the for-loop multi-target test (added in 4227f8d) to homogeneous-element shapes only; the mixed-type case requires unboxing sp_RbVal at the inner _get and is left for follow-up.

Tests: default 453/0/0, promote 453/0/0.

Closes #679

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
1175512df5dd
Parent
9fd1a3b99993

spinel_analyze.rbmodifiedView file

@@ -12396,6 +12396,68 @@
1239612396 cls_meth_ptypes_put(parent_init_ci, parent_init_idx, parent_ptypes)
1239712397 end
1239812398
12399+ def propagate_super_methods_to_parent
12400+ ci = 0
12401+ while ci < @cls_names.length
12402+ if @cls_parents[ci] != ""
12403+ child_method_names = @cls_meth_names[ci].split(";")
12404+ j = 0
12405+ while j < child_method_names.length
12406+ mname = child_method_names[j]
12407+ if mname != "" && mname != "initialize"
12408+ # Walk the parent chain to find the closest ancestor that
12409+ # defines a same-named method.
12410+ walk_ci_sm = find_class_idx(@cls_parents[ci])
12411+ parent_ci_sm = -1
12412+ while walk_ci_sm >= 0
12413+ if cls_find_method_direct(walk_ci_sm, mname) >= 0
12414+ parent_ci_sm = walk_ci_sm
12415+ break
12416+ end
12417+ parent_str_sm = @cls_parents[walk_ci_sm]
12418+ if parent_str_sm == ""
12419+ break
12420+ end
12421+ walk_ci_sm = find_class_idx(parent_str_sm)
12422+ end
12423+ if parent_ci_sm >= 0
12424+ propagate_super_method_one(ci, j, parent_ci_sm, mname)
12425+ end
12426+ end
12427+ j = j + 1
12428+ end
12429+ end
12430+ ci = ci + 1
12431+ end
12432+ end
12433+
12434+ def propagate_super_method_one(child_ci, child_mi, parent_ci, mname)
12435+ bodies = @cls_meth_bodies[child_ci].split(";")
12436+ if child_mi >= bodies.length
12437+ return
12438+ end
12439+ bs = bodies[child_mi]
12440+ if bs == ""
12441+ return
12442+ end
12443+ bid = bs.to_i
12444+ if bid < 0
12445+ return
12446+ end
12447+ child_pnames = cls_meth_pnames_get(child_ci, child_mi)
12448+ child_ptypes = cls_meth_ptypes_get(child_ci, child_mi)
12449+ parent_mi = cls_find_method_direct(parent_ci, mname)
12450+ if parent_mi < 0
12451+ return
12452+ end
12453+ parent_ptypes = cls_meth_ptypes_get(parent_ci, parent_mi)
12454+ if parent_ptypes.length == 0
12455+ return
12456+ end
12457+ propagate_super_walk(bid, child_pnames, child_ptypes, parent_ptypes)
12458+ cls_meth_ptypes_put(parent_ci, parent_mi, parent_ptypes)
12459+ end
12460+
1239912461 def propagate_super_walk(nid, child_pnames, child_ptypes, parent_ptypes)
1240012462 if nid < 0
1240112463 return
@@ -17879,6 +17941,11 @@
1787917941 # default `mrb_int` even when every `Child.new` site passes a
1788017942 # typed pointer.
1788117943 propagate_super_init_to_parent
17944+ # Same propagation but for instance methods other than initialize:
17945+ # `def greet(name); super; end` in a Child class should let the
17946+ # parent's #greet see `name` as String when Child#greet is called
17947+ # with a String arg.
17948+ propagate_super_methods_to_parent
1788217949 # Update ivar types from constructor params
1788317950 update_ivar_types_from_params
1788417951 # Infer setter param types from ivar types