Commit ea3023ea
ea3023ea299dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
codegen: declare rescue-bind vars in proc/lambda bodies
Virtual &block params have TY_UNKNOWN type; rescue-bind variables
like rescue => e have TY_EXCEPTION. Previously both were excluded
from proc-body local declarations via the is_block_param flag, so
lv_e was undeclared inside _proc_N functions.
Switch the guard from !is_block_param to type != TY_UNKNOWN to allow TY_EXCEPTION locals while still skipping virtual block slots.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- ea3023ea299d
- Parent
- ff32fc295cc5
src/codegen.cmodifiedView file
@@ -5110,6 +5110,18 @@
51105110
return;51115111
}51125112
if (!strcmp(ty, "LocalVariableReadNode")) { emit_local_ref(c, id, nt_str(nt, id, "name"), b); return; }5113+
if (!strcmp(ty, "LocalVariableWriteNode")) {5114+
/* assignment used as expression: ({ lv = rhs; lv; }) */5115+
const char *nm = nt_str(nt, id, "name");5116+
int v = nt_ref(nt, id, "value");5117+
LocalVar *lv = scope_local(comp_scope_of(c, id), nm);5118+
buf_puts(b, "({ ");5119+
emit_local_ref(c, id, nm, b); buf_puts(b, " = ");5120+
if (lv && lv->type == TY_POLY && comp_ntype(c, v) != TY_POLY) emit_boxed(c, v, b);5121+
else emit_expr(c, v, b);5122+
buf_puts(b, "; "); emit_local_ref(c, id, nm, b); buf_puts(b, "; })");5123+
return;5124+
}51135125
if (!strcmp(ty, "YieldNode")) {51145126
if (g_block_id < 0) { buf_puts(b, "SP_INT_NIL"); return; } /* no block: yield is nil */51155127
emit_block_invoke(c, nt_ref(nt, id, "arguments"), b, 0, 1);@@ -7158,7 +7170,8 @@
71587170
for (int i = 0; i < locals.n; i++) {71597171
LocalVar *lv = scope_local(bs, locals.v[i]);71607172
/* a celled local is a captured var (accessed via _cap), not a fn-local */7161-
if (lv && !lv->is_block_param && !lv->is_cell) declare_local(c, pb, lv, 0);7173+
/* skip virtual &block slots (TY_UNKNOWN) but allow rescue-bind vars (TY_EXCEPTION) */7174+
if (lv && lv->type != TY_UNKNOWN && !lv->is_cell) declare_local(c, pb, lv, 0);71627175
}71637176
if (ret_ptr) {71647177
/* launder a heap-pointer return through the mrb_int slot: emit the body's