Commit f9754211
f9754211e765Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #1406 from ryanseys/rs-proc-recursive-cell
fix(codegen): assign self-recursive Proc bindings through the int cell
Details
- Commit
- f9754211e765
- Parent
- d7ac9a9f0fff
- Merge parent
- b85092e4c50f
src/codegen_stmt.cmodifiedView file
@@ -401,6 +401,23 @@
401401
int v = nt_ref(c->nt, id, "value");402402
LocalVar *lv = scope_local(comp_scope_of(c, id), nm);403403
emit_indent(b, indent);404+
/* A TY_PROC value lives in an int cell as (mrb_int)(uintptr_t)sp_Proc*. The405+
write target must be the raw cell deref (an lvalue) with the pointer406+
re-encoded as int; emit_local_ref's read form casts to sp_Proc* and is not407+
assignable (self-recursive `f = proc { f.call(...) }`). */408+
if (lv && lv->type == TY_PROC &&409+
(lv->is_cell || (g_cap_struct && g_cap_names && nameset_has(g_cap_names, nm)))) {410+
if (g_cap_struct && g_cap_names && nameset_has(g_cap_names, nm))411+
buf_printf(b, "*((%s *)_cap)->%s", g_cap_struct, nm);412+
else413+
buf_printf(b, "*_cell_%s", nm);414+
buf_puts(b, " = (mrb_int)(uintptr_t)(");415+
const char *pvty = nt_type(c->nt, v);416+
if (pvty && !strcmp(pvty, "NilNode")) buf_puts(b, "NULL");417+
else emit_expr(c, v, b);418+
buf_puts(b, ");\n");419+
return;420+
}404421
emit_local_ref(c, id, nm, b);405422
buf_puts(b, " = ");406423
/* `x = nil` -> the variable's type-appropriate default */test/proc_recursive.rbaddedView file
Loading diff…
test/proc_recursive.rb.expectedaddedView file
Loading diff…