Commit 61e94e2a

61e94e2a6898Merged

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

Description

fix(codegen): forward arguments through poly method dispatch

compile_poly_method_call emitted sp_<Class>_<method>(recv) with no argument list, dropping any positional arguments at the call site. The following pattern didn't compile:

class Doubler; def call(x); x * 2; end; end class Tripler; def call(x); x * 3; end; end def apply(a, x); a.call(x); end apply(Doubler.new, 5) # 10 apply(Tripler.new, 5) # 15

Master fails with:

error: too few arguments to function 'sp_Doubler_call'; expected 2, have 1

Stash the receiver in a temp so it isn't re-evaluated for each cls_id branch, compile the argument list once into arg_strs, and forward it verbatim into every sp_<Class>_<method>(self, args...) call.

Adds test/bm_poly_method_args.rb as a small reproducer.

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

Details

Commit
61e94e2a6898
Parent
36cc6fb9bd66

spinel_codegen.rbmodifiedView file

@@ -15740,21 +15740,36 @@
1574015740 end
1574115741 ret_ct = c_type(ret_type)
1574215742 ret_def = c_default_val(ret_type)
15743+ # Stash the receiver in a temp so we don't re-evaluate the
15744+ # expression for every if-branch below.
15745+ recv_tmp = new_temp
15746+ emit(" sp_RbVal " + recv_tmp + " = " + rc + ";")
15747+ # Compile the call's argument list once.
15748+ arg_strs = ""
15749+ args_id = @nd_arguments[nid]
15750+ if args_id >= 0
15751+ aargs = get_args(args_id)
15752+ k = 0
15753+ while k < aargs.length
15754+ arg_strs = arg_strs + ", " + compile_expr(aargs[k])
15755+ k = k + 1
15756+ end
15757+ end
1574315758 tmp = new_temp
1574415759 emit(" " + ret_ct + " " + tmp + " = " + ret_def + ";")
15745- emit(" if (" + rc + ".tag == SP_TAG_OBJ) {")
15760+ emit(" if (" + recv_tmp + ".tag == SP_TAG_OBJ) {")
1574615761 i = 0
1574715762 while i < @cls_names.length
1574815763 cname = @cls_names[i]
1574915764 midx = cls_find_method_direct(i, mname)
1575015765 if midx >= 0
15751- call_expr = "sp_" + cname + "_" + sanitize_name(mname) + "((sp_" + cname + " *)" + rc + ".v.p)"
15766+ call_expr = "sp_" + cname + "_" + sanitize_name(mname) + "((sp_" + cname + " *)" + recv_tmp + ".v.p" + arg_strs + ")"
1575215767 rhs = call_expr
1575315768 if is_poly_ret == 1
1575415769 this_rt = cls_method_return(i, mname)
1575515770 rhs = box_val_to_poly(call_expr, this_rt)
1575615771 end
15757- emit(" if (" + rc + ".v.cls_id == " + i.to_s + ") " + tmp + " = " + rhs + ";")
15772+ emit(" if (" + recv_tmp + ".v.cls_id == " + i.to_s + ") " + tmp + " = " + rhs + ";")
1575815773 end
1575915774 i = i + 1
1576015775 end