Commit 2772baa0
2772baa049d2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
codegen: bind a value-type receiver by value in instance_exec trampolines
An instance_exec trampoline (def m(&b); instance_exec(args, &b); end) inlines
at the call site by rebinding self to a temp holding the receiver. The splice
always declared that temp as a pointer (sp_X *_t = <recv>), which is a type
error when the receiver class is value-type-promoted: such a receiver is a
by-value struct (sp_X), not sp_X *.
Bind the rebound self by value and dereference its ivars with . (via
g_self_deref) when the receiver class is a value type; keep the pointer form
otherwise. Value types are immutable, so the by-value copy is transparent. This
matches the direct-instance_exec value-type path (a value-type class that stays
heap-allocated is unaffected -- it keeps the pointer form).
Refs #1374
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 2772baa049d2
- Parent
- e16d3e7fd82f
src/codegen_call.cmodifiedView file
TyKind body_ty = ie_bn > 0 ? comp_ntype(c, ie_bb[ie_bn - 1]) : TY_NIL; int scalar_res = is_scalar_ret(body_ty) && body_ty != TY_VOID && body_ty != TY_NIL && body_ty != TY_UNKNOWN; int tr = ++g_tmp, tres = ++g_tmp; int self_is_val = c->classes[cls_id].is_value_type; Buf rb; memset(&rb, 0, sizeof rb); if (ie_self_cls >= 0) buf_puts(&rb, g_self); /* implicit self */ else emit_expr(c, recv, &rb); emit_indent(g_pre, g_indent); buf_printf(g_pre, "sp_%s *_t%d = %s;\n", c->classes[cls_id].name, tr, rb.p ? rb.p : "NULL"); /* A value-type receiver is a stack struct, not a pointer: bind the rebound self by value and dereference its ivars with `.` in the splice. Value types are immutable, so the copy is transparent. */ buf_printf(g_pre, "sp_%s %s_t%d = %s;\n", c->classes[cls_id].name, self_is_val ? "" : "*", tr, rb.p ? rb.p : (self_is_val ? "{0}" : "NULL")); free(rb.p); if (scalar_res) { emit_indent(g_pre, g_indent); emit_ctype(c, body_ty, g_pre); } char selfbuf[64]; snprintf(selfbuf, sizeof selfbuf, "_t%d", tr); const char *saved_self2 = g_self; g_self = selfbuf; const char *saved_deref2 = g_self_deref; g_self_deref = self_is_val ? "." : "->"; int saved_ie = g_ie_class_id; g_ie_class_id = cls_id; /* Bind the block params (interned in the enclosing scope, declared there): instance_exec assigns the call-site args; instance_eval } g_ie_class_id = saved_ie; g_self = saved_self2; g_self_deref = saved_deref2; if (scalar_res) buf_printf(b, "_t%d", tres); else buf_printf(b, "_t%d", tr); /* statement use: value is the receiver */ return;test/instance_exec_valuetype_trampoline.rbaddedView file
Loading diff…
test/instance_exec_valuetype_trampoline.rb.expectedaddedView file
Loading diff…