Commit c4d8870b

c4d8870b663aMerged

Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): method-level rescue propagates last expr as return value

Methods of the form def f; X; rescue; Y; end previously returned the function's default value (0 / NULL / etc.) regardless of which branch ran. The rescue body's value was simply dropped — the existing compile_body_return arm for BeginNode called compile_begin_stmt and then emitted return c_return_default(...).

This patch adds a return-value-aware path. When the method body ends in a BeginNode with a rescue clause, no retry, and no ensure:

  • Allocate a return-typed slot.
  • Wrap the begin body in setjmp; in the success branch, capture the body's last expression into the slot (compile_body_into).
  • In the rescue branch, walk the rescue chain, capturing each matching body's last expression into the slot (compile_rescue_chain_into).
  • Return the slot.

compile_body_into and compile_rescue_chain_into are mirrors of compile_stmts_body and compile_rescue_chain that emit the last statement as an expression assignment. They handle the special case where the last stmt is an IO-shaped intrinsic (puts, print, p, pp, printf, warn) that has no expression form in spinel codegen — those are compiled as stmts and the slot keeps its default. Other no-effect last shapes (ReturnNode, WhileNode, UntilNode, BreakNode, NextNode) defer to compile_stmt similarly.

Retry-bearing rescues fall back to the legacy stmt form (drops the value) — supporting retry with return-value capture would require restructuring the for-loop emit pattern. Left for a follow-up; rare shape in practice.

Ensure-bearing rescues also fall back to the legacy stmt form. compile_begin_stmt's 2-setjmp pattern correctly replays the ensure body on both the explicit-return path (via @ensure_stack) and the propagation path; the single-setjmp capture form here would skip ensure on explicit return out of the rescue body.

New test: test/method_level_rescue_return.rb covers bare rescue fallback, typed rescue with ArgumentError, and a multi-stmt rescue body (last stmt is the return value).

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

Details

Commit
c4d8870b663a
Parent
1d5291554707

spinel_codegen.rbmodifiedView file

