Commit 71c12b5f

71c12b5ff39fMerged

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

Description

fix(codegen): pad missing positional args in poly-dispatch arms

When recv.m(...) is compiled with a poly receiver, the dispatch emits one C-call arm per user class that defines m. Each arm has to match its target's fixed C arity, which can differ from the call site's argument count when one of the candidate methods takes positional defaults. Without padding, the C compiler rejects the table with too few arguments to function 'sp_<Class>_<m>' even though the runtime cls_id check would skip that arm.

Pad the missing trailing slots with default-typed zeros (0 for non-poly param types, sp_box_nil() for poly) so each arm compiles regardless of which other class's method dictates the column.

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

Details

Commit
71c12b5ff39f
Parent
15f7e5f356d7

spinel_codegen.rbmodifiedView file

@@ -21680,7 +21680,27 @@
2168021680 cname = @cls_names[i]
2168121681 midx = cls_find_method_direct(i, mname)
2168221682 if midx >= 0
21683- call_expr = "sp_" + cname + "_" + sanitize_name(mname) + "((sp_" + cname + " *)" + recv_tmp + ".v.p" + arg_strs + ")"
21683+ # Pad with default-typed zeros when target method has more
21684+ # params than the call site supplied — same shape that C
21685+ # demands at every fixed-arity call. Per-arm padding lets us
21686+ # poly-dispatch `recv.m()` even when one of the candidate
21687+ # classes has a method that takes positional defaults.
21688+ arm_arg_strs = arg_strs
21689+ all_ptypes = @cls_meth_ptypes[i].split("|")
21690+ if midx < all_ptypes.length
21691+ arm_ptypes = all_ptypes[midx].split(",")
21692+ pk = arg_compiled.length
21693+ while pk < arm_ptypes.length
21694+ pad = "0"
21695+ pt_base = base_type(arm_ptypes[pk])
21696+ if pt_base == "poly"
21697+ pad = "sp_box_nil()"
21698+ end
21699+ arm_arg_strs = arm_arg_strs + ", " + pad
21700+ pk = pk + 1
21701+ end
21702+ end
21703+ call_expr = "sp_" + cname + "_" + sanitize_name(mname) + "((sp_" + cname + " *)" + recv_tmp + ".v.p" + arm_arg_strs + ")"
2168421704 rhs = call_expr
2168521705 if is_poly_ret == 1
2168621706 this_rt = cls_method_return(i, mname)