Commit db5b5032

db5b5032f4a3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

Skip dead (never-referenced) methods instead of type-checking them

A method whose name is never referenced as a call, alias target, or symbol literal is dead code; emitting it forces type inference on uninvoked params that have no call sites to constrain them. Compute a reachability flag and skip both the prototype and body of dead methods, matching the legacy compiler. An implicit-invocation safelist (to_s, ==, each, <=>, ...) keeps methods reached only through desugaring live.

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

Details

Commit
db5b5032f4a3
Parent
f0499e539cbc

src/analyze.cmodifiedView file

@@ -1118,6 +1118,46 @@
11181118 return changed;
11191119 }
11201120
1121+/* Mark each method scope reachable iff its name is referenced anywhere as
1122+ a call name, alias target, or symbol literal. Scope 0 (top level) and
1123+ every `initialize` are always reachable. */
1124+static void compute_reachable(Compiler *c) {
1125+ const NodeTable *nt = c->nt;
1126+ /* Build the set of referenced names. */
1127+ char **names = NULL; int n = 0, cap = 0;
1128+ #define ADD_NAME(NM) do { const char *_nm = (NM); \
1129+ if (_nm) { int _f = 0; for (int _i = 0; _i < n; _i++) if (!strcmp(names[_i], _nm)) { _f = 1; break; } \
1130+ if (!_f) { if (n >= cap) { cap = cap ? cap*2 : 32; names = realloc(names, sizeof(char*)*(size_t)cap); } names[n++] = strdup(_nm); } } } while (0)
1131+
1132+ for (int id = 0; id < nt->count; id++) {
1133+ const char *ty = nt_type(nt, id);
1134+ if (!ty) continue;
1135+ if (!strcmp(ty, "CallNode")) ADD_NAME(nt_str(nt, id, "name"));
1136+ else if (!strcmp(ty, "SymbolNode")) ADD_NAME(nt_str(nt, id, "value"));
1137+ }
1138+ for (int ci = 0; ci < c->nclasses; ci++) {
1139+ ClassInfo *cls = &c->classes[ci];
1140+ for (int i = 0; i < cls->naliases; i++) { ADD_NAME(cls->alias_new[i]); ADD_NAME(cls->alias_old[i]); }
1141+ }
1142+
1143+ /* Names that may be invoked implicitly (no explicit CallNode): keep live. */
1144+ static const char *const implicit[] = {
1145+ "to_s", "inspect", "==", "<=>", "eql?", "hash", "each", "coerce",
1146+ "to_str", "to_ary", "to_a", "to_i", "to_int", "to_h", "to_proc", "call", NULL };
1147+
1148+ for (int s = 0; s < c->nscopes; s++) {
1149+ Scope *sc = &c->scopes[s];
1150+ if (s == 0 || !sc->name || !strcmp(sc->name, "initialize")) { sc->reachable = 1; continue; }
1151+ sc->reachable = 0;
1152+ for (int i = 0; implicit[i]; i++) if (!strcmp(implicit[i], sc->name)) { sc->reachable = 1; break; }
1153+ if (sc->reachable) continue;
1154+ for (int i = 0; i < n; i++) if (!strcmp(names[i], sc->name)) { sc->reachable = 1; break; }
1155+ }
1156+ for (int i = 0; i < n; i++) free(names[i]);
1157+ free(names);
1158+ #undef ADD_NAME
1159+}
1160+
11211161 void analyze_program(Compiler *c) {
11221162 /* scope 0 = top level */
11231163 Scope *top = comp_scope_new(c, NULL, -1);
@@ -1179,6 +1219,13 @@
11791219 if (!ch) break;
11801220 }
11811221
1222+ /* Reachability: an instance/free method is live only if its name is
1223+ referenced somewhere -- as a call name, an alias target, or a symbol
1224+ literal (covering send/method/define_method). Names never mentioned
1225+ are dead code; skipping them avoids type-checking uninvoked methods
1226+ (e.g. a never-called method with an uninferrable param). */
1227+ compute_reachable(c);
1228+
11821229 /* finalize: gc-root needs + full node type cache */
11831230 for (int s = 0; s < c->nscopes; s++)
11841231 for (int i = 0; i < c->scopes[s].nlocals; i++)