Commit 6bb06657

6bb066577fccMerged

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

Description

Multi-write constant type narrowing and TY_POLY static initializer fix

When a constant is assigned via multi-write with a block-returning RHS (e.g. P, Q = [1, 2].map { |n| n * 10 }), the fixpoint previously widened P/Q to TY_POLY during early iterations (when the block param type was still TY_UNKNOWN), and ty_unify then locked them there.

Adds infer_multiwrite_const_types(): a fixpoint pass that overrides the widened type once the array element type is settled and non-poly. It runs after infer_global_const_types so it sees fully-converged block param and return types.

Also fixes the TY_POLY static file-scope initializer: sp_box_nil() (a function call) is not valid as a C static initializer. Use {0} for both gvar and const TY_POLY declarations, matching how ivar initializers are already generated.

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

Details

Commit
6bb066577fcc
Parent
1ee517c8458b

src/analyze.cmodifiedView file

@@ -1621,6 +1621,12 @@
16211621 const char *nm = nt_str(nt, id, "name");
16221622 if (nm && nm[0] == '$' && is_c_ident(nm + 1)) comp_gvar_intern(c, nm + 1);
16231623 }
1624+ else if (!strcmp(ty, "ConstantTargetNode")) {
1625+ /* target in a multi-write: A, B = expr */
1626+ const char *nm = nt_str(nt, id, "name");
1627+ if (nm && is_c_ident(nm) && comp_class_index(c, nm) < 0)
1628+ comp_const_intern(c, nm);
1629+ }
16241630 else if (!strcmp(ty, "ConstantWriteNode")) {
16251631 const char *nm = nt_str(nt, id, "name");
16261632 /* a constant bound to a regex literal is resolved at compile time to a
@@ -1688,6 +1694,48 @@
16881694 return changed;
16891695 }
16901696
1697+/* Re-infer constants assigned via multi-write with a call/variable RHS.
1698+ The existing infer_write_types pass widened them to TY_POLY early (before
1699+ block params converged); this pass overrides with the now-stable element
1700+ type once it is known and not poly. */
1701+static int infer_multiwrite_const_types(Compiler *c) {
1702+ const NodeTable *nt = c->nt;
1703+ int changed = 0;
1704+ for (int id = 0; id < nt->count; id++) {
1705+ const char *ty = nt_type(nt, id);
1706+ if (!ty || strcmp(ty, "MultiWriteNode")) continue;
1707+ int value = nt_ref(nt, id, "value");
1708+ if (value < 0) continue;
1709+ const char *vty = nt_type(nt, value);
1710+ if (vty && !strcmp(vty, "ArrayNode")) continue; /* literal handled in infer_write_types */
1711+ TyKind st = infer_type(c, value);
1712+ if (!ty_is_array(st)) continue;
1713+ TyKind elem = ty_array_elem(st);
1714+ if (elem == TY_POLY || elem == TY_UNKNOWN) continue; /* not yet settled */
1715+ int ln = 0;
1716+ const int *lefts = nt_arr(nt, id, "lefts", &ln);
1717+ for (int i = 0; i < ln; i++) {
1718+ const char *lty = nt_type(nt, lefts[i]) ? nt_type(nt, lefts[i]) : "";
1719+ if (strcmp(lty, "ConstantTargetNode")) continue;
1720+ const char *nm = nt_str(nt, lefts[i], "name");
1721+ LocalVar *cv = nm ? comp_const(c, nm) : NULL;
1722+ if (!cv || cv->type == elem) continue;
1723+ cv->type = elem; changed = 1;
1724+ }
1725+ int rn = 0;
1726+ const int *rights = nt_arr(nt, id, "rights", &rn);
1727+ for (int j = 0; j < rn; j++) {
1728+ const char *rty2 = nt_type(nt, rights[j]) ? nt_type(nt, rights[j]) : "";
1729+ if (strcmp(rty2, "ConstantTargetNode")) continue;
1730+ const char *nm = nt_str(nt, rights[j], "name");
1731+ LocalVar *cv = nm ? comp_const(c, nm) : NULL;
1732+ if (!cv || cv->type == elem) continue;
1733+ cv->type = elem; changed = 1;
1734+ }
1735+ }
1736+ return changed;
1737+}
1738+
16911739 /* Resolve each class's superclass index from its ClassNode. */
16921740 static void resolve_parents(Compiler *c) {
16931741 const NodeTable *nt = c->nt;
@@ -1959,8 +2007,8 @@
19592007 }
19602008 }
19612009 }
1962- /* call/super/yield returning a typed array: assign element types to targets */
1963- if (multi_src && value >= 0) {
2010+ /* any expression returning a typed array: assign element types to targets */
2011+ if (value >= 0) {
19642012 TyKind st = infer_type(c, value);
19652013 if (ty_is_array(st)) {
19662014 TyKind elem = ty_array_elem(st);
@@ -1985,6 +2033,13 @@
19852033 c->classes[ms_arr->class_id].ivar_types[iv_ms] = mg; changed = 1;
19862034 }
19872035 }
2036+ else if (!strcmp(lty_ms, "ConstantTargetNode")) {
2037+ const char *cnm_ms = nt_str(nt, lefts[i], "name");
2038+ LocalVar *cv_ms = cnm_ms ? comp_const(c, cnm_ms) : NULL;
2039+ if (!cv_ms) continue;
2040+ TyKind mg_ms = ty_unify(cv_ms->type, elem);
2041+ if (mg_ms != cv_ms->type) { cv_ms->type = mg_ms; changed = 1; }
2042+ }
19882043 }
19892044 for (int j = 0; j < rn2; j++) {
19902045 const char *lty_ms = nt_type(nt, rights2[j]) ? nt_type(nt, rights2[j]) : "";
@@ -2004,6 +2059,13 @@
20042059 c->classes[ms_arr->class_id].ivar_types[iv_ms2] = mg2; changed = 1;
20052060 }
20062061 }
2062+ else if (!strcmp(lty_ms, "ConstantTargetNode")) {
2063+ const char *cnm_ms2 = nt_str(nt, rights2[j], "name");
2064+ LocalVar *cv_ms2 = cnm_ms2 ? comp_const(c, cnm_ms2) : NULL;
2065+ if (!cv_ms2) continue;
2066+ TyKind mg_ms2 = ty_unify(cv_ms2->type, elem);
2067+ if (mg_ms2 != cv_ms2->type) { cv_ms2->type = mg_ms2; changed = 1; }
2068+ }
20072069 }
20082070 int rest_nid2 = nt_ref(nt, id, "rest");
20092071 if (rest_nid2 >= 0) {
@@ -2036,6 +2098,15 @@
20362098 if (!lv || lv->is_param || lv->is_block_param) continue;
20372099 lv->type = ty_unify(lv->type, et);
20382100 }
2101+ else if (!strcmp(lty, "ConstantTargetNode")) {
2102+ const char *cnm = nt_str(nt, lefts[i], "name");
2103+ LocalVar *cv = cnm ? comp_const(c, cnm) : NULL;
2104+ if (!cv) continue;
2105+ TyKind et = infer_type(c, els[i]);
2106+ if (et == TY_NIL) continue;
2107+ TyKind mg = ty_unify(cv->type, et);
2108+ if (mg != cv->type) { cv->type = mg; changed = 1; }
2109+ }
20392110 else if (!strcmp(lty, "MultiTargetNode")) {
20402111 /* (b, c) nested target: inner RHS must be an ArrayNode literal */
20412112 const char *ety = nt_type(nt, els[i]);
@@ -2063,13 +2134,22 @@
20632134 int ridx = en - rn + j;
20642135 if (ridx < 0 || ridx >= en) continue;
20652136 const char *rty3 = nt_type(nt, rights[j]);
2066- if (!rty3 || strcmp(rty3, "LocalVariableTargetNode")) continue;
2067- const char *rnm2 = nt_str(nt, rights[j], "name");
2137+ if (!rty3) continue;
20682138 TyKind et = infer_type(c, els[ridx]);
20692139 if (et == TY_NIL) continue;
2070- LocalVar *lv = rnm2 ? scope_local(comp_scope_of(c, id), rnm2) : NULL;
2071- if (!lv || lv->is_param || lv->is_block_param) continue;
2072- lv->type = ty_unify(lv->type, et);
2140+ if (!strcmp(rty3, "LocalVariableTargetNode")) {
2141+ const char *rnm2 = nt_str(nt, rights[j], "name");
2142+ LocalVar *lv = rnm2 ? scope_local(comp_scope_of(c, id), rnm2) : NULL;
2143+ if (!lv || lv->is_param || lv->is_block_param) continue;
2144+ lv->type = ty_unify(lv->type, et);
2145+ }
2146+ else if (!strcmp(rty3, "ConstantTargetNode")) {
2147+ const char *cnm2 = nt_str(nt, rights[j], "name");
2148+ LocalVar *cv2 = cnm2 ? comp_const(c, cnm2) : NULL;
2149+ if (!cv2) continue;
2150+ TyKind mg3 = ty_unify(cv2->type, et);
2151+ if (mg3 != cv2->type) { cv2->type = mg3; changed = 1; }
2152+ }
20732153 }
20742154 /* rest (splat) target: elements [ln, en-rn) become a typed array */
20752155 int rest_nid = nt_ref(nt, id, "rest");
@@ -3234,6 +3314,7 @@
32343314 ch |= infer_cvar_types(c);
32353315 ch |= infer_inherited_ivars(c);
32363316 ch |= infer_global_const_types(c);
3317+ ch |= infer_multiwrite_const_types(c);
32373318 ch |= infer_return_types(c);
32383319 if (!ch) break;
32393320 }