Commit 211c332e

211c332e7682Merged

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

Description

Fix Struct.new block defs and add Struct#[] support

fix_struct_block_scopes: after register_structs, reassign scope class_id for DefNodes inside Struct.new { } blocks. walk_scope runs before register_structs, so these defs got class_id=-1; implicit method calls like x inside to_s were TY_UNKNOWN.

analyze.c: add Struct#[] return type inference for symbol/integer keys, resolving to specific member type or TY_POLY for general case.

codegen.c: emit Struct#[] as direct field access for literal symbol or integer keys, and a comparison chain for dynamic keys.

Refs #994

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

Details

Commit
211c332e7682
Parent
b57676d05e4a

src/analyze.cmodifiedView file

@@ -802,6 +802,20 @@
802802 return TY_POLY;
803803 }
804804 }
805+ if (!strcmp(name, "[]") && argc == 1) {
806+ /* struct[:sym] or struct[int]: return specific member type if known */
807+ int mi = struct_member_idx(c, sc, argv[0]);
808+ if (mi >= 0) return sc->ivar_types[mi];
809+ /* integer index: try to resolve literal */
810+ const char *kty = nt_type(nt, argv[0]);
811+ if (kty && !strcmp(kty, "IntegerNode")) {
812+ long long idx = (long long)nt_int(nt, argv[0], "value", 0);
813+ if (idx < 0) idx += (long long)sc->nivars;
814+ if (idx >= 0 && idx < sc->nivars) return sc->ivar_types[(int)idx];
815+ }
816+ return TY_POLY;
817+ }
818+ if (!strcmp(name, "[]=") && argc == 2) return sc->nivars > 0 ? sc->ivar_types[0] : TY_POLY;
805819 }
806820
807821 /* built-in class reopening: look up user-defined methods on scalar built-in types */
@@ -2479,6 +2493,41 @@
24792493 }
24802494 }
24812495
2496+/* Fix scope class_id for DefNodes inside Struct.new { } blocks.
2497+ walk_scope runs before register_structs, so defs in struct blocks get
2498+ class_id=-1. This pass corrects them after the class is registered. */
2499+static void fix_struct_block_scopes(Compiler *c) {
2500+ const NodeTable *nt = c->nt;
2501+ for (int id = 0; id < nt->count; id++) {
2502+ const char *ty = nt_type(nt, id);
2503+ if (!ty || strcmp(ty, "ConstantWriteNode")) continue;
2504+ const char *cname = nt_str(nt, id, "name");
2505+ int val = nt_ref(nt, id, "value");
2506+ if (!cname || val < 0 || !is_struct_call(c, val)) continue;
2507+ int blk = nt_ref(nt, val, "block");
2508+ if (blk < 0) continue;
2509+ int ci = comp_class_index(c, cname);
2510+ if (ci < 0) continue;
2511+ /* Walk the block body and fix any DefNode scopes */
2512+ int bbody = nt_ref(nt, blk, "body");
2513+ if (bbody < 0) continue;
2514+ int bn = 0;
2515+ const int *stmts = nt_arr(nt, bbody, "body", &bn);
2516+ for (int k = 0; k < bn; k++) {
2517+ const char *sty = nt_type(nt, stmts[k]);
2518+ if (!sty || strcmp(sty, "DefNode")) continue;
2519+ int dn = stmts[k];
2520+ /* Find the scope whose def_node == dn and fix its class_id */
2521+ for (int s = 0; s < c->nscopes; s++) {
2522+ if (c->scopes[s].def_node == dn) {
2523+ c->scopes[s].class_id = ci;
2524+ break;
2525+ }
2526+ }
2527+ }
2528+ }
2529+}
2530+
24822531 /* Process attr_accessor/reader/writer call: register ivars + reader/writer names.
24832532 If `singleton` is non-zero, registers singleton (class-level) accessors instead. */
24842533 static void register_attr_call(Compiler *c, ClassInfo *cls, int s, int singleton) {
@@ -5903,6 +5952,7 @@
59035952
59045953 walk_scope(c, c->nt->root_id, 0, -1);
59055954 register_structs(c);
5955+ fix_struct_block_scopes(c);
59065956 register_module_functions(c);
59075957 register_locals(c);
59085958 register_attrs(c);