Commit 2d24a16e

2d24a16ed3c8Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #499 from ryanseys/rs-fix-block-abi-trailing-args

fix(codegen): emit (NULL, NULL) for block-ABI trailing args when call site has no block

Details

Commit
2d24a16ed3c8
Parent
e87623480894
Merge parent
5ee6e60232a5

spinel_codegen.rbmodifiedView file

@@ -9224,10 +9224,17 @@
92249224 end
92259225
92269226 # Builds the trailing portion of a call-args list — each non-empty
9227- # piece prefixed with ", ", empties skipped. Returns "" when both
9227+ # piece prefixed with ", ", empties skipped. Returns "" when all
92289228 # are empty. Mirrors build_params_str: callers concatenate the
92299229 # result onto a self/recv prefix to form the full arg list.
9230- def build_call_tail(ca, bp)
9230+ #
9231+ # `yargs` carries the trailing yield ABI args (`_block, _benv`) when
9232+ # the callee declares `yield` but the call site doesn't pass a
9233+ # literal block. Without it, methods with both `&block` and `yield`
9234+ # in their body get a 3-arg call against a 5-arg signature and clang
9235+ # fails the build. Mirrors the `, NULL, NULL` shape that the
9236+ # top-level no-recv path appends in compile_no_recv_call_expr.
9237+ def build_call_tail(ca, bp, yargs = "")
92319238 result = ""
92329239 if ca != ""
92339240 result = result + ", " + ca
@@ -9235,9 +9242,29 @@
92359242 if bp != ""
92369243 result = result + ", " + bp
92379244 end
9245+ if yargs != ""
9246+ result = result + yargs
9247+ end
92389248 result
92399249 end
92409250
9251+ # Returns the trailing yield-ABI args (`, NULL, NULL`) when an
9252+ # instance method declares `yield` in its body but the call site
9253+ # doesn't inline (no literal block — those route through
9254+ # compile_yield_call_stmt and bypass the C call entirely). The
9255+ # callee's C signature is `..., _block, _benv` whenever
9256+ # cls_method_has_yield is set; omitting these triggers
9257+ # "too few arguments to function call" at clang time.
9258+ def cls_call_yargs(ci, midx)
9259+ if ci < 0 || midx < 0
9260+ return ""
9261+ end
9262+ if cls_method_has_yield(ci, midx) == 1
9263+ return ", NULL, NULL"
9264+ end
9265+ ""
9266+ end
9267+
92419268 # ---- Emit top-level methods ----
92429269 def emit_toplevel_methods
92439270 i = 0
@@ -12921,6 +12948,12 @@
1292112948 bp = "0"
1292212949 end
1292312950 end
12951+ # Pad the trailing `_block, _benv` yield-ABI slots when the callee's
12952+ # body uses `yield`. The literal-block path (compile_yield_call_stmt)
12953+ # bypasses this C call entirely by inlining the body, so reaching
12954+ # here means no literal block at the call site — `NULL, NULL` is
12955+ # always safe.
12956+ yargs = cls_call_yargs(owner_ci, owner_midx)
1292412957 # When the call resolves through inheritance to an
1292512958 # ancestor's method, the C function takes `sp_<owner>
1292612959 # *self`. Our `self` here is `sp_<current> *`. Cast so
@@ -12966,7 +12999,7 @@
1296612999 if rt_ok == 1 && pt_ok == 1
1296713000 tmp = new_temp
1296813001 rt_c = c_type(base_rt)
12969- default_call = "sp_" + owner + "_" + sanitize_name(mname) + "(" + recv_arg + build_call_tail(ca, bp) + ")"
13002+ default_call = "sp_" + owner + "_" + sanitize_name(mname) + "(" + recv_arg + build_call_tail(ca, bp, yargs) + ")"
1297013003 # Initialize tmp to a type-default so the base call only runs
1297113004 # in the default arm — calling it eagerly before the switch
1297213005 # would raise from an abstract stub before any subclass arm
@@ -12981,7 +13014,7 @@
1298113014 cand_owner2 = p2[1].to_i
1298213015 cand_name = @cls_names[cand_owner2]
1298313016 cand_recv = "(sp_" + cand_name + " *)" + self_expr
12984- cand_call = "sp_" + cand_name + "_" + sanitize_name(mname) + "(" + cand_recv + build_call_tail(ca, bp) + ")"
13017+ cand_call = "sp_" + cand_name + "_" + sanitize_name(mname) + "(" + cand_recv + build_call_tail(ca, bp, yargs) + ")"
1298513018 emit(" case " + cand_cid.to_s + "LL: " + tmp + " = " + cand_call + "; break;")
1298613019 ol2 = ol2 + 1
1298713020 end
@@ -12991,7 +13024,7 @@
1299113024 end
1299213025 end
1299313026 end
12994- return "sp_" + owner + "_" + sanitize_name(mname) + "(" + recv_arg + build_call_tail(ca, bp) + ")"
13027+ return "sp_" + owner + "_" + sanitize_name(mname) + "(" + recv_arg + build_call_tail(ca, bp, yargs) + ")"
1299513028 end
1299613029 # Check attr_readers (bare method call like `x` meaning self.x)
1299713030 readers = @cls_attr_readers[@current_class_idx].split(";")
@@ -17976,7 +18009,12 @@
1797618009 bp = "0"
1797718010 end
1797818011 end
17979- tail = build_call_tail(ca, bp)
18012+ # Pad the trailing `_block, _benv` yield-ABI slots when the callee's
18013+ # body uses `yield`. The literal-block path inlines via
18014+ # compile_yield_call_stmt and bypasses this C call entirely, so
18015+ # reaching here means no literal block at the call site.
18016+ yargs = cls_call_yargs(oci2, midx2)
18017+ tail = build_call_tail(ca, bp, yargs)
1798018018 if owner == cname
1798118019 return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + tail + ")"
1798218020 else
@@ -29903,7 +29941,17 @@
2990329941 while emitted.length < @current_method_yield_arity
2990429942 emitted.push("0")
2990529943 end
29906- emit(" if (_block) _block(" + emitted.join(", ") + ", _benv);")
29944+ # When the enclosing method declares `&block`, fall back to invoking the
29945+ # captured proc if the yield-ABI slot is empty. This matches Ruby semantics
29946+ # (yield and `block.call` drive the same proc) and lets `&block_arg` call
29947+ # sites work even when the call emit fills lv_block but leaves _block NULL.
29948+ if @current_method_block_param != ""
29949+ bp = "lv_" + @current_method_block_param
29950+ emit(" if (_block) _block(" + emitted.join(", ") + ", _benv);")
29951+ emit(" else if (" + bp + ") sp_proc_call(" + bp + ", (mrb_int[]){" + emitted.join(", ") + "});")
29952+ else
29953+ emit(" if (_block) _block(" + emitted.join(", ") + ", _benv);")
29954+ end
2990729955 end
2990829956
2990929957 def compile_yield_call_stmt(nid, mi)