Commit bc14d977

bc14d9772da5Merged

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

Description

fix(codegen): truncate extra args in poly-dispatch arms

The arity-padding logic (which pads missing trailing slots so each arm matches its target's fixed C arity) didn't handle the opposite case: when the call site supplies more args than a candidate class's method accepts. With several classes sharing a method name but disagreeing on parameter count, those over-arity arms emitted sp_<Class>_<m>(... too_many_args) and the C compile failed even though the runtime cls_id check would skip the wrong arm.

Rebuild arm_arg_strs from arg_compiled per arm, taking the call site's arg at slot k when present and padding/truncating around the target's arm_ptypes.length. Each arm now compiles with exactly the shape its target signature demands.

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

Details

Commit
bc14d9772da5
Parent
a8c8ac8156dc

spinel_codegen.rbmodifiedView file

@@ -22500,25 +22500,30 @@
2250022500 cname = @cls_names[i]
2250122501 midx = cls_find_method_direct(i, mname)
2250222502 if midx >= 0
22503- # Pad with default-typed zeros when target method has more
22504- # params than the call site supplied — same shape that C
22505- # demands at every fixed-arity call. Per-arm padding lets us
22506- # poly-dispatch `recv.m()` even when one of the candidate
22507- # classes has a method that takes positional defaults.
22508- arm_arg_strs = arg_strs
22503+ # Match each arm to the target method's *fixed* C arity:
22504+ # pad missing trailing slots with default-typed zeros, and
22505+ # truncate extras (when the call site supplies more args than
22506+ # this candidate accepts — happens when several classes share
22507+ # the method name but disagree on parameter count).
22508+ arm_arg_strs = ""
2250922509 all_ptypes = @cls_meth_ptypes[i].split("|")
22510+ arm_ptypes = "".split(",")
2251022511 if midx < all_ptypes.length
2251122512 arm_ptypes = all_ptypes[midx].split(",")
22512- pk = arg_compiled.length
22513- while pk < arm_ptypes.length
22513+ end
22514+ pk = 0
22515+ while pk < arm_ptypes.length
22516+ if pk < arg_compiled.length
22517+ arm_arg_strs = arm_arg_strs + ", " + arg_compiled[pk]
22518+ else
2251422519 pad = "0"
2251522520 pt_base = base_type(arm_ptypes[pk])
2251622521 if pt_base == "poly"
2251722522 pad = "sp_box_nil()"
2251822523 end
2251922524 arm_arg_strs = arm_arg_strs + ", " + pad
22520- pk = pk + 1
2252122525 end
22526+ pk = pk + 1
2252222527 end
2252322528 call_expr = "sp_" + cname + "_" + sanitize_name(mname) + "((sp_" + cname + " *)" + recv_tmp + ".v.p" + arm_arg_strs + ")"
2252422529 rhs = call_expr