Commit f0364b82

f0364b82271fMerged

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

Description

instance_exec / instance_eval: inline block expansion

Inline recv.instance_exec(args){|p|body} and recv.instance_eval{...} at the call site with self rebound to the receiver. A new instance_eval/exec node->class map (g_ie_node_class) lets bare calls and ivar reads inside the block resolve against the receiver's class during inference, so the block's method params get typed and the call value is inferred. Block params bind to call args (exec) or the receiver (eval); the call value is the block's last expression.

Fixes instance_exec_direct/parity/capture/valuetype. Numbered-param (_1) instance_eval, multi-statement value bodies, the trampoline forms and bm_instance_eval remain.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
f0364b82271f
Parent
7260fbba568c

src/analyze.cmodifiedView file

@@ -4,6 +4,9 @@
44 #include <stdlib.h>
55 #include <string.h>
66
7+/* The receiver class for a node inside an instance_eval/exec block, or -1. */
8+static int ie_class_of(Compiler *c, int node);
9+
710 /* Forward declarations for FFI helpers defined later in this file. */
811 static const char *ffi_arg_str(const NodeTable *nt, int nid);
912 static int ffi_arg_int(const NodeTable *nt, int nid);
@@ -529,6 +532,19 @@
529532 return result == TY_UNKNOWN ? TY_NIL : result;
530533 }
531534
535+ /* recv.instance_eval/exec { ... } -> the block's last-expression type
536+ (bare calls inside resolve via the ie node->class map). */
537+ if (recv >= 0 && (!strcmp(name, "instance_eval") || !strcmp(name, "instance_exec")) &&
538+ ty_is_object(rt) && comp_method_in_chain(c, ty_object_class(rt), name, NULL) < 0) {
539+ int blk = nt_ref(nt, id, "block");
540+ if (blk >= 0) {
541+ int body = nt_ref(nt, blk, "body");
542+ int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
543+ if (bn > 0) { TyKind bt = infer_type(c, bb[bn - 1]); return bt == TY_VOID ? TY_NIL : bt; }
544+ return TY_NIL;
545+ }
546+ }
547+
532548 /* method(:sym) / <recv>.method(:sym) -> a bound Method object */
533549 if (name && !strcmp(name, "method") && method_sym_arg(c, id) != NULL) return TY_METHOD;
534550
@@ -1319,6 +1335,14 @@
13191335 int smi = comp_cmethod_in_chain(c, g_cbody_class_id, name, NULL);
13201336 if (smi >= 0) return method_call_ret(c, smi, id);
13211337 }
1338+ /* bare call inside an instance_eval/exec block: dispatch on receiver class */
1339+ if (recv < 0) {
1340+ int iec = ie_class_of(c, id);
1341+ if (iec >= 0) {
1342+ int imi = comp_method_in_chain(c, iec, name, NULL);
1343+ if (imi >= 0) return method_call_ret(c, imi, id);
1344+ }
1345+ }
13221346 /* user-defined free-function call (no receiver) */
13231347 if (recv < 0) {
13241348 int mi = comp_method_index(c, name);
@@ -2365,6 +2389,7 @@
23652389 const char *nm = nt_str(nt, id, "name");
23662390 Scope *s = comp_scope_of(c, id);
23672391 int cls_id = (s->class_id >= 0) ? s->class_id : g_ie_class_id;
2392+ if (cls_id < 0) cls_id = ie_class_of(c, id);
23682393 if (cls_id < 0) cls_id = comp_class_index(c, "Toplevel");
23692394 if (cls_id < 0) return TY_UNKNOWN;
23702395 ClassInfo *ci = &c->classes[cls_id];
@@ -5331,6 +5356,14 @@
53315356 if (recv < 0) {
53325357 int mi = comp_method_index(c, name);
53335358 int caller_cid = -1;
5359+ /* bare call inside an instance_eval/exec block: dispatch on the
5360+ receiver's class so its params get the call-site arg types. */
5361+ int iec = ie_class_of(c, id);
5362+ if (mi < 0 && iec >= 0) {
5363+ int def_cid = -1;
5364+ mi = comp_method_in_chain(c, iec, name, &def_cid);
5365+ if (mi >= 0) caller_cid = def_cid >= 0 ? def_cid : iec;
5366+ }
53345367 if (mi < 0) {
53355368 Scope *self = comp_scope_of(c, id);
53365369 if (self->class_id >= 0) {
@@ -5619,6 +5652,60 @@
56195652 }
56205653 }
56215654
5655+ /* recv.instance_eval { |me| } : the block params all receive the receiver
5656+ (Ruby yields self), typed as the receiver's object type. */
5657+ for (int id = 0; id < nt->count; id++) {
5658+ const char *ty = nt_type(nt, id);
5659+ if (!ty || strcmp(ty, "CallNode")) continue;
5660+ const char *cname = nt_str(nt, id, "name");
5661+ if (!cname || strcmp(cname, "instance_eval")) continue;
5662+ int recv = nt_ref(nt, id, "receiver");
5663+ if (recv < 0) continue;
5664+ TyKind rt = infer_type(c, recv);
5665+ if (!ty_is_object(rt)) continue;
5666+ int blk = nt_ref(nt, id, "block");
5667+ if (blk < 0) continue;
5668+ int pn = nt_ref(nt, blk, "parameters");
5669+ if (pn < 0) continue;
5670+ int inner = nt_ref(nt, pn, "parameters");
5671+ int pnode = inner >= 0 ? inner : pn;
5672+ int rnp = 0; const int *reqs = nt_arr(nt, pnode, "requireds", &rnp);
5673+ Scope *bs = comp_scope_of(c, blk);
5674+ for (int k = 0; k < rnp; k++) {
5675+ const char *p = nt_str(nt, reqs[k], "name");
5676+ if (!p) continue;
5677+ LocalVar *lv = scope_local_intern(bs, p); lv->is_block_param = 1;
5678+ if (lv->type != rt) { lv->type = rt; changed = 1; }
5679+ }
5680+ }
5681+
5682+ /* recv.instance_exec(args) { |params| } : block params take the call-site
5683+ arg types (strict arity). */
5684+ for (int id = 0; id < nt->count; id++) {
5685+ const char *ty = nt_type(nt, id);
5686+ if (!ty || strcmp(ty, "CallNode")) continue;
5687+ const char *cname = nt_str(nt, id, "name");
5688+ if (!cname || strcmp(cname, "instance_exec")) continue;
5689+ if (nt_ref(nt, id, "receiver") < 0) continue;
5690+ int blk = nt_ref(nt, id, "block");
5691+ if (blk < 0) continue;
5692+ int pn = nt_ref(nt, blk, "parameters");
5693+ if (pn < 0) continue;
5694+ int inner = nt_ref(nt, pn, "parameters");
5695+ int pnode = inner >= 0 ? inner : pn;
5696+ int rnp = 0; const int *reqs = nt_arr(nt, pnode, "requireds", &rnp);
5697+ int iargs = nt_ref(nt, id, "arguments");
5698+ int iac = 0; const int *iav = iargs >= 0 ? nt_arr(nt, iargs, "arguments", &iac) : NULL;
5699+ Scope *bs = comp_scope_of(c, blk);
5700+ for (int k = 0; k < rnp && k < iac; k++) {
5701+ const char *p = nt_str(nt, reqs[k], "name");
5702+ if (!p) continue;
5703+ TyKind at = infer_type(c, iav[k]);
5704+ LocalVar *lv = scope_local_intern(bs, p); lv->is_block_param = 1;
5705+ if (at != TY_UNKNOWN && lv->type != at) { lv->type = at; changed = 1; }
5706+ }
5707+ }
5708+
56225709 /* Fiber.new { |first| ... }: the block param receives the resume value,
56235710 which is always a poly (boxed) value at the runtime ABI boundary. */
56245711 for (int id = 0; id < nt->count; id++) {
@@ -6807,6 +6894,51 @@
68076894 }
68086895 }
68096896
6897+/* For nodes inside an instance_eval/exec block, the receiver's class id; -1
6898+ elsewhere. Lets bare calls/ivar refs in the block resolve against the
6899+ receiver's class during inference (codegen mirrors this via g_ie_class_id). */
6900+static int *g_ie_node_class = NULL;
6901+
6902+static void mark_ie_subtree(Compiler *c, int node, int cls) {
6903+ if (node < 0) return;
6904+ const char *ty = nt_type(c->nt, node);
6905+ if (!ty) return;
6906+ /* a nested def/class starts a fresh self; don't bleed the rebind into it */
6907+ if (!strcmp(ty, "DefNode") || !strcmp(ty, "ClassNode") || !strcmp(ty, "ModuleNode")) return;
6908+ g_ie_node_class[node] = cls;
6909+ int nr = nt_num_refs(c->nt, node);
6910+ for (int i = 0; i < nr; i++) mark_ie_subtree(c, nt_ref_at(c->nt, node, i), cls);
6911+ int na = nt_num_arrs(c->nt, node);
6912+ for (int i = 0; i < na; i++) { int n = 0; const int *ids = nt_arr_at(c->nt, node, i, &n); for (int k = 0; k < n; k++) mark_ie_subtree(c, ids[k], cls); }
6913+}
6914+
6915+/* (Re)build the instance_eval/exec node→class map from current receiver types. */
6916+static void build_ie_map(Compiler *c) {
6917+ const NodeTable *nt = c->nt;
6918+ if (!g_ie_node_class) g_ie_node_class = malloc(sizeof(int) * (size_t)nt->count);
6919+ for (int i = 0; i < nt->count; i++) g_ie_node_class[i] = -1;
6920+ for (int id = 0; id < nt->count; id++) {
6921+ const char *ty = nt_type(nt, id);
6922+ if (!ty || strcmp(ty, "CallNode")) continue;
6923+ const char *nm = nt_str(nt, id, "name");
6924+ if (!nm || (strcmp(nm, "instance_eval") && strcmp(nm, "instance_exec"))) continue;
6925+ int recv = nt_ref(nt, id, "receiver");
6926+ int blk = nt_ref(nt, id, "block");
6927+ if (recv < 0 || blk < 0) continue;
6928+ TyKind rt = infer_type(c, recv);
6929+ if (!ty_is_object(rt)) continue;
6930+ int cls = ty_object_class(rt);
6931+ int body = nt_ref(nt, blk, "body");
6932+ if (body >= 0) mark_ie_subtree(c, body, cls);
6933+ }
6934+}
6935+
6936+/* The receiver class for a node inside an instance_eval/exec block, or -1. */
6937+static int ie_class_of(Compiler *c, int node) {
6938+ (void)c;
6939+ return (g_ie_node_class && node >= 0) ? g_ie_node_class[node] : -1;
6940+}
6941+
68106942 void analyze_program(Compiler *c) {
68116943 /* scope 0 = top level */
68126944 Scope *top = comp_scope_new(c, NULL, -1);
@@ -6976,6 +7108,7 @@
69767108
69777109 for (int iter = 0; iter < 128; iter++) {
69787110 int ch = 0;
7111+ build_ie_map(c); /* refresh instance_exec receiver-class map each pass */
69797112 ch |= infer_write_types(c);
69807113 ch |= infer_param_types(c);
69817114 ch |= propagate_prep_params(c);