@@ -30410,6 +30410,110 @@
3041030410 end
3041130411 end
3041230412
30413+ # Helper for method-level begin/rescue: compiles the statements of a
30414+ # body, emitting all-but-last as stmts and assigning the last expr's
30415+ # value into ret_tmp. Used so that `def f; X; rescue; Y; end` returns
30416+ # X's value on the success path and Y's value on the rescue path.
30417+ def compile_body_into(body_id, ret_tmp, return_type)
30418+ if body_id < 0
30419+ return
30420+ end
30421+ stmts = get_stmts(body_id)
30422+ if stmts.length == 0
30423+ return
30424+ end
30425+ i = 0
30426+ while i < stmts.length - 1
30427+ compile_stmt(stmts[i])
30428+ i = i + 1
30429+ end
30430+ last = stmts.last
30431+ lt = @nd_type[last]
30432+ if lt == "ReturnNode" || lt == "WhileNode" || lt == "UntilNode" || lt == "BreakNode" || lt == "NextNode"
30433+ compile_stmt(last)
30434+ return
30435+ end
30436+ if return_type == "void"
30437+ compile_stmt(last)
30438+ return
30439+ end
30440+ # IO-shaped intrinsics (puts/print/p/printf/warn/pp) have no
30441+ # expression form in spinel codegen — compile_call_expr would
30442+ # warn "cannot resolve" and return the literal "0", silently
30443+ # dropping the side effect. Force statement form for these.
30444+ if lt == "CallNode" && @nd_receiver[last] < 0
30445+ cn = @nd_name[last]
30446+ if cn == "puts" || cn == "print" || cn == "p" || cn == "pp" || cn == "printf" || cn == "warn"
30447+ compile_stmt(last)
30448+ return
30449+ end
30450+ end
30451+ expr = compile_expr(last)
30452+ emit(" " + ret_tmp + " = " + expr + ";")
30453+ end
30454+
30455+ # Mirror of compile_rescue_chain that captures the rescue body's last
30456+ # expression into ret_tmp instead of dropping it. Used by the
30457+ # method-level BeginNode arm in compile_body_return.
30458+ def compile_rescue_chain_into(rc, ret_tmp, return_type)
30459+ exc_types = parse_id_list(@nd_exceptions[rc])
30460+ ref = @nd_reference[rc]
30461+ # Snapshot the in-flight exception BEFORE the type-check `if` so
30462+ # the else-branch (re-raise / propagation) can also reference it.
30463+ @rescue_depth = @rescue_depth + 1
30464+ saved_cls = "_rescue_cls_" + @rescue_depth.to_s
30465+ saved_msg = "_rescue_msg_" + @rescue_depth.to_s
30466+ emit(" const char *" + saved_cls + " = (const char *)sp_last_exc_cls;")
30467+ emit(" const char *" + saved_msg + " = sp_exc_msg[sp_exc_top];")
30468+ @rescue_cls_stack.push(saved_cls)
30469+ @rescue_msg_stack.push(saved_msg)
30470+ has_type_check = 0
30471+ if exc_types.length > 0
30472+ has_type_check = 1
30473+ cond = ""
30474+ k = 0
30475+ while k < exc_types.length
30476+ if k > 0
30477+ cond = cond + " || "
30478+ end
30479+ cond = cond + "sp_exc_is_a((const char*)sp_last_exc_cls, \"" + @nd_name[exc_types[k]] + "\")"
30480+ k = k + 1
30481+ end
30482+ emit(" if (" + cond + ") {")
30483+ @indent = @indent + 1
30484+ end
30485+ bound_rname = ""
30486+ if ref >= 0
30487+ bound_rname = @nd_name[ref]
30488+ emit(" lv_" + bound_rname + " = " + saved_msg + ";")
30489+ @exc_var_names.push(bound_rname)
30490+ @exc_var_cls_vars.push(saved_cls)
30491+ end
30492+ compile_body_into(@nd_body[rc], ret_tmp, return_type)
30493+ if bound_rname != ""
30494+ @exc_var_names.pop
30495+ @exc_var_cls_vars.pop
30496+ end
30497+ @rescue_cls_stack.pop
30498+ @rescue_msg_stack.pop
30499+ @rescue_depth = @rescue_depth - 1
30500+ if has_type_check == 1
30501+ @indent = @indent - 1
30502+ sub = @nd_subsequent[rc]
30503+ if sub >= 0
30504+ emit(" } else {")
30505+ @indent = @indent + 1
30506+ compile_rescue_chain_into(sub, ret_tmp, return_type)
30507+ @indent = @indent - 1
30508+ else
30509+ emit(" } else {")
30510+ emit(" sp_raise_cls(" + saved_cls + ", " + saved_msg + ");")
30511+ end
30512+ emit(" }")
30513+ end
30514+ 0
30515+ end
30516+
3041330517 def compile_body_return(body_id, return_type)
3041430518 if body_id < 0
3041530519 if return_type != "void"
@@ -30462,9 +30566,53 @@
3046230566 return
3046330567 end
3046430568 if @nd_type[last] == "BeginNode"
30465- compile_begin_stmt(last)
30569+ rc_id = @nd_rescue_clause[last]
30570+ ec_id = @nd_ensure_clause[last]
30571+ has_rescue_local = 0
30572+ if rc_id >= 0
30573+ has_rescue_local = 1
30574+ end
30575+ has_retry_local = 0
30576+ if has_rescue_local == 1 && body_has_retry(@nd_body[rc_id]) == 1
30577+ has_retry_local = 1
30578+ end
30579+ if has_rescue_local == 0 || has_retry_local == 1 || ec_id >= 0
30580+ # No rescue / has retry / has ensure: fall back to legacy stmt-form.
30581+ # compile_begin_stmt's 2-setjmp ensure pattern correctly replays the
30582+ # ensure body on both the explicit-return path and the propagation
30583+ # path; the return-value-capture form below uses a single setjmp and
30584+ # would skip ensure on explicit returns out of the rescue body.
30585+ compile_begin_stmt(last)
30586+ if return_type != "void"
30587+ emit(" return " + c_return_default(return_type) + ";")
30588+ end
30589+ return
30590+ end
30591+ # Method-level begin/rescue with return-value capture: bind the
30592+ # last expression of the begin body and of each rescue body into
30593+ # a return slot, then return the slot after the optional ensure.
30594+ @needs_setjmp = 1
30595+ ret_tmp = new_temp
30596+ if return_type != "void"
30597+ emit(" " + c_type(return_type) + " " + ret_tmp + " = " + c_default_val(return_type) + ";")
30598+ end
30599+ emit(" sp_exc_top++;")
30600+ emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {")
30601+ @indent = @indent + 1
30602+ compile_body_into(@nd_body[last], ret_tmp, return_type)
30603+ emit(" sp_exc_top--;")
30604+ @indent = @indent - 1
30605+ emit(" } else {")
30606+ @indent = @indent + 1
30607+ emit(" sp_exc_top--;")
30608+ compile_rescue_chain_into(rc_id, ret_tmp, return_type)
30609+ @indent = @indent - 1
30610+ emit(" }")
30611+ if ec_id >= 0
30612+ compile_stmts_body(@nd_body[ec_id])
30613+ end
3046630614 if return_type != "void"
30467- emit(" return " + c_return_default(return_type) + ";")
30615+ emit(" return " + ret_tmp + ";")
3046830616 end
3046930617 return
3047030618 end