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

@@ -5966,12 +5966,27 @@
59665966 cname = rt[4, rt.length - 4]
59675967 ci = find_class_idx(cname)
59685968 if ci >= 0
5969+ # Walk inheritance: when the method isn't on `ci` directly,
5970+ # find the parent that actually defines it and update
5971+ # *that* class's @cls_meth_ptypes so the body-side
5972+ # promotion (infer_param_array_type_from_body) sees the
5973+ # caller's arg types. Issue #84.
5974+ owner_ci = ci
59695975 midx = cls_find_method_direct(ci, mname)
5976+ if midx < 0
5977+ owner = find_method_owner(ci, mname)
5978+ if owner != "" && owner != cname
5979+ owner_ci = find_class_idx(owner)
5980+ if owner_ci >= 0
5981+ midx = cls_find_method_direct(owner_ci, mname)
5982+ end
5983+ end
5984+ end
59705985 if midx >= 0
59715986 args_id = @nd_arguments[nid]
59725987 if args_id >= 0
59735988 arg_ids = get_args(args_id)
5974- all_ptypes = @cls_meth_ptypes[ci].split("|")
5989+ all_ptypes = @cls_meth_ptypes[owner_ci].split("|")
59755990 if midx < all_ptypes.length
59765991 ptypes = all_ptypes[midx].split(",")
59775992 kk = 0
@@ -5987,7 +6002,7 @@
59876002 kk = kk + 1
59886003 end
59896004 all_ptypes[midx] = ptypes.join(",")
5990- @cls_meth_ptypes[ci] = all_ptypes.join("|")
6005+ @cls_meth_ptypes[owner_ci] = all_ptypes.join("|")
59916006 end
59926007 end
59936008 end