Commit 51027269

510272696d02Merged

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

Description

Default an evidence-free empty-array ivar to int_array

An ivar assigned only an empty array literal (no element type from usage) stayed UNKNOWN and fell back to a scalar int struct field, mismatching the sp_IntArray_new() the write emits. A post-fixpoint backstop now types such a slot as an (empty) int array so the field is a pointer.

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

Details

Commit
510272696d02
Parent
a0ddc1f2df92

src/analyze.cmodifiedView file

@@ -2234,6 +2234,25 @@
22342234 if (p && p->type == TY_UNKNOWN) p->type = TY_POLY;
22352235 }
22362236 }
2237+
2238+ /* Backstop: an ivar assigned only an empty array literal (no element
2239+ evidence from usage) is left UNKNOWN, which falls back to int and a
2240+ scalar struct field. Default such a slot to an (empty) int array so the
2241+ field is a pointer matching the emitted sp_IntArray_new(). */
2242+ for (int id = 0; id < c->nt->count; id++) {
2243+ const char *ty = nt_type(c->nt, id);
2244+ if (!ty || strcmp(ty, "InstanceVariableWriteNode")) continue;
2245+ int v = nt_ref(c->nt, id, "value");
2246+ const char *vty = v >= 0 ? nt_type(c->nt, v) : NULL;
2247+ if (!vty || strcmp(vty, "ArrayNode")) continue;
2248+ int en = 0; nt_arr(c->nt, v, "elements", &en);
2249+ if (en != 0) continue;
2250+ Scope *s = comp_scope_of(c, id);
2251+ if (s->class_id < 0) continue;
2252+ ClassInfo *ci = &c->classes[s->class_id];
2253+ int iv = comp_ivar_index(ci, nt_str(c->nt, id, "name"));
2254+ if (iv >= 0 && ci->ivar_types[iv] == TY_UNKNOWN) ci->ivar_types[iv] = TY_INT_ARRAY;
2255+ }
22372256 /* recompute returns: a method returning such a param is now poly */
22382257 for (int iter = 0; iter < 8; iter++) if (!infer_return_types(c)) break;
22392258