Commit ef007427

ef0074272cc5Merged

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

Description

codegen: support instance_exec forwarding from a non-trampoline method body

A method that forwards its block to a receiverless instance_exec(args, &b) but does more than that single call (e.g. binds the result to a local and transforms it: r = instance_exec(x, @base, &b); r * 2) failed to compile: the embedded instance_exec was unsupported and the method's return type was left unknown, so a value-position call (puts m.run(...)) reported an unsupported argument.

A receiverless instance_exec(args, &b) is a block invocation -- it runs the forwarded block with args and self unchanged -- so treat it like a yield throughout, which lets the existing yield-inliner handle the whole method:

  • ie_implicit_self_class accepts a &b-forwarded block (not just a literal).
  • infer_call resolves the forwarded instance_exec to the enclosing method's yield value, falling back to poly (a sound boxed carrier, not nil) when the call sites aren't typed yet -- keeping the method inlinable.
  • block params passed to such a method are typed from the instance_exec args (first_ie_exec_args), like first_yield / a block-param .call.
  • the instance_exec splice resolves the &b BlockArgumentNode to the active literal block at the inline site, as inner(&block) already does.

Also fix the yield-inliner's self binding for a value-type receiver: bind self by value (not as a pointer) and dereference its ivars with ., matching the direct/trampoline instance_exec paths.

Refs #1374

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

Details

Commit
ef0074272cc5
Parent
2772baa049d2

src/analyze.cmodifiedView file

@@ -448,7 +448,9 @@
448448 int blk = nt_ref(nt, id, "block");
449449 if (blk < 0) return -1;
450450 const char *bty = nt_type(nt, blk);
451- if (!bty || strcmp(bty, "BlockNode")) return -1;
451+ /* A literal block, or a `&b` forward of the enclosing method's block (which
452+ resolves to the literal active where the method inlines). */
453+ if (!bty || (strcmp(bty, "BlockNode") && strcmp(bty, "BlockArgumentNode"))) return -1;
452454 Scope *s = comp_scope_of(c, id);
453455 if (!s || s->class_id < 0 || s->is_cmethod) return -1;
454456 return s->class_id;