Commit 7ea954bb
7ea954bb52d8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): begin/rescue as expression + raise ConstantReadNode
Two coordinated #704 fixes:
-
compile_expr's BeginNode arm (was absent).
puts (begin; X; rescue Y; Z; end)previously fell through to the compile_expr default and emitted0-- no body emit, no rescue handling. The new arm mirrors compile_body_return_inner's BeginNode arm:- Bare
begin..end(no rescue / ensure): use compile_body_into to evaluate the body into ret_tmp. - begin..rescue (with optional else / ensure, no retry): emit a single setjmp wrapper, run the body (or the else body when present) into ret_tmp on the success path, and route the rescue chain through compile_rescue_chain_into on the exception path. ensure body's side effects emit after the rescue chain; its value is discarded per Ruby semantics.
- retry shapes: fall back to compile_begin_stmt + default return temp value.
- Bare
-
emit_raise_one_arg(
raise ClassName): was emittingsp_raise("ClassName"), but sp_raise hardcodes the class to "RuntimeError" and passes its arg as the message. Sorescue ClassNamedidn't match (caught a RuntimeError-with- that-message-string instead). Route ConstantReadNode raises throughsp_raise_cls("ClassName", default_msg)when the constant names a known exception class, mirroring the .new path's handling.
Test: begin_rescue_as_expression.rb. 451 -> 453 tests pass. optcarrot 59662 holds.
Fixes #704
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 7ea954bb52d8
- Parent
- 7980373f777b
spinel_codegen.rbmodifiedView file
# Used by both the stmt-form and expr-form raise arms. def emit_raise_one_arg(arg_id) if @nd_type[arg_id] == "ConstantReadNode" emit(" sp_raise(\"" + @nd_name[arg_id] + "\");") # `raise ArgumentError` -- raise the named class with no message. # sp_raise(msg) hardcodes the class to RuntimeError, so passing # "ArgumentError" as `msg` raises a RuntimeError-with-message, # which `rescue ArgumentError` doesn't catch. Route through # sp_raise_cls(class, msg) so the class survives. Issue #704. cname_cr = @nd_name[arg_id] if is_exception_class_name(cname_cr) == 1 @needs_exc_class_hierarchy = 1 emit(" sp_raise_cls(\"" + cname_cr + "\", " + exc_default_msg_expr(cname_cr) + ");") return end emit(" sp_raise(\"" + cname_cr + "\");") return end # `raise <BuiltinExc>.new(<msg>)` -- ConstantReadNode is the exc end return "0" end if t == "BeginNode" # `puts (begin ... rescue ... end)` -- begin/rescue used as # an expression. Same return-capture pattern as # compile_body_return_inner's BeginNode arm. Issue #704. rt_bn = infer_type(nid) rc_id_bn = @nd_rescue_clause[nid] ec_id_bn = @nd_ensure_clause[nid] if rc_id_bn < 0 && ec_id_bn < 0 # Bare `begin ... end` with no rescue / ensure -- evaluate the # body's last expression. compile_body_into emits the body and # captures the last expr into ret_tmp. ret_tmp_bn = new_temp emit(" " + c_type(rt_bn) + " " + ret_tmp_bn + " = " + c_default_val(rt_bn) + ";") compile_body_into(@nd_body[nid], ret_tmp_bn, rt_bn) return ret_tmp_bn end if rc_id_bn >= 0 && body_has_retry(@nd_body[rc_id_bn]) == 0 # Single-setjmp rescue: capture begin body's last expression OR # rescue arm's last expression into ret_tmp. When ensure is # present, run the ensure body for side effects after the # rescue chain -- its value is discarded per Ruby semantics. @needs_setjmp = 1 ret_tmp_bn = new_temp emit(" " + c_type(rt_bn) + " " + ret_tmp_bn + " = " + c_default_val(rt_bn) + ";") emit(" sp_exc_top++;") emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {") @indent = @indent + 1 else_id_bn = @nd_else_clause[nid] if else_id_bn >= 0 compile_stmts_body(@nd_body[nid]) else_body_bn = @nd_body[else_id_bn] if else_body_bn >= 0 compile_body_into(else_body_bn, ret_tmp_bn, rt_bn) end else compile_body_into(@nd_body[nid], ret_tmp_bn, rt_bn) end emit(" sp_exc_top--;") @indent = @indent - 1 emit(" } else {") @indent = @indent + 1 emit(" sp_exc_top--;") compile_rescue_chain_into(rc_id_bn, ret_tmp_bn, rt_bn) @indent = @indent - 1 emit(" }") if ec_id_bn >= 0 # ensure body's value is discarded; emit as side-effecting stmts. compile_stmts_body(@nd_body[ec_id_bn]) end return ret_tmp_bn end # retry shapes: fall back to stmt form (no capture). compile_begin_stmt(nid) return c_default_val(rt_bn) end if t == "CaseNode" # Case as expression: use a temp var and compile each branch as assignment rt = infer_type(nid)test/begin_rescue_as_expression.expectedaddedView file
Loading diff…
test/begin_rescue_as_expression.rbaddedView file
Loading diff…