Commit 84bdebc0

84bdebc0e2b3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 10 files · +6 added · ~4 modified

Description

Bind mixed local/ivar/literal args for instance_exec trampolines

A trampoline def m(p..., &b); instance_exec(tbody..., &b); end whose body passes more than a 1:1 forward of the caller's args -- e.g. instance_exec(x, @base, 7, &b) mixing a method param, an ivar, and a literal -- bound the caller block's params to the caller's args, so a 3-param block fed by m.fire(3) { |a,b,c| } only bound a (and left b/c poly, miscompiling the body).

ie_tramp_effective_arg resolves the node to bind/emit for each block param: the p-th trampoline-body arg, with a read of one of the trampoline's own positional params rewritten to the matching caller argument (an ivar or literal is emitted as-is in the rebound-self splice). It bails on a splat tbody, leaving the existing 1:1 forwarding path for instance_exec(*args, &b) untouched. Both the analyze block-param typing and the codegen binding use it.

Adds instance_exec_mixed_args; the splat and spliced-types forms already worked and gain regression tests.

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

Details

Commit
84bdebc0e2b3
Parent
fdbe84a788f9

src/analyze.cmodifiedView file

@@ -443,6 +443,61 @@
443443 return (lty && !strcmp(lty, "KeywordHashNode")) ? av[ac - 1] : -1;
444444 }
445445
446+/* For a call `recv.m(cargs) { ... }` to an instance_exec trampoline
447+ `def m(p..., &b); instance_exec(tbody..., &b); end`, the node to bind/emit
448+ for the block's p-th parameter: the p-th trampoline-body arg, with a read of
449+ one of the trampoline's own positional params rewritten to the matching
450+ caller argument. Returns -1 when out of range, when not such a trampoline, or
451+ when tbody uses a splat (the existing 1:1 forwarding path handles that).
452+ ie_tramp_effective_argc returns the tbody arg count (or -1 to bail). */
453+static int ie_tramp_body_args(Compiler *c, int caller_id, const int **tav_out, Scope **ms_out) {
454+ const NodeTable *nt = c->nt;
455+ int recv = nt_ref(nt, caller_id, "receiver");
456+ if (recv < 0) return -1;
457+ TyKind rt = infer_type(c, recv);
458+ if (!ty_is_object(rt)) return -1;
459+ const char *nm = nt_str(nt, caller_id, "name");
460+ int mi = nm ? comp_method_in_chain(c, ty_object_class(rt), nm, NULL) : -1;
461+ if (mi < 0) return -1;
462+ Scope *ms = &c->scopes[mi];
463+ if (ms->body < 0) return -1;
464+ int bn = 0; const int *bb = nt_arr(nt, ms->body, "body", &bn);
465+ if (bn != 1 || !bb) return -1;
466+ int targs = nt_ref(nt, bb[0], "arguments");
467+ int tac = 0; const int *tav = targs >= 0 ? nt_arr(nt, targs, "arguments", &tac) : NULL;
468+ for (int i = 0; i < tac; i++) {
469+ const char *aty = nt_type(nt, tav[i]);
470+ if (aty && !strcmp(aty, "SplatNode")) return -1; /* forwarding path handles splat */
471+ }
472+ if (tav_out) *tav_out = tav;
473+ if (ms_out) *ms_out = ms;
474+ return tac;
475+}
476+
477+int ie_tramp_effective_argc(Compiler *c, int caller_id) {
478+ return ie_tramp_body_args(c, caller_id, NULL, NULL);
479+}
480+
481+int ie_tramp_effective_arg(Compiler *c, int caller_id, int p) {
482+ const NodeTable *nt = c->nt;
483+ const int *tav = NULL; Scope *ms = NULL;
484+ int tac = ie_tramp_body_args(c, caller_id, &tav, &ms);
485+ if (tac < 0 || p < 0 || p >= tac) return -1;
486+ int arg = tav[p];
487+ const char *aty = nt_type(nt, arg);
488+ if (aty && !strcmp(aty, "LocalVariableReadNode")) {
489+ const char *an = nt_str(nt, arg, "name");
490+ for (int j = 0; j < ms->nparams; j++) {
491+ if (ms->pnames[j] && an && !strcmp(ms->pnames[j], an)) {
492+ int cargs = nt_ref(nt, caller_id, "arguments");
493+ int cac = 0; const int *cav = cargs >= 0 ? nt_arr(nt, cargs, "arguments", &cac) : NULL;
494+ return j < cac ? cav[j] : -1;
495+ }
496+ }
497+ }
498+ return arg; /* ivar / literal / other: evaluated in the rebound-self context */
499+}
500+
446501 /* (Re)build the instance_eval/exec node→class map from current receiver types. */
447502 void build_ie_map(Compiler *c) {
448503 const NodeTable *nt = c->nt;