Commit b2f3eee5

b2f3eee5f8dbMerged

Ryan Seys <[email protected]> · 5 months ago · 5 files · +4 added · ~1 modified

Description

codegen: diagnose send/public_send with a runtime method name

A literal-name send/public_send is already rewritten to a direct call. A runtime method name cannot be resolved by AOT compilation, so emit a specific diagnostic instead of a silent nil-stub or an opaque node-dump reject.

The check is scoped to sends that codegen will actually emit: a send in an unreachable (dead-code-eliminated) method is skipped, because rejecting it would fail otherwise-valid programs that merely contain an unused method. A program that defines its own send is left to normal dispatch.

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

Details

Commit
b2f3eee5f8db
Parent
b1e4d47607fd

src/codegen.cmodifiedView file

@@ -2264,6 +2264,50 @@
22642264 } \
22652265 } while (0)
22662266
2267+/* `send` / `__send__` / `public_send` with a literal symbol/string name is
2268+ rewritten to a direct call before this point (textually for a receiver form,
2269+ on the AST for implicit self). A call to one of these that survives therefore
2270+ has a runtime method name, which AOT cannot dispatch in a closed world --
2271+ turn the opaque downstream reject (or silent nil-stub) into one actionable
2272+ diagnostic anchored to the call site. Skipped entirely if the program defines
2273+ its own method by that name, since then the call resolves normally. */
2274+static void reject_runtime_send(Compiler *c) {
2275+ const NodeTable *nt = c->nt;
2276+ static const char *const names[] = { "send", "__send__", "public_send", NULL };
2277+ for (int s = 0; s < c->nscopes; s++) {
2278+ const char *sn = c->scopes[s].name;
2279+ if (!sn) continue;
2280+ for (int k = 0; names[k]; k++)
2281+ if (!strcmp(sn, names[k])) return; /* user-defined: leave to normal dispatch */
2282+ }
2283+ for (int id = 0; id < nt->count; id++) {
2284+ const char *ty = nt_type(nt, id);
2285+ if (!ty || strcmp(ty, "CallNode")) continue;
2286+ const char *nm = nt_str(nt, id, "name");
2287+ if (!nm) continue;
2288+ int is_send = 0;
2289+ for (int k = 0; names[k]; k++) if (!strcmp(nm, names[k])) { is_send = 1; break; }
2290+ if (!is_send) continue;
2291+ int args = nt_ref(nt, id, "arguments");
2292+ if (args < 0) continue;
2293+ int ac = 0; const int *av = nt_arr(nt, args, "arguments", &ac);
2294+ if (ac < 1 || !av) continue;
2295+ const char *a0 = nt_type(nt, av[0]);
2296+ /* a literal name should have been rewritten already; only a runtime name
2297+ (a variable, a method result, an interpolated string, ...) reaches here */
2298+ if (a0 && (!strcmp(a0, "SymbolNode") || !strcmp(a0, "StringNode"))) continue;
2299+ /* Only diagnose a send that codegen will actually emit. A send in a dead
2300+ (unreachable) method is pruned before emission, so rejecting it would
2301+ fail otherwise-valid programs that merely contain an unused method.
2302+ walk_scope assigns every node — including those inside blocks — the
2303+ enclosing method's scope, and the emit loop emits a scope's body only
2304+ when it is reachable; mirror that gate here. */
2305+ int sc = c->nscope[id];
2306+ if (sc < 0 || sc >= c->nscopes || !c->scopes[sc].reachable) continue;
2307+ unsupported(c, id, "send with a runtime method name (AOT needs a compile-time-known name)");
2308+ }
2309+}
2310+
22672311 char *codegen_program(const NodeTable *nt) {
22682312 Compiler *c = comp_new(nt);
22692313 analyze_program(c);
@@ -2302,6 +2346,10 @@
23022346 return strdup("");
23032347 }
23042348
2349+ /* Reject runtime-name send before any emission so the diagnostic fires
2350+ regardless of how the call's result is later consumed. */
2351+ reject_runtime_send(c);
2352+
23052353 Buf b; memset(&b, 0, sizeof b);
23062354 memset(&g_procs, 0, sizeof g_procs);
23072355 memset(&g_proc_protos, 0, sizeof g_proc_protos);