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
&bBlockArgumentNode to the active literal block at the inline site, asinner(&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
int blk = nt_ref(nt, id, "block"); if (blk < 0) return -1; const char *bty = nt_type(nt, blk); if (!bty || strcmp(bty, "BlockNode")) return -1; /* A literal block, or a `&b` forward of the enclosing method's block (which resolves to the literal active where the method inlines). */ if (!bty || (strcmp(bty, "BlockNode") && strcmp(bty, "BlockArgumentNode"))) return -1; Scope *s = comp_scope_of(c, id); if (!s || s->class_id < 0 || s->is_cmethod) return -1; return s->class_id;src/analyze_infer.cmodifiedView file
Loading diff…
src/analyze_pass.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
test/instance_exec_trampoline_result.rbaddedView file
Loading diff…
test/instance_exec_trampoline_result.rb.expectedaddedView file
Loading diff…