Commit d1ce90c5

d1ce90c59bb8Merged

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

Description

Implement undef method support

Add undefs[] to ClassInfo and register UndefNode in a pre-pass so that method dispatch on TY_OBJECT receivers emits sp_raise_cls for undeffed methods rather than calling the original implementation.

Child-class undef shadows the parent's method for child instances while leaving parent instances callable.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
d1ce90c59bb8
Parent
4c9077b40653

src/analyze.cmodifiedView file

@@ -2719,6 +2719,42 @@
27192719 }
27202720 }
27212721
2722+static void register_undefs_body(Compiler *c, ClassInfo *cls, int body) {
2723+ const NodeTable *nt = c->nt;
2724+ int n = 0;
2725+ const int *stmts = body >= 0 ? nt_arr(nt, body, "body", &n) : NULL;
2726+ for (int k = 0; k < n; k++) {
2727+ int s = stmts[k];
2728+ const char *sty = nt_type(nt, s);
2729+ if (!sty || strcmp(sty, "UndefNode")) continue;
2730+ int names_n = 0;
2731+ const int *names = nt_arr(nt, s, "names", &names_n);
2732+ for (int j = 0; j < names_n; j++) {
2733+ const char *mname = nt_str(nt, names[j], "value");
2734+ if (mname) comp_add_undef(cls, mname);
2735+ }
2736+ }
2737+}
2738+
2739+static void register_undefs(Compiler *c) {
2740+ const NodeTable *nt = c->nt;
2741+ for (int ci = 0; ci < c->nclasses; ci++) {
2742+ ClassInfo *cls = &c->classes[ci];
2743+ register_undefs_body(c, cls, nt_ref(nt, cls->def_node, "body"));
2744+ }
2745+ for (int id = 0; id < nt->count; id++) {
2746+ const char *ty = nt_type(nt, id);
2747+ if (!ty || (strcmp(ty, "ClassNode") && strcmp(ty, "ModuleNode"))) continue;
2748+ int cp = nt_ref(nt, id, "constant_path");
2749+ const char *cname = cp >= 0 ? nt_str(nt, cp, "name") : NULL;
2750+ if (!cname) continue;
2751+ int ci = comp_class_index(c, cname);
2752+ if (ci < 0) continue;
2753+ if (id == c->classes[ci].def_node) continue;
2754+ register_undefs_body(c, &c->classes[ci], nt_ref(nt, id, "body"));
2755+ }
2756+}
2757+
27222758 static int is_c_ident(const char *s) {
27232759 if (!s || !*s) return 0;
27242760 for (const char *p = s; *p; p++)
@@ -6035,6 +6071,7 @@
60356071 register_locals(c);
60366072 register_attrs(c);
60376073 register_aliases(c);
6074+ register_undefs(c);
60386075 register_globals_consts(c);
60396076
60406077 /* rescue variables (`rescue => e`) are typed as exception objects */