Commit 2966d2d4
2966d2d40985Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): walk inheritance chain when propagating caller arg types to params
Issue #84: an empty [] literal passed to an inherited method
didn't get its element type promoted because the caller-side
inference (Child.new.add_to(...)) only updated Child's
@cls_meth_ptypes, never Base's where the method body actually
lives. The body-side promotion in infer_param_array_type_from_body
then saw the slot still typed int and the strict guard
(int_array only) never fired — so lv_buf got declared mrb_int
even though the body did buf.push("hi").
Walk the inheritance chain in the propagation site: when
cls_find_method_direct(child_ci, mname) < 0, fall back to
find_method_owner to get the parent class that actually defines
the method, and update that class's @cls_meth_ptypes. The
existing strict guard then fires for the parent, the body
promotion picks the right typed array, and codegen emits the
matching lv_buf declaration.
This is option 1 from the issue's two-option list. Option 2 (loosening the guard plus class-method poly detection for mixed callers) remains a separate, larger refactor.
test/empty_array_inherited_method.rb covers single inheritance,
two-deep inheritance (Grandchild → Child → Base), and a mixed
class that defines its own add_to while inheriting also_add —
the propagation must hit the right @cls_meth_ptypes slot in
each case.
Closes #84
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 2966d2d40985
- Parent
- cb86bd52ab10
spinel_codegen.rbmodifiedView file
cname = rt[4, rt.length - 4] ci = find_class_idx(cname) if ci >= 0 # Walk inheritance: when the method isn't on `ci` directly, # find the parent that actually defines it and update # *that* class's @cls_meth_ptypes so the body-side # promotion (infer_param_array_type_from_body) sees the # caller's arg types. Issue #84. owner_ci = ci midx = cls_find_method_direct(ci, mname) if midx < 0 owner = find_method_owner(ci, mname) if owner != "" && owner != cname owner_ci = find_class_idx(owner) if owner_ci >= 0 midx = cls_find_method_direct(owner_ci, mname) end end end if midx >= 0 args_id = @nd_arguments[nid] if args_id >= 0 arg_ids = get_args(args_id) all_ptypes = @cls_meth_ptypes[ci].split("|") all_ptypes = @cls_meth_ptypes[owner_ci].split("|") if midx < all_ptypes.length ptypes = all_ptypes[midx].split(",") kk = 0 kk = kk + 1 end all_ptypes[midx] = ptypes.join(",") @cls_meth_ptypes[ci] = all_ptypes.join("|") @cls_meth_ptypes[owner_ci] = all_ptypes.join("|") end end endtest/empty_array_inherited_method.rbaddedView file
Loading diff…