Commit 9af4022c

9af4022c222dMerged

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

Description

analyze/codegen: fix attr_reader/alias for class reopenings

  1. register_attrs and register_aliases only scanned the first class body (def_node). Reopened class bodies were skipped, so attr_reader and alias declarations in a second class Foo block were invisible to the rest of the compiler. Add a second pass that scans all ClassNode/ModuleNode in the AST, matching the existing pattern used by the include-processing pass.

  2. infer_type and emit_call used the raw (unresolved) name when building the ivar name for an attr_reader lookup. alias value access_token registered the alias correctly, but then @value was looked up instead of @access_token. Fix by calling comp_resolve_alias before building the ivar field name.

  3. The TY_POLY.call proc-dispatch shortcut was incorrectly intercepting user-defined call methods. Guard it: only use the proc path when no user class defines a call instance method.

Fixes alias_attr_reader_source, bundle_classd_33.

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

Details

Commit
9af4022c222d
Parent
10e800f23460

src/analyze.cmodifiedView file

@@ -682,12 +682,16 @@
682682 if (!strcmp(name, "is_a?") || !strcmp(name, "kind_of?") || !strcmp(name, "instance_of?") ||
683683 !strcmp(name, "respond_to?") || !strcmp(name, "==") || !strcmp(name, "!=") ||
684684 !strcmp(name, "nil?") || !strcmp(name, "equal?") || !strcmp(name, "frozen?")) return TY_BOOL;
685- /* attr reader */
686- if (comp_reader_in_chain(c, cid, name, NULL)) {
687- char ivn[256];
688- snprintf(ivn, sizeof ivn, "@%s", name);
689- int iv = comp_ivar_index(cls, ivn);
690- if (iv >= 0) return cls->ivar_types[iv];
685+ /* attr reader (resolve alias so `alias v access_token` returns @access_token type) */
686+ { int rdcls = -1;
687+ if (comp_reader_in_chain(c, cid, name, &rdcls)) {
688+ const char *rname = comp_resolve_alias(c, cid, name);
689+ char ivn[256];
690+ snprintf(ivn, sizeof ivn, "@%s", rname);
691+ ClassInfo *rci = (rdcls >= 0 && rdcls < c->nclasses) ? &c->classes[rdcls] : cls;
692+ int iv = comp_ivar_index(rci, ivn);
693+ if (iv >= 0) return rci->ivar_types[iv];
694+ }
691695 }
692696 /* attr writer: obj.x= returns the assigned value */
693697 size_t ln = strlen(name);
@@ -720,11 +724,15 @@
720724 if (recv < 0) {
721725 Scope *self = comp_scope_of(c, id);
722726 if (self->class_id >= 0) {
723- if (comp_reader_in_chain(c, self->class_id, name, NULL)) {
724- char ivn[256];
725- snprintf(ivn, sizeof ivn, "@%s", name);
726- int iv = comp_ivar_index(&c->classes[self->class_id], ivn);
727- if (iv >= 0) return c->classes[self->class_id].ivar_types[iv];
727+ { int rdcls2 = -1;
728+ if (comp_reader_in_chain(c, self->class_id, name, &rdcls2)) {
729+ const char *rname2 = comp_resolve_alias(c, self->class_id, name);
730+ char ivn[256];
731+ snprintf(ivn, sizeof ivn, "@%s", rname2);
732+ ClassInfo *rci2 = (rdcls2 >= 0 && rdcls2 < c->nclasses) ? &c->classes[rdcls2] : &c->classes[self->class_id];
733+ int iv = comp_ivar_index(rci2, ivn);
734+ if (iv >= 0) return rci2->ivar_types[iv];
735+ }
728736 }
729737 /* bare `new` inside a class method returns an instance of self's class */
730738 if (self->is_cmethod && !strcmp(name, "new"))
@@ -2278,67 +2286,102 @@
22782286 /* Collect attr_reader/attr_writer/attr_accessor declarations in class
22792287 bodies, registering backing ivars + reader/writer method names.
22802288 Also scans class << self bodies for singleton-level attr_accessors. */
2289+static void register_attrs_body(Compiler *c, ClassInfo *cls, int body) {
2290+ const NodeTable *nt = c->nt;
2291+ int n = 0;
2292+ const int *stmts = body >= 0 ? nt_arr(nt, body, "body", &n) : NULL;
2293+ for (int k = 0; k < n; k++) {
2294+ int s = stmts[k];
2295+ const char *sty = nt_type(nt, s);
2296+ if (!sty) continue;
2297+ if (!strcmp(sty, "CallNode")) {
2298+ register_attr_call(c, cls, s, 0);
2299+ }
2300+ else if (!strcmp(sty, "SingletonClassNode")) {
2301+ /* class << self; attr_accessor :x; end */
2302+ int sbody = nt_ref(nt, s, "body");
2303+ if (sbody < 0) continue;
2304+ int sn = 0;
2305+ const int *sstmts = nt_arr(nt, sbody, "body", &sn);
2306+ for (int j = 0; j < sn; j++) {
2307+ int ss = sstmts[j];
2308+ const char *ssty = nt_type(nt, ss);
2309+ if (ssty && !strcmp(ssty, "CallNode"))
2310+ register_attr_call(c, cls, ss, 1);
2311+ }
2312+ }
2313+ }
2314+}
2315+
22812316 static void register_attrs(Compiler *c) {
22822317 const NodeTable *nt = c->nt;
2318+ /* Pass 1: process primary definition bodies. */
22832319 for (int ci = 0; ci < c->nclasses; ci++) {
22842320 ClassInfo *cls = &c->classes[ci];
2285- int body = nt_ref(nt, cls->def_node, "body");
2286- int n = 0;
2287- const int *stmts = body >= 0 ? nt_arr(nt, body, "body", &n) : NULL;
2288- for (int k = 0; k < n; k++) {
2289- int s = stmts[k];
2290- const char *sty = nt_type(nt, s);
2291- if (!sty) continue;
2292- if (!strcmp(sty, "CallNode")) {
2293- register_attr_call(c, cls, s, 0);
2294- } else if (!strcmp(sty, "SingletonClassNode")) {
2295- /* class << self; attr_accessor :x; end */
2296- int sbody = nt_ref(nt, s, "body");
2297- if (sbody < 0) continue;
2298- int sn = 0;
2299- const int *sstmts = nt_arr(nt, sbody, "body", &sn);
2300- for (int j = 0; j < sn; j++) {
2301- int ss = sstmts[j];
2302- const char *ssty = nt_type(nt, ss);
2303- if (ssty && !strcmp(ssty, "CallNode"))
2304- register_attr_call(c, cls, ss, 1);
2305- }
2306- }
2307- }
2321+ register_attrs_body(c, cls, nt_ref(nt, cls->def_node, "body"));
2322+ }
2323+ /* Pass 2: scan all ClassNode/ModuleNode reopenings. */
2324+ for (int id = 0; id < nt->count; id++) {
2325+ const char *ty = nt_type(nt, id);
2326+ if (!ty || (strcmp(ty, "ClassNode") && strcmp(ty, "ModuleNode"))) continue;
2327+ int cp = nt_ref(nt, id, "constant_path");
2328+ const char *cname = cp >= 0 ? nt_str(nt, cp, "name") : NULL;
2329+ if (!cname) continue;
2330+ int ci = comp_class_index(c, cname);
2331+ if (ci < 0) continue;
2332+ if (id == c->classes[ci].def_node) continue; /* already handled above */
2333+ register_attrs_body(c, &c->classes[ci], nt_ref(nt, id, "body"));
23082334 }
23092335 }
23102336
23112337 /* Collect `alias new old` (AliasMethodNode) and `alias_method :new, :old`
23122338 (CallNode) statements in class bodies into the class alias table. */
2339+static void register_aliases_body(Compiler *c, ClassInfo *cls, int body) {
2340+ const NodeTable *nt = c->nt;
2341+ int n = 0;
2342+ const int *stmts = body >= 0 ? nt_arr(nt, body, "body", &n) : NULL;
2343+ for (int k = 0; k < n; k++) {
2344+ int s = stmts[k];
2345+ const char *sty = nt_type(nt, s);
2346+ if (!sty) continue;
2347+ if (!strcmp(sty, "AliasMethodNode")) {
2348+ int nn = nt_ref(nt, s, "new_name");
2349+ int on = nt_ref(nt, s, "old_name");
2350+ const char *nw = nn >= 0 ? nt_str(nt, nn, "value") : NULL;
2351+ const char *od = on >= 0 ? nt_str(nt, on, "value") : NULL;
2352+ comp_add_alias(cls, nw, od);
2353+ }
2354+ else if (!strcmp(sty, "CallNode")) {
2355+ const char *nm = nt_str(nt, s, "name");
2356+ if (!nm || strcmp(nm, "alias_method")) continue;
2357+ int args = nt_ref(nt, s, "arguments");
2358+ int an = 0;
2359+ const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
2360+ if (an >= 2 && nt_type(nt, argv[0]) && !strcmp(nt_type(nt, argv[0]), "SymbolNode") &&
2361+ nt_type(nt, argv[1]) && !strcmp(nt_type(nt, argv[1]), "SymbolNode"))
2362+ comp_add_alias(cls, nt_str(nt, argv[0], "value"), nt_str(nt, argv[1], "value"));
2363+ }
2364+ }
2365+}
2366+
23132367 static void register_aliases(Compiler *c) {
23142368 const NodeTable *nt = c->nt;
2369+ /* Pass 1: primary definition bodies. */
23152370 for (int ci = 0; ci < c->nclasses; ci++) {
23162371 ClassInfo *cls = &c->classes[ci];
2317- int body = nt_ref(nt, cls->def_node, "body");
2318- int n = 0;
2319- const int *stmts = body >= 0 ? nt_arr(nt, body, "body", &n) : NULL;
2320- for (int k = 0; k < n; k++) {
2321- int s = stmts[k];
2322- const char *sty = nt_type(nt, s);
2323- if (!sty) continue;
2324- if (!strcmp(sty, "AliasMethodNode")) {
2325- int nn = nt_ref(nt, s, "new_name");
2326- int on = nt_ref(nt, s, "old_name");
2327- const char *nw = nn >= 0 ? nt_str(nt, nn, "value") : NULL;
2328- const char *od = on >= 0 ? nt_str(nt, on, "value") : NULL;
2329- comp_add_alias(cls, nw, od);
2330- }
2331- else if (!strcmp(sty, "CallNode")) {
2332- const char *nm = nt_str(nt, s, "name");
2333- if (!nm || strcmp(nm, "alias_method")) continue;
2334- int args = nt_ref(nt, s, "arguments");
2335- int an = 0;
2336- const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
2337- if (an >= 2 && nt_type(nt, argv[0]) && !strcmp(nt_type(nt, argv[0]), "SymbolNode") &&
2338- nt_type(nt, argv[1]) && !strcmp(nt_type(nt, argv[1]), "SymbolNode"))
2339- comp_add_alias(cls, nt_str(nt, argv[0], "value"), nt_str(nt, argv[1], "value"));
2340- }
2341- }
2372+ register_aliases_body(c, cls, nt_ref(nt, cls->def_node, "body"));
2373+ }
2374+ /* Pass 2: reopened class/module bodies. */
2375+ for (int id = 0; id < nt->count; id++) {
2376+ const char *ty = nt_type(nt, id);
2377+ if (!ty || (strcmp(ty, "ClassNode") && strcmp(ty, "ModuleNode"))) continue;
2378+ int cp = nt_ref(nt, id, "constant_path");
2379+ const char *cname = cp >= 0 ? nt_str(nt, cp, "name") : NULL;
2380+ if (!cname) continue;
2381+ int ci = comp_class_index(c, cname);
2382+ if (ci < 0) continue;
2383+ if (id == c->classes[ci].def_node) continue;
2384+ register_aliases_body(c, &c->classes[ci], nt_ref(nt, id, "body"));
23422385 }
23432386 }
23442387