Commit ab824fbd

ab824fbd4cc4Merged

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

Description

fix(parse,codegen): super passes literal block to parent (#665)

super { ... } from a child method previously emitted sp_Parent_test((sp_Parent*)self) which type-checked against (self, _block, _benv) and failed with "too few arguments".

Two parts:

parse: PM_SUPER_NODE and PM_FORWARDING_SUPER_NODE now emit their block field (R "block", n->block). Pre-fix the AST simply dropped it, so the codegen had no signal that a literal block was attached.

codegen: compile_super_expr detects a BlockNode on the super call and routes to a new compile_super_with_block_inline. It mirrors compile_yield_call_expr but reads the inlined body / params / locals from the parent class's @cls_meth_* tables. The block body replaces yield inside the parent body via the standard compile_stmt_with_block walker; a result temp captures the parent body's last expression so the super expression's value still flows through enclosing expressions. ForwardingSuperNode forwards the current method's formals; SuperNode uses its explicit arg list.

The non-block branch additionally pads _block, _benv with NULL/NULL via the existing cls_call_yargs helper, so any other call into a yielding parent (e.g. plain super against a yield-using parent with no block on this call) still type-checks the C signature.

The basic block-only shape from the issue compiles and runs. Block parameters bound to super (e.g. super(x) { |s| ... }) and parent methods that infer params from a non-super-only call site are follow-ups; the as-reported repro now works.

Closes #665.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
ab824fbd4cc4
Parent
6e4ea5584bd1

spinel_codegen.rbmodifiedView file

@@ -12867,6 +12867,22 @@
1286712867 if owner_su == ""
1286812868 return "0"
1286912869 end
12870+ # `super { ... }` carries a literal block. The parent method declares
12871+ # `_block, _benv` in its C signature when its body yields, so a plain
12872+ # `sp_Parent_test(self)` call type-checks too few arguments. Inline
12873+ # the parent body in place and substitute `yield` with the block body
12874+ # — same model the regular literal-block call path uses
12875+ # (compile_yield_call_expr). Issue #665.
12876+ blk_su = @nd_block[nid]
12877+ if blk_su >= 0 && @nd_type[blk_su] == "BlockNode"
12878+ owner_ci_su = find_class_idx(owner_su)
12879+ if owner_ci_su >= 0
12880+ midx_su = cls_find_method_direct(owner_ci_su, mname_su)
12881+ if midx_su >= 0
12882+ return compile_super_with_block_inline(nid, t, owner_ci_su, midx_su, blk_su)
12883+ end
12884+ end
12885+ end
1287012886 cast_self = "(sp_" + owner_su + " *)self"
1287112887 args_c = ""
1287212888 if t == "ForwardingSuperNode"
@@ -12895,7 +12911,159 @@
1289512911 end
1289612912 end
1289712913 end
12898- "sp_" + owner_su + "_" + sanitize_name(mname_su) + "(" + cast_self + args_c + ")"
12914+ # Pad the trailing `_block, _benv` yield-ABI slots when the parent's
12915+ # body yields — the literal-block case is handled above, so on this
12916+ # branch we always pass NULL/NULL for the block slot. Issue #665.
12917+ yargs_su = ""
12918+ owner_ci_su2 = find_class_idx(owner_su)
12919+ if owner_ci_su2 >= 0
12920+ midx_su2 = cls_find_method_direct(owner_ci_su2, mname_su)
12921+ yargs_su = cls_call_yargs(owner_ci_su2, midx_su2)
12922+ end
12923+ "sp_" + owner_su + "_" + sanitize_name(mname_su) + "(" + cast_self + args_c + yargs_su + ")"
12924+ end
12925+
12926+ # `super { ... }` lowering. Mirrors compile_yield_call_expr but reads
12927+ # the inlined body / params / locals from the parent class's method
12928+ # tables (owner_ci, midx) rather than the top-level @meth_* tables.
12929+ # The block body replaces `yield` inside the parent body via the
12930+ # standard compile_stmt_with_block walker; the result temp captures
12931+ # the parent body's last expression so super's value can be
12932+ # propagated through enclosing expressions. Issue #665.
12933+ def compile_super_with_block_inline(nid, t, owner_ci, midx, blk)
12934+ bp_names = "".split(",")
12935+ bp_id = @nd_parameters[blk]
12936+ if bp_id >= 0
12937+ inner_bp = @nd_parameters[bp_id]
12938+ if inner_bp >= 0
12939+ reqs_bp = parse_id_list(@nd_requireds[inner_bp])
12940+ kbp = 0
12941+ while kbp < reqs_bp.length
12942+ bp_names.push(@nd_name[reqs_bp[kbp]])
12943+ kbp = kbp + 1
12944+ end
12945+ end
12946+ end
12947+ arg_ids_sw = []
12948+ if t == "SuperNode"
12949+ args_id_sw = @nd_arguments[nid]
12950+ if args_id_sw >= 0
12951+ arg_ids_sw = get_args(args_id_sw)
12952+ end
12953+ end
12954+ pnames = cls_meth_pnames_get(owner_ci, midx)
12955+ ptypes = cls_meth_ptypes_get(owner_ci, midx)
12956+ @block_counter = @block_counter + 1
12957+ suffix_sw = "_s" + @block_counter.to_s
12958+ map_from_sw = "".split(",")
12959+ map_to_sw = "".split(",")
12960+ cur_params_for_fwd = "".split(",")
12961+ if t == "ForwardingSuperNode"
12962+ cur_params_all = @cls_meth_params[@current_class_idx].split("|")
12963+ midx_cur_sw2 = cls_find_method_direct(@current_class_idx, @current_method_name)
12964+ if midx_cur_sw2 >= 0 && midx_cur_sw2 < cur_params_all.length
12965+ cur_params_for_fwd = cur_params_all[midx_cur_sw2].split(",")
12966+ end
12967+ end
12968+ kp_sw = 0
12969+ while kp_sw < pnames.length
12970+ pt_sw = "int"
12971+ if kp_sw < ptypes.length
12972+ pt_sw = ptypes[kp_sw]
12973+ end
12974+ tname_sw = pnames[kp_sw] + suffix_sw
12975+ val_sw = c_default_val(pt_sw)
12976+ if t == "ForwardingSuperNode"
12977+ if kp_sw < cur_params_for_fwd.length && cur_params_for_fwd[kp_sw] != ""
12978+ val_sw = "lv_" + cur_params_for_fwd[kp_sw]
12979+ end
12980+ else
12981+ if kp_sw < arg_ids_sw.length
12982+ val_sw = compile_expr(arg_ids_sw[kp_sw])
12983+ arg_t_sw = infer_type(arg_ids_sw[kp_sw])
12984+ if base_type(pt_sw) == "bigint" && arg_t_sw == "int"
12985+ @needs_bigint = 1
12986+ val_sw = "sp_bigint_new_int(" + val_sw + ")"
12987+ elsif base_type(pt_sw) == "int" && arg_t_sw == "bigint"
12988+ @needs_bigint = 1
12989+ val_sw = "sp_bigint_to_int((sp_Bigint *)" + val_sw + ")"
12990+ end
12991+ end
12992+ end
12993+ emit(" " + c_type(pt_sw) + " lv_" + tname_sw + " = " + val_sw + ";")
12994+ map_from_sw.push(pnames[kp_sw])
12995+ map_to_sw.push(tname_sw)
12996+ declare_var(tname_sw, pt_sw)
12997+ kp_sw = kp_sw + 1
12998+ end
12999+ bid_sw = -1
13000+ bodies_sw = @cls_meth_bodies[owner_ci].split(";")
13001+ if midx < bodies_sw.length
13002+ bid_sw = bodies_sw[midx].to_i
13003+ end
13004+ if bid_sw >= 0
13005+ flocals_n_sw = "".split(",")
13006+ flocals_t_sw = "".split(",")
13007+ sn_sw = @nd_scope_names[bid_sw]
13008+ if sn_sw != ""
13009+ flocals_n_sw = sn_sw.split("|")
13010+ flocals_t_sw = @nd_scope_types[bid_sw].split("|")
13011+ end
13012+ kf_sw = 0
13013+ while kf_sw < flocals_n_sw.length
13014+ tname_lv = flocals_n_sw[kf_sw] + suffix_sw
13015+ emit(" " + c_type(flocals_t_sw[kf_sw]) + " lv_" + tname_lv + " = " + c_default_val(flocals_t_sw[kf_sw]) + ";")
13016+ map_from_sw.push(flocals_n_sw[kf_sw])
13017+ map_to_sw.push(tname_lv)
13018+ declare_var(tname_lv, flocals_t_sw[kf_sw])
13019+ declare_var(flocals_n_sw[kf_sw], flocals_t_sw[kf_sw])
13020+ kf_sw = kf_sw + 1
13021+ end
13022+ end
13023+ rt_sw = "int"
13024+ returns_sw = @cls_meth_returns[owner_ci].split(";")
13025+ if midx < returns_sw.length
13026+ rt_sw = returns_sw[midx]
13027+ end
13028+ result_tmp_sw = new_temp
13029+ emit(" " + c_type(rt_sw) + " " + result_tmp_sw + " = " + c_return_default(rt_sw) + ";")
13030+ saved_rmf_sw = @inline_rename_map_from
13031+ saved_rmt_sw = @inline_rename_map_to
13032+ @inline_rename_map_from = map_from_sw
13033+ @inline_rename_map_to = map_to_sw
13034+ if bid_sw >= 0
13035+ stmts_sw = get_stmts(bid_sw)
13036+ ns_sw = stmts_sw.length
13037+ ks_sw = 0
13038+ while ks_sw < ns_sw - 1
13039+ compile_stmt_with_block(stmts_sw[ks_sw], blk, bp_names, map_from_sw, map_to_sw)
13040+ ks_sw = ks_sw + 1
13041+ end
13042+ if ns_sw > 0
13043+ last_sw = stmts_sw.last
13044+ last_ty_sw = @nd_type[last_sw]
13045+ if last_ty_sw == "YieldNode" || last_ty_sw == "IfNode" || last_ty_sw == "CaseNode" ||
13046+ last_ty_sw == "WhileNode" || last_ty_sw == "BeginNode" || last_ty_sw == "ReturnNode" ||
13047+ last_ty_sw == "LocalVariableWriteNode" || last_ty_sw == "InstanceVariableWriteNode" ||
13048+ last_ty_sw == "ClassVariableWriteNode" || last_ty_sw == "GlobalVariableWriteNode"
13049+ compile_stmt_with_block(last_sw, blk, bp_names, map_from_sw, map_to_sw)
13050+ else
13051+ last_val_sw = compile_expr_remap(last_sw, map_from_sw, map_to_sw)
13052+ last_t_sw = infer_type(last_sw)
13053+ if base_type(rt_sw) == "bigint" && last_t_sw == "int"
13054+ @needs_bigint = 1
13055+ last_val_sw = "sp_bigint_new_int(" + last_val_sw + ")"
13056+ elsif base_type(rt_sw) == "int" && last_t_sw == "bigint"
13057+ @needs_bigint = 1
13058+ last_val_sw = "sp_bigint_to_int((sp_Bigint *)" + last_val_sw + ")"
13059+ end
13060+ emit(" " + result_tmp_sw + " = " + last_val_sw + ";")
13061+ end
13062+ end
13063+ end
13064+ @inline_rename_map_from = saved_rmf_sw
13065+ @inline_rename_map_to = saved_rmt_sw
13066+ result_tmp_sw
1289913067 end
1290013068
1290113069 def c_string_literal(s)