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:

  1. compile_expr's BeginNode arm (was absent). puts (begin; X; rescue Y; Z; end) previously fell through to the compile_expr default and emitted 0 -- 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.
  2. emit_raise_one_arg(raise ClassName): was emitting sp_raise("ClassName"), but sp_raise hardcodes the class to "RuntimeError" and passes its arg as the message. So rescue ClassName didn't match (caught a RuntimeError-with- that-message-string instead). Route ConstantReadNode raises through sp_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

@@ -1319,7 +1319,18 @@
13191319 # Used by both the stmt-form and expr-form raise arms.
13201320 def emit_raise_one_arg(arg_id)
13211321 if @nd_type[arg_id] == "ConstantReadNode"
1322- emit(" sp_raise(\"" + @nd_name[arg_id] + "\");")
1322+ # `raise ArgumentError` -- raise the named class with no message.
1323+ # sp_raise(msg) hardcodes the class to RuntimeError, so passing
1324+ # "ArgumentError" as `msg` raises a RuntimeError-with-message,
1325+ # which `rescue ArgumentError` doesn't catch. Route through
1326+ # sp_raise_cls(class, msg) so the class survives. Issue #704.
1327+ cname_cr = @nd_name[arg_id]
1328+ if is_exception_class_name(cname_cr) == 1
1329+ @needs_exc_class_hierarchy = 1
1330+ emit(" sp_raise_cls(\"" + cname_cr + "\", " + exc_default_msg_expr(cname_cr) + ");")
1331+ return
1332+ end
1333+ emit(" sp_raise(\"" + cname_cr + "\");")
13231334 return
13241335 end
13251336 # `raise <BuiltinExc>.new(<msg>)` -- ConstantReadNode is the exc
@@ -13368,6 +13379,61 @@
1336813379 end
1336913380 return "0"
1337013381 end
13382+ if t == "BeginNode"
13383+ # `puts (begin ... rescue ... end)` -- begin/rescue used as
13384+ # an expression. Same return-capture pattern as
13385+ # compile_body_return_inner's BeginNode arm. Issue #704.
13386+ rt_bn = infer_type(nid)
13387+ rc_id_bn = @nd_rescue_clause[nid]
13388+ ec_id_bn = @nd_ensure_clause[nid]
13389+ if rc_id_bn < 0 && ec_id_bn < 0
13390+ # Bare `begin ... end` with no rescue / ensure -- evaluate the
13391+ # body's last expression. compile_body_into emits the body and
13392+ # captures the last expr into ret_tmp.
13393+ ret_tmp_bn = new_temp
13394+ emit(" " + c_type(rt_bn) + " " + ret_tmp_bn + " = " + c_default_val(rt_bn) + ";")
13395+ compile_body_into(@nd_body[nid], ret_tmp_bn, rt_bn)
13396+ return ret_tmp_bn
13397+ end
13398+ if rc_id_bn >= 0 && body_has_retry(@nd_body[rc_id_bn]) == 0
13399+ # Single-setjmp rescue: capture begin body's last expression OR
13400+ # rescue arm's last expression into ret_tmp. When ensure is
13401+ # present, run the ensure body for side effects after the
13402+ # rescue chain -- its value is discarded per Ruby semantics.
13403+ @needs_setjmp = 1
13404+ ret_tmp_bn = new_temp
13405+ emit(" " + c_type(rt_bn) + " " + ret_tmp_bn + " = " + c_default_val(rt_bn) + ";")
13406+ emit(" sp_exc_top++;")
13407+ emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {")
13408+ @indent = @indent + 1
13409+ else_id_bn = @nd_else_clause[nid]
13410+ if else_id_bn >= 0
13411+ compile_stmts_body(@nd_body[nid])
13412+ else_body_bn = @nd_body[else_id_bn]
13413+ if else_body_bn >= 0
13414+ compile_body_into(else_body_bn, ret_tmp_bn, rt_bn)
13415+ end
13416+ else
13417+ compile_body_into(@nd_body[nid], ret_tmp_bn, rt_bn)
13418+ end
13419+ emit(" sp_exc_top--;")
13420+ @indent = @indent - 1
13421+ emit(" } else {")
13422+ @indent = @indent + 1
13423+ emit(" sp_exc_top--;")
13424+ compile_rescue_chain_into(rc_id_bn, ret_tmp_bn, rt_bn)
13425+ @indent = @indent - 1
13426+ emit(" }")
13427+ if ec_id_bn >= 0
13428+ # ensure body's value is discarded; emit as side-effecting stmts.
13429+ compile_stmts_body(@nd_body[ec_id_bn])
13430+ end
13431+ return ret_tmp_bn
13432+ end
13433+ # retry shapes: fall back to stmt form (no capture).
13434+ compile_begin_stmt(nid)
13435+ return c_default_val(rt_bn)
13436+ end
1337113437 if t == "CaseNode"
1337213438 # Case as expression: use a temp var and compile each branch as assignment
1337313439 rt = infer_type(nid)