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
end92259225
92269226
# Builds the trailing portion of a call-args list — each non-empty9227-
# piece prefixed with ", ", empties skipped. Returns "" when both9227+
# piece prefixed with ", ", empties skipped. Returns "" when all92289228
# are empty. Mirrors build_params_str: callers concatenate the92299229
# 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`) when9232+
# the callee declares `yield` but the call site doesn't pass a9233+
# 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 clang9235+
# fails the build. Mirrors the `, NULL, NULL` shape that the9236+
# 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 + ", " + bp92379244
end9245+
if yargs != ""9246+
result = result + yargs9247+
end92389248
result92399249
end92409250
9251+
# Returns the trailing yield-ABI args (`, NULL, NULL`) when an9252+
# instance method declares `yield` in its body but the call site9253+
# doesn't inline (no literal block — those route through9254+
# compile_yield_call_stmt and bypass the C call entirely). The9255+
# callee's C signature is `..., _block, _benv` whenever9256+
# cls_method_has_yield is set; omitting these triggers9257+
# "too few arguments to function call" at clang time.9258+
def cls_call_yargs(ci, midx)9259+
if ci < 0 || midx < 09260+
return ""9261+
end9262+
if cls_method_has_yield(ci, midx) == 19263+
return ", NULL, NULL"9264+
end9265+
""9266+
end9267+
92419268
# ---- Emit top-level methods ----92429269
def emit_toplevel_methods92439270
i = 0@@ -12921,6 +12948,12 @@
1292112948
bp = "0"1292212949
end1292312950
end12951+
# Pad the trailing `_block, _benv` yield-ABI slots when the callee's12952+
# body uses `yield`. The literal-block path (compile_yield_call_stmt)12953+
# bypasses this C call entirely by inlining the body, so reaching12954+
# here means no literal block at the call site — `NULL, NULL` is12955+
# always safe.12956+
yargs = cls_call_yargs(owner_ci, owner_midx)1292412957
# When the call resolves through inheritance to an1292512958
# 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 == 11296713000
tmp = new_temp1296813001
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 runs1297113004
# in the default arm — calling it eagerly before the switch1297213005
# would raise from an abstract stub before any subclass arm@@ -12981,7 +13014,7 @@
1298113014
cand_owner2 = p2[1].to_i1298213015
cand_name = @cls_names[cand_owner2]1298313016
cand_recv = "(sp_" + cand_name + " *)" + self_expr12984-
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 + 11298713020
end@@ -12991,7 +13024,7 @@
1299113024
end1299213025
end1299313026
end12994-
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
end1299613029
# 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
end1797818011
end17979-
tail = build_call_tail(ca, bp)18012+
# Pad the trailing `_block, _benv` yield-ABI slots when the callee's18013+
# body uses `yield`. The literal-block path inlines via18014+
# compile_yield_call_stmt and bypasses this C call entirely, so18015+
# 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 == cname1798118019
return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + tail + ")"1798218020
else@@ -29903,7 +29941,17 @@
2990329941
while emitted.length < @current_method_yield_arity2990429942
emitted.push("0")2990529943
end29906-
emit(" if (_block) _block(" + emitted.join(", ") + ", _benv);")29944+
# When the enclosing method declares `&block`, fall back to invoking the29945+
# captured proc if the yield-ABI slot is empty. This matches Ruby semantics29946+
# (yield and `block.call` drive the same proc) and lets `&block_arg` call29947+
# 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_param29950+
emit(" if (_block) _block(" + emitted.join(", ") + ", _benv);")29951+
emit(" else if (" + bp + ") sp_proc_call(" + bp + ", (mrb_int[]){" + emitted.join(", ") + "});")29952+
else29953+
emit(" if (_block) _block(" + emitted.join(", ") + ", _benv);")29954+
end2990729955
end2990829956
2990929957
def compile_yield_call_stmt(nid, mi)test/block_optional_unused.rbaddedView file
Loading diff…
test/block_optional_unused.rb.expectedaddedView file
Loading diff…