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
cls_meth_ptypes_put(parent_init_ci, parent_init_idx, parent_ptypes) end def propagate_super_methods_to_parent ci = 0 while ci < @cls_names.length if @cls_parents[ci] != "" child_method_names = @cls_meth_names[ci].split(";") j = 0 while j < child_method_names.length mname = child_method_names[j] if mname != "" && mname != "initialize" # Walk the parent chain to find the closest ancestor that # defines a same-named method. walk_ci_sm = find_class_idx(@cls_parents[ci]) parent_ci_sm = -1 while walk_ci_sm >= 0 if cls_find_method_direct(walk_ci_sm, mname) >= 0 parent_ci_sm = walk_ci_sm break end parent_str_sm = @cls_parents[walk_ci_sm] if parent_str_sm == "" break end walk_ci_sm = find_class_idx(parent_str_sm) end if parent_ci_sm >= 0 propagate_super_method_one(ci, j, parent_ci_sm, mname) end end j = j + 1 end end ci = ci + 1 end end def propagate_super_method_one(child_ci, child_mi, parent_ci, mname) bodies = @cls_meth_bodies[child_ci].split(";") if child_mi >= bodies.length return end bs = bodies[child_mi] if bs == "" return end bid = bs.to_i if bid < 0 return end child_pnames = cls_meth_pnames_get(child_ci, child_mi) child_ptypes = cls_meth_ptypes_get(child_ci, child_mi) parent_mi = cls_find_method_direct(parent_ci, mname) if parent_mi < 0 return end parent_ptypes = cls_meth_ptypes_get(parent_ci, parent_mi) if parent_ptypes.length == 0 return end propagate_super_walk(bid, child_pnames, child_ptypes, parent_ptypes) cls_meth_ptypes_put(parent_ci, parent_mi, parent_ptypes) end def propagate_super_walk(nid, child_pnames, child_ptypes, parent_ptypes) if nid < 0 return # default `mrb_int` even when every `Child.new` site passes a # typed pointer. propagate_super_init_to_parent # Same propagation but for instance methods other than initialize: # `def greet(name); super; end` in a Child class should let the # parent's #greet see `name` as String when Child#greet is called # with a String arg. propagate_super_methods_to_parent # Update ivar types from constructor params update_ivar_types_from_params # Infer setter param types from ivar typestest/for_multi_var.rbmodifiedView file
Loading diff…
test/for_multi_var.rb.expectedmodifiedView file
Loading diff…
test/super_with_args.rbaddedView file
Loading diff…
test/super_with_args.rb.expectedaddedView file
Loading diff…