Commit 5a7453ff

5a7453ff51abMerged

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

Description

Resolve implicit-self receiver for direct instance_exec/eval

A receiverless instance_exec { ... } / instance_eval { ... } inside an instance method resolves, per CRuby, to self.instance_exec { ... }. Spinel rejected it ('unsupported call: instance_exec recv=-') because every instance_exec path required an explicit receiver.

ie_implicit_self_class recognizes the form -- a receiverless instance_eval/ exec with a literal block (a BlockNode, not a &b BlockArgumentNode, which stays a trampoline-body call) inside an instance method -- and returns the method's class. The three receiver-resolving sites use it:

  • build_ie_map marks the block body with that class so bare calls inside (add(n)) dispatch on self;
  • the call return-type and the instance_exec block-param binding accept the receiverless form;
  • codegen lowers it like a direct call, aliasing the existing self into the splice temp instead of evaluating a receiver expression.

The const-receiver, nested, and top-level-method forms already worked; adds their tests alongside the new implicit-self one.

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

Details

Commit
5a7453ff51ab
Parent
649bc00f5658

src/analyze.cmodifiedView file

@@ -395,6 +395,26 @@
395395 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); }
396396 }
397397
398+/* For a receiverless instance_eval/exec CallNode with a literal block inside
399+ an instance method, the receiver is self (CRuby resolves it to
400+ self.instance_exec). Return that class index, else -1. The literal-block
401+ requirement (a BlockNode, not a `&b` BlockArgumentNode) keeps this distinct
402+ from a trampoline body's `instance_exec(args, &b)`, which codegen lowers via
403+ its own trampoline detector. */
404+int ie_implicit_self_class(Compiler *c, int id) {
405+ const NodeTable *nt = c->nt;
406+ if (nt_ref(nt, id, "receiver") >= 0) return -1;
407+ const char *nm = nt_str(nt, id, "name");
408+ if (!nm || (strcmp(nm, "instance_eval") && strcmp(nm, "instance_exec"))) return -1;
409+ int blk = nt_ref(nt, id, "block");
410+ if (blk < 0) return -1;
411+ const char *bty = nt_type(nt, blk);
412+ if (!bty || strcmp(bty, "BlockNode")) return -1;
413+ Scope *s = comp_scope_of(c, id);
414+ if (!s || s->class_id < 0 || s->is_cmethod) return -1;
415+ return s->class_id;
416+}
417+
398418 /* (Re)build the instance_eval/exec node→class map from current receiver types. */
399419 void build_ie_map(Compiler *c) {
400420 const NodeTable *nt = c->nt;
@@ -407,13 +427,21 @@
407427 if (!nm) continue;
408428 int recv = nt_ref(nt, id, "receiver");
409429 int blk = nt_ref(nt, id, "block");
410- if (recv < 0 || blk < 0) continue;
411- TyKind rt = infer_type(c, recv);
412- if (!ty_is_object(rt)) continue;
413- int cls = ty_object_class(rt);
414- if (strcmp(nm, "instance_eval") && strcmp(nm, "instance_exec")) {
415- /* not a direct instance_eval/exec: maybe a trampoline method on `cls`? */
416- if (!comp_trampoline_kind(c, cls, nm, NULL)) continue;
430+ if (blk < 0) continue;
431+ int cls;
432+ if (recv < 0) {
433+ /* receiverless instance_eval/exec inside an instance method: self. */
434+ cls = ie_implicit_self_class(c, id);
435+ if (cls < 0) continue;
436+ }
437+ else {
438+ TyKind rt = infer_type(c, recv);
439+ if (!ty_is_object(rt)) continue;
440+ cls = ty_object_class(rt);
441+ if (strcmp(nm, "instance_eval") && strcmp(nm, "instance_exec")) {
442+ /* not a direct instance_eval/exec: maybe a trampoline method on `cls`? */
443+ if (!comp_trampoline_kind(c, cls, nm, NULL)) continue;
444+ }
417445 }
418446 int body = nt_ref(nt, blk, "body");
419447 if (body >= 0) mark_ie_subtree(c, body, cls);