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

@@ -38,10 +38,14 @@
3838 }
3939 }
4040
41- /* Names that may be invoked implicitly (no explicit CallNode): keep live. */
41+ /* Names that may be invoked implicitly (no explicit CallNode): keep live.
42+ method_missing is reached only through the fallback dispatch the codegen
43+ synthesizes, never a direct call, so it must be seeded here or the
44+ reachability BFS would prune it. */
4245 static const char *const implicit[] = {
4346 "to_s", "inspect", "==", "<=>", "eql?", "hash", "each", "coerce",
44- "to_str", "to_ary", "to_a", "to_i", "to_int", "to_h", "to_proc", "call", NULL };
47+ "to_str", "to_ary", "to_a", "to_i", "to_int", "to_h", "to_proc", "call",
48+ "method_missing", NULL };
4549
4650 /* BFS queue (scope indices). */
4751 int *queue = malloc((size_t)c->nscopes * sizeof(int));
@@ -2013,6 +2017,22 @@
20132017 !strcmp(snm, "ir_emit_sa") ? TY_STR_ARRAY : TY_INT_ARRAY;
20142018 sc->ret = TY_STRING; /* each returns the accumulated buf (a string) */
20152019 }
2020+ /* Seed method_missing's signature. It is reached only through the synthesized
2021+ fallback dispatch (no AST call site), so the param backstop below can't bind
2022+ it: name is the called method's Symbol and args is the rest poly-array.
2023+ Unseeded it keeps TY_UNKNOWN params and gets pruned. */
2024+ for (int s = 0; s < c->nscopes; s++) {
2025+ Scope *sc = &c->scopes[s];
2026+ if (sc->class_id < 0 || !sc->name) continue;
2027+ if (!strcmp(sc->name, "method_missing") && sc->nparams >= 1 && sc->pnames) {
2028+ LocalVar *pn = scope_local(sc, sc->pnames[0]);
2029+ if (pn && pn->type == TY_UNKNOWN) pn->type = TY_SYMBOL;
2030+ if (sc->rest_idx >= 0 && sc->rest_idx < sc->nparams) {
2031+ LocalVar *pr = scope_local(sc, sc->pnames[sc->rest_idx]);
2032+ if (pr && pr->type == TY_UNKNOWN) pr->type = TY_POLY_ARRAY;
2033+ }
2034+ }
2035+ }
20162036 for (int it = 0; it < 8; it++) { if (!infer_param_types(c)) break; }
20172037
20182038 /* Backstop step 2: a reachable method that STILL has TY_UNKNOWN params was