Commit 1d52911e
1d52911e69c5Merged
Ryan Seys <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified
Description
analyze,codegen: dispatch unresolved object calls to method_missing
An unresolved call on a concrete user-object receiver was a hard compile
error, even when the receiver's class chain defined method_missing.
CRuby routes such calls to method_missing, passing the called name as a
Symbol and the remaining arguments as a rest array; spinel now does the
same when the shape is statically lowerable.
The routing is placed at the very end of both call inference and call
codegen, after every builtin, reopened-class, and universal-method path
has had its chance, so method_missing only fires when nothing else
resolves the call -- matching CRuby's lookup order. Inference returns the
method's declared return type; codegen emits a direct call to the
chain-resolved method_missing, casting the receiver to the defining
class and packing the call arguments into a fresh poly array.
Two supporting fixes were needed. method_missing has no AST call site,
so the parameter backstop pruned it as having unbound (TY_UNKNOWN)
params; its signature is now seeded (name -> Symbol, rest -> poly array)
alongside the existing synthesized-helper seeding, and the name is kept
reachable so the BFS does not drop it. The called method's name is
interned during analysis, before the symbol-name table is emitted, since
interning it at codegen time would land after the table is finalized and
yield an empty symbol at runtime.
Only the canonical (name) and (name, *args) shapes with a usable
scalar return are lowered; a descendant override of method_missing, a
value-type receiver, or a splat/keyword call shape falls through to the
existing diagnostic rather than risk a mis-dispatch. respond_to_missing?
wiring into respond_to? is left as a follow-up.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 1d52911e69c5
- Parent
- b1e4d47607fd
src/analyze.cmodifiedView file
} } /* Names that may be invoked implicitly (no explicit CallNode): keep live. */ /* Names that may be invoked implicitly (no explicit CallNode): keep live. method_missing is reached only through the fallback dispatch the codegen synthesizes, never a direct call, so it must be seeded here or the reachability BFS would prune it. */ static const char *const implicit[] = { "to_s", "inspect", "==", "<=>", "eql?", "hash", "each", "coerce", "to_str", "to_ary", "to_a", "to_i", "to_int", "to_h", "to_proc", "call", NULL }; "to_str", "to_ary", "to_a", "to_i", "to_int", "to_h", "to_proc", "call", "method_missing", NULL }; /* BFS queue (scope indices). */ int *queue = malloc((size_t)c->nscopes * sizeof(int)); !strcmp(snm, "ir_emit_sa") ? TY_STR_ARRAY : TY_INT_ARRAY; sc->ret = TY_STRING; /* each returns the accumulated buf (a string) */ } /* Seed method_missing's signature. It is reached only through the synthesized fallback dispatch (no AST call site), so the param backstop below can't bind it: name is the called method's Symbol and args is the rest poly-array. Unseeded it keeps TY_UNKNOWN params and gets pruned. */ for (int s = 0; s < c->nscopes; s++) { Scope *sc = &c->scopes[s]; if (sc->class_id < 0 || !sc->name) continue; if (!strcmp(sc->name, "method_missing") && sc->nparams >= 1 && sc->pnames) { LocalVar *pn = scope_local(sc, sc->pnames[0]); if (pn && pn->type == TY_UNKNOWN) pn->type = TY_SYMBOL; if (sc->rest_idx >= 0 && sc->rest_idx < sc->nparams) { LocalVar *pr = scope_local(sc, sc->pnames[sc->rest_idx]); if (pr && pr->type == TY_UNKNOWN) pr->type = TY_POLY_ARRAY; } } } for (int it = 0; it < 8; it++) { if (!infer_param_types(c)) break; } /* Backstop step 2: a reachable method that STILL has TY_UNKNOWN params wassrc/analyze_infer.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
test/method_missing_basic.rbaddedView file
Loading diff…
test/method_missing_basic.rb.expectedaddedView file
Loading diff…