Commit aa049e24

aa049e24057bMerged

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

Description

Fix @ivar type pollution when ivar has both direct init and element mutation

In infer_write_types, when processing @buf[0] = -1 style element mutations, if the slot is TY_UNKNOWN but the ivar has a direct InstanceVariableWriteNode with a typed (non-empty) RHS (e.g. @buf = [nil] * 7), skip the usage-driven hash/array promotion.

Previously: iteration 1 ran infer_write_types before infer_ivar_types, so @buf was TY_UNKNOWN when @buf[0] = -1 ran, promoting it to TY_INT_INT_HASH. Then infer_ivar_types unified TY_INT_INT_HASH with TY_POLY_ARRAY -> TY_POLY. TY_POLY became the stable fixpoint, breaking @buf[1, 4] = @table[a][i] (3-arg []= needs ty_is_array(recv)).

Also fix 3-arg []= return type in analyze: it returns argv[2] (the src), not argv[0] (the start index). The old code typed fill() as returning mrb_int, causing a C compile error when the 3-arg []= expression value is sp_RbVal.

Fixes three_level_array_outer_index.

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

Details

Commit
aa049e24057b
Parent
75d776f9f88c

src/analyze.cmodifiedView file

@@ -1254,7 +1254,7 @@
12541254 if (!strcmp(name, "shift") && argc == 0) return ty_array_elem(rt);
12551255 if (!strcmp(name, "slice!") && argc == 2) return rt; /* removed subarray */
12561256 if (!strcmp(name, "[]=") && argc == 2) return ty_array_elem(rt);
1257- if (!strcmp(name, "[]=") && argc == 3) return a0 != TY_UNKNOWN ? a0 : rt;
1257+ if (!strcmp(name, "[]=") && argc == 3) return infer_type(c, argv[2]);
12581258 if ((!strcmp(name, "assoc") || !strcmp(name, "rassoc")) && rt == TY_POLY_ARRAY)
12591259 return TY_POLY_ARRAY; /* the matching sub-array, or nil (NULL ptr) */
12601260 if (!strcmp(name, "to_h") && argc == 0 && block < 0) {
@@ -3711,6 +3711,26 @@
37113711 int iv = inm ? comp_ivar_index(ci, inm) : -1;
37123712 if (iv < 0) continue;
37133713 slot = &ci->ivar_types[iv];
3714+ /* If the slot is TY_UNKNOWN but has a direct non-empty InstanceVariableWriteNode
3715+ (e.g. @buf = [nil]*7), skip usage-driven promotion — infer_ivar_types will
3716+ set the correct type from the direct assignment. Without this guard,
3717+ @buf[0] = -1 in the first fixpoint iteration promotes @buf to an int hash
3718+ before @buf = [nil]*7 has been processed, causing ty_unify to widen to TY_POLY. */
3719+ if (*slot == TY_UNKNOWN && inm) {
3720+ int has_typed_write = 0;
3721+ for (int _wi = 0; _wi < nt->count && !has_typed_write; _wi++) {
3722+ if (!nt_type(nt, _wi) || strcmp(nt_type(nt, _wi), "InstanceVariableWriteNode")) continue;
3723+ const char *_wnm = nt_str(nt, _wi, "name");
3724+ if (!_wnm || strcmp(_wnm, inm)) continue;
3725+ Scope *_ws = comp_scope_of(c, _wi);
3726+ if (!_ws || _ws->class_id != s->class_id) continue;
3727+ int _wval = nt_ref(nt, _wi, "value");
3728+ if (_wval < 0) continue;
3729+ TyKind _wt = infer_type(c, _wval);
3730+ if (_wt != TY_UNKNOWN && _wt != TY_NIL) has_typed_write = 1;
3731+ }
3732+ if (has_typed_write) continue;
3733+ }
37143734 }
37153735 else if (is_push && rty && !strcmp(rty, "CallNode")) {
37163736 /* `getter_method << x` where getter returns @ivar: trace through