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:
-
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.
-
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
emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {") @indent = @indent + 1 compile_stmts_body(@nd_body[nid]) # `begin..rescue..else..end`: the else body runs only when the # body completed without exception. Place it inside the # setjmp-success branch, after the body. The exception path # (`else { ... rescue ... }` below) doesn't run else. Phase 1B # of the exception handling gap fixes. else_id_b = @nd_else_clause[nid] if else_id_b >= 0 else_body_b = @nd_body[else_id_b] if else_body_b >= 0 compile_stmts_body(else_body_b) end end @indent = @indent - 1 emit(" sp_exc_top--;") if has_retry == 1 emit(" sp_exc_top++;") emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {") @indent = @indent + 1 compile_body_into(@nd_body[last], ret_tmp, return_type) # Method-level begin..rescue..else: when else is present, the # begin body's value is discarded -- Ruby semantics says the # else body's last expression becomes the method's return. # Run begin body as a void-shaped block, then else body with # ret_tmp capture. Phase 1B of the exception handling gap fixes. else_id_bri = @nd_else_clause[last] if else_id_bri >= 0 compile_stmts_body(@nd_body[last]) else_body_bri = @nd_body[else_id_bri] if else_body_bri >= 0 compile_body_into(else_body_bri, ret_tmp, return_type) end else compile_body_into(@nd_body[last], ret_tmp, return_type) end emit(" sp_exc_top--;") @indent = @indent - 1 emit(" } else {")test/begin_rescue_else_clause.rbaddedView file
Loading diff…
test/begin_rescue_else_clause.rb.expectedaddedView file
Loading diff…