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
selfinto 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
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); }}/* For a receiverless instance_eval/exec CallNode with a literal block inside an instance method, the receiver is self (CRuby resolves it to self.instance_exec). Return that class index, else -1. The literal-block requirement (a BlockNode, not a `&b` BlockArgumentNode) keeps this distinct from a trampoline body's `instance_exec(args, &b)`, which codegen lowers via its own trampoline detector. */int ie_implicit_self_class(Compiler *c, int id) { const NodeTable *nt = c->nt; if (nt_ref(nt, id, "receiver") >= 0) return -1; const char *nm = nt_str(nt, id, "name"); if (!nm || (strcmp(nm, "instance_eval") && strcmp(nm, "instance_exec"))) return -1; 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; Scope *s = comp_scope_of(c, id); if (!s || s->class_id < 0 || s->is_cmethod) return -1; return s->class_id;}/* (Re)build the instance_eval/exec node→class map from current receiver types. */void build_ie_map(Compiler *c) { const NodeTable *nt = c->nt; if (!nm) continue; int recv = nt_ref(nt, id, "receiver"); int blk = nt_ref(nt, id, "block"); if (recv < 0 || blk < 0) continue; TyKind rt = infer_type(c, recv); if (!ty_is_object(rt)) continue; int cls = ty_object_class(rt); if (strcmp(nm, "instance_eval") && strcmp(nm, "instance_exec")) { /* not a direct instance_eval/exec: maybe a trampoline method on `cls`? */ if (!comp_trampoline_kind(c, cls, nm, NULL)) continue; if (blk < 0) continue; int cls; if (recv < 0) { /* receiverless instance_eval/exec inside an instance method: self. */ cls = ie_implicit_self_class(c, id); if (cls < 0) continue; } else { TyKind rt = infer_type(c, recv); if (!ty_is_object(rt)) continue; cls = ty_object_class(rt); if (strcmp(nm, "instance_eval") && strcmp(nm, "instance_exec")) { /* not a direct instance_eval/exec: maybe a trampoline method on `cls`? */ if (!comp_trampoline_kind(c, cls, nm, NULL)) continue; } } int body = nt_ref(nt, blk, "body"); if (body >= 0) mark_ie_subtree(c, body, cls);src/analyze.hmodifiedView file
Loading diff…
src/analyze_infer.cmodifiedView file
Loading diff…
src/analyze_pass.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
test/instance_exec_const_receiver.rbaddedView file
Loading diff…
test/instance_exec_implicit_self.rbaddedView file
Loading diff…
test/instance_exec_nested.rbaddedView file
Loading diff…
test/instance_exec_toplevel_method.rbaddedView file
Loading diff…