Commit eb58d48f

eb58d48f362dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +4 added · ~1 modified

Description

Merge pull request #1504 from ryanseys/rs-send-runtime-diagnostic

Diagnose send/public_send with a runtime method name

Details

Commit
eb58d48f362d
Parent
b7b7de0821a8
Merge parent
b2f3eee5f8db

src/codegen.cmodifiedView file

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