Commit d43bdc75

d43bdc756ba3Merged

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

Description

Fix module constant array type inference and initialization codegen

analyze.c: extend infer_global_const_types to detect CONST << v / CONST.push(v) / CONST.append(v) patterns and infer the constant's array type from the pushed element type, so LOG = [] followed by LOG << hash_entry produces sp_PolyArray instead of the default sp_IntArray.

codegen.c: fix ConstantWriteNode / GlobalVariableWriteNode empty array/hash initialization to use the type-appropriate constructor (sp_PolyArray_new, sp_StrArray_new, sp_IntIntHash_new, etc.) when the constant's inferred type is already known, mirroring the existing ivar write logic.

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

Details

Commit
d43bdc756ba3
Parent
39d081dc7046

src/analyze.cmodifiedView file

@@ -2036,6 +2036,31 @@
20362036 }
20372037 continue;
20382038 }
2039+ else if (!strcmp(ty, "CallNode")) {
2040+ /* CONST << v / CONST.push(v) / CONST.append(v): infer CONST as an
2041+ array whose element type comes from v's type. Only applies when
2042+ the receiver is a direct ConstantReadNode. */
2043+ const char *cnm = nt_str(nt, id, "name");
2044+ if (!cnm) continue;
2045+ int is_push = (!strcmp(cnm, "<<") || !strcmp(cnm, "push") || !strcmp(cnm, "append"));
2046+ if (!is_push) continue;
2047+ int crecv = nt_ref(nt, id, "receiver");
2048+ if (crecv < 0) continue;
2049+ const char *rty = nt_type(nt, crecv);
2050+ if (!rty || strcmp(rty, "ConstantReadNode")) continue;
2051+ const char *cnm2 = nt_str(nt, crecv, "name");
2052+ if (!cnm2) continue;
2053+ lv = comp_const(c, cnm2);
2054+ if (!lv || lv->type != TY_UNKNOWN) continue;
2055+ int cargs = nt_ref(nt, id, "arguments");
2056+ int cac = 0;
2057+ const int *cav = cargs >= 0 ? nt_arr(nt, cargs, "arguments", &cac) : NULL;
2058+ if (cac < 1 || !cav) continue;
2059+ TyKind et = infer_type(c, cav[0]);
2060+ if (et == TY_UNKNOWN || et == TY_NIL) continue;
2061+ vt = ty_array_of(et);
2062+ if (vt == TY_UNKNOWN) vt = TY_POLY_ARRAY;
2063+ }
20392064 else {
20402065 continue;
20412066 }