Commit 59e86e74

59e86e74ebb5Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

codegen: fix ensure+exception re-raise using saved class/message

When a begin..ensure has no rescue, the unhandled exception must be re-raised after the ensure body runs. Previously the re-raise used sp_exc_msg[sp_exc_top], but sp_exc_top had already been decremented twice by then (once in the else handler, once for propagation), causing an off-by-one index into the message array.

Fix: save sp_exc_msg/sp_exc_cls immediately after sp_exc_top-- in the else handler (while the index is still valid), then use those saved pointers for the eventual sp_raise_cls call. Also propagate the saved class+message upward when nested ensures exist.

Fixes bundle_class_07 (ensure_runs_on_raise_nested): 435 -> 437 pass.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
59e86e74ebb5
Parent
e888148a7374

src/codegen.cmodifiedView file

@@ -6270,6 +6270,12 @@
62706270 int eid = ++g_tmp;
62716271 int has_retval = (g_ret_type != TY_VOID && g_ret_type != TY_UNKNOWN);
62726272 emit_indent(b, indent); buf_printf(b, "int _retf%d = 0;\n", eid);
6273+ /* _excf/_excmsg/_exccls track an unhandled exception (no rescue) so
6274+ that ensure can re-raise it after running. Saved immediately after
6275+ sp_exc_top-- while the index is still valid. */
6276+ emit_indent(b, indent); buf_printf(b, "int _excf%d = 0;\n", eid);
6277+ emit_indent(b, indent); buf_printf(b, "const char *_excmsg%d = NULL;\n", eid);
6278+ emit_indent(b, indent); buf_printf(b, "const char *_exccls%d = NULL;\n", eid);
62736279 if (has_retval) {
62746280 emit_indent(b, indent); emit_ctype(c, g_ret_type, b);
62756281 buf_printf(b, " _retv%d = %s;\n", eid, default_value(g_ret_type));
@@ -6298,7 +6304,16 @@
62986304 emit_indent(b, indent); buf_puts(b, "}\n");
62996305 emit_indent(b, indent); buf_puts(b, "else {\n");
63006306 emit_indent(b, indent + 1); buf_puts(b, "sp_exc_top--;\n");
6301- if (rescue >= 0) emit_rescue(c, rescue, b, indent + 1, fr, resultvar);
6307+ if (rescue >= 0) {
6308+ emit_rescue(c, rescue, b, indent + 1, fr, resultvar);
6309+ }
6310+ else {
6311+ /* No rescue: save exception info for re-raise after ensure runs.
6312+ sp_exc_top has just been decremented so sp_exc_top is the right index. */
6313+ emit_indent(b, indent + 1);
6314+ buf_printf(b, "_excf%d = 1; _excmsg%d = sp_exc_msg[sp_exc_top]; _exccls%d = sp_exc_cls[sp_exc_top];\n",
6315+ eid, eid, eid);
6316+ }
63026317 emit_indent(b, indent); buf_puts(b, "}\n");
63036318
63046319 g_ensure_depth--;
@@ -6318,12 +6333,19 @@
63186333 buf_printf(b, "if (_retf%d) { _retf%d = 1; sp_exc_top--; goto _ensure%d; }\n",
63196334 eid, outer->lid, outer->lid);
63206335 }
6336+ /* Unhandled exception: propagate info to outer ensure context. */
6337+ emit_indent(b, indent);
6338+ buf_printf(b, "if (_excf%d) { _excf%d = 1; _excmsg%d = _excmsg%d; _exccls%d = _exccls%d; sp_exc_top--; goto _ensure%d; }\n",
6339+ eid, outer->lid, outer->lid, eid, outer->lid, eid, outer->lid);
63216340 }
63226341 else {
63236342 if (has_retval) buf_printf(b, "if (_retf%d) return _retv%d;\n", eid, eid);
63246343 else if (g_ret_type == TY_POLY) buf_printf(b, "if (_retf%d) return sp_box_nil();\n", eid);
63256344 else if (g_ret_type == TY_UNKNOWN) buf_printf(b, "if (_retf%d) return 0;\n", eid); /* main() */
63266345 else buf_printf(b, "if (_retf%d) return;\n", eid);
6346+ /* Unhandled exception: re-raise using the saved class/message. */
6347+ emit_indent(b, indent);
6348+ buf_printf(b, "if (_excf%d) sp_raise_cls(_exccls%d, _excmsg%d);\n", eid, eid, eid);
63276349 }
63286350 return;
63296351 }
@@ -6828,6 +6850,10 @@
68286850 emit_indent(b, indent); emit_tail_lead(b); buf_printf(b, "_t%d;\n", t);
68296851 return;
68306852 }
6853+ /* Non-scalar (e.g. TY_VOID when body diverges with raise or return):
6854+ emit as a plain statement; any `return` inside uses deferred mechanism. */
6855+ emit_begin(c, id, b, indent, NULL);
6856+ return;
68316857 }
68326858
68336859 /* statements that don't produce a usable tail value: emit normally;