Commit 1468a90a
1468a90ac9ecMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Infer @ivar array/hash element types from fill usage; array << as expression
Two complementary fixes for the @buf = []; @buf << x pattern:
- analyze: the container-usage fold that gives an empty local its element
type from
<</push/[]=now also applies to instance variables, so an empty@buffilled by@buf << xinfers int_array (etc.). - codegen:
arr << x/ push / append in value position (e.g. as a method's tail expression) lowers to a statement-expression that pushes and yields the array, instead of falling through to an unresolved call. Statement position is unchanged (emit_array_mutate_stmt).
make test: 181 pass / 5 fail / 658 error (was 179 / 5 / 660), no regressions.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 1468a90ac9ec
- Parent
- b6427d2e9977
src/analyze.cmodifiedView file
@@ -1350,34 +1350,53 @@
13501350
else {13511351
continue;13521352
}1353-
if (recv < 0 || strcmp(nt_type(nt, recv) ? nt_type(nt, recv) : "", "LocalVariableReadNode")) continue;1354-
const char *rnm = nt_str(nt, recv, "name");1355-
LocalVar *lv = rnm ? scope_local(comp_scope_of(c, recv), rnm) : NULL;1356-
if (!lv || lv->is_param || lv->is_block_param) continue;1353+
if (recv < 0) continue;1354+
const char *rty = nt_type(nt, recv);1355+
/* fold into a local's type or an ivar's type (an empty `@buf=[]` filled by1356+
`@buf << x` infers its element type the same way a local does) */1357+
TyKind *slot = NULL;1358+
if (rty && !strcmp(rty, "LocalVariableReadNode")) {1359+
const char *rnm = nt_str(nt, recv, "name");1360+
LocalVar *lv = rnm ? scope_local(comp_scope_of(c, recv), rnm) : NULL;1361+
if (!lv || lv->is_param || lv->is_block_param) continue;1362+
slot = &lv->type;1363+
}1364+
else if (rty && !strcmp(rty, "InstanceVariableReadNode")) {1365+
const char *inm = nt_str(nt, recv, "name");1366+
Scope *s = comp_scope_of(c, recv);1367+
if (s->class_id < 0) continue;1368+
ClassInfo *ci = &c->classes[s->class_id];1369+
int iv = inm ? comp_ivar_index(ci, inm) : -1;1370+
if (iv < 0) continue;1371+
slot = &ci->ivar_types[iv];1372+
}1373+
else continue;13571374
1375+
TyKind before = *slot;13581376
if (is_push || kt == TY_INT) {13591377
/* array (a known string's `<<` is append, not push -- don't pollute) */13601378
if (vt == TY_UNKNOWN) continue;1361-
if (lv->type != TY_UNKNOWN && !ty_is_array(lv->type)) continue;1362-
lv->type = ty_unify(lv->type, ty_array_of(vt));1379+
if (*slot != TY_UNKNOWN && !ty_is_array(*slot)) continue;1380+
*slot = ty_unify(*slot, ty_array_of(vt));13631381
}13641382
else if (kt == TY_STRING) {13651383
if (vt == TY_UNKNOWN) continue;13661384
TyKind hv = ty_hash_of(TY_STRING, vt);13671385
if (hv == TY_UNKNOWN) hv = TY_STR_POLY_HASH; /* mixed values */1368-
if (lv->type != TY_UNKNOWN && !ty_is_hash(lv->type)) continue;1386+
if (*slot != TY_UNKNOWN && !ty_is_hash(*slot)) continue;13691387
/* a str-keyed hash that has seen >1 value type widens to StrPoly */1370-
if (lv->type != TY_UNKNOWN && lv->type != hv &&1371-
(lv->type == TY_STR_INT_HASH || lv->type == TY_STR_STR_HASH || lv->type == TY_STR_POLY_HASH))1388+
if (*slot != TY_UNKNOWN && *slot != hv &&1389+
(*slot == TY_STR_INT_HASH || *slot == TY_STR_STR_HASH || *slot == TY_STR_POLY_HASH))13721390
hv = TY_STR_POLY_HASH;1373-
lv->type = hv;1391+
*slot = hv;13741392
}13751393
else if (kt == TY_SYMBOL) {13761394
/* symbol key -> SymPolyHash (boxed values) */13771395
if (vt == TY_UNKNOWN) continue;1378-
if (lv->type != TY_UNKNOWN && lv->type != TY_SYM_POLY_HASH) continue;1379-
lv->type = TY_SYM_POLY_HASH;1396+
if (*slot != TY_UNKNOWN && *slot != TY_SYM_POLY_HASH) continue;1397+
*slot = TY_SYM_POLY_HASH;13801398
}1399+
if (*slot != before) changed = 1;13811400
}13821401
13831402
/* detect change vs the stashed old types */src/codegen.cmodifiedView file
Loading diff…