Commit 2547ef71

2547ef71d9a9Merged

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

Description

fix(codegen): implement begin..rescue..else..end (#Phase 1B)

The else clause runs only when the begin body completed without exception. Pre-fix the parser emitted else_clause but the codegen ignored it -- else body was silently dropped.

Two emit paths needed updating:

  1. compile_begin_stmt (statement form): after the body's stmts compile inside the setjmp-success branch, run the else body too. Exception path stays unchanged.

  2. compile_body_return_inner's method-level BeginNode arm: when else is present, the begin body's value is discarded -- Ruby semantics says else's last expression becomes the method's return value. Run begin body as a void-shaped block (no ret_tmp capture), then else body with ret_tmp capture. When else is absent, keep the existing single-capture form.

Regression: test/begin_rescue_else_clause.rb covers:

  • else with no exception (else runs)
  • else with exception (else skipped, rescue runs)
  • else + ensure ordering
  • method-level def with begin..rescue..else (success / exception paths)

Default tests 448/0/0, promote 447/0/0, optcarrot 59662 unchanged.

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

Details

Commit
2547ef71d9a9
Parent
296472ce2cf3

spinel_codegen.rbmodifiedView file

@@ -37161,6 +37161,18 @@
3716137161 emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {")
3716237162 @indent = @indent + 1
3716337163 compile_stmts_body(@nd_body[nid])
37164+ # `begin..rescue..else..end`: the else body runs only when the
37165+ # body completed without exception. Place it inside the
37166+ # setjmp-success branch, after the body. The exception path
37167+ # (`else { ... rescue ... }` below) doesn't run else. Phase 1B
37168+ # of the exception handling gap fixes.
37169+ else_id_b = @nd_else_clause[nid]
37170+ if else_id_b >= 0
37171+ else_body_b = @nd_body[else_id_b]
37172+ if else_body_b >= 0
37173+ compile_stmts_body(else_body_b)
37174+ end
37175+ end
3716437176 @indent = @indent - 1
3716537177 emit(" sp_exc_top--;")
3716637178 if has_retry == 1
@@ -39162,7 +39174,21 @@
3916239174 emit(" sp_exc_top++;")
3916339175 emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {")
3916439176 @indent = @indent + 1
39165- compile_body_into(@nd_body[last], ret_tmp, return_type)
39177+ # Method-level begin..rescue..else: when else is present, the
39178+ # begin body's value is discarded -- Ruby semantics says the
39179+ # else body's last expression becomes the method's return.
39180+ # Run begin body as a void-shaped block, then else body with
39181+ # ret_tmp capture. Phase 1B of the exception handling gap fixes.
39182+ else_id_bri = @nd_else_clause[last]
39183+ if else_id_bri >= 0
39184+ compile_stmts_body(@nd_body[last])
39185+ else_body_bri = @nd_body[else_id_bri]
39186+ if else_body_bri >= 0
39187+ compile_body_into(else_body_bri, ret_tmp, return_type)
39188+ end
39189+ else
39190+ compile_body_into(@nd_body[last], ret_tmp, return_type)
39191+ end
3916639192 emit(" sp_exc_top--;")
3916739193 @indent = @indent - 1
3916839194 emit(" } else {")