Commit 4a2c4a5c

4a2c4a5c9aa3Merged

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

Description

Infer class method block params + ivar hash write type propagation

Three related type inference improvements:

  1. infer_block_params: handle ConstantReadNode.classMethod { |x| } case. When the block receiver is a ConstantReadNode (e.g. Runner.run), look up the class method via comp_cmethod_in_chain so block params are typed from the yield arg types. Previously only instance methods and .new were handled.

  2. infer_params_from_ivar_hash_ops: new post-fixpoint pass that infers method param types from ivar hash subscript operations in the body. For def []=(key, val); @h[key] = val; end where @h is a typed hash, key and val get inferred from the hash's key/value types. Fixes methods like custom []= that are BFS-reachable but have no typed call sites.

  3. is_idx_write guard: the !is_push guard blocking hash promotion from ivar[] reads was also blocking @h[k]=v writes from updating @h's type. Add is_idx_write flag (set for CallNode []= and IndexOperatorWriteNode) and exempt writes from the guard, so writes always propagate through.

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

Details

Commit
4a2c4a5c9aa3
Parent
9c0c192be53a

src/analyze.cmodifiedView file

@@ -3629,7 +3629,7 @@
36293629 for (int id = 0; id < nt->count; id++) {
36303630 const char *ty = nt_type(nt, id);
36313631 if (!ty) continue;
3632- int recv, kt = TY_UNKNOWN, vt = TY_UNKNOWN, is_push = 0;
3632+ int recv, kt = TY_UNKNOWN, vt = TY_UNKNOWN, is_push = 0, is_idx_write = 0;
36333633 if (!strcmp(ty, "CallNode")) {
36343634 recv = nt_ref(nt, id, "receiver");
36353635 const char *name = nt_str(nt, id, "name");
@@ -3640,7 +3640,7 @@
36403640 is_push = 1; vt = infer_type(c, argv[0]);
36413641 }
36423642 else if (name && !strcmp(name, "[]=") && an == 2) {
3643- kt = infer_type(c, argv[0]); vt = infer_type(c, argv[1]);
3643+ is_idx_write = 1; kt = infer_type(c, argv[0]); vt = infer_type(c, argv[1]);
36443644 }
36453645 else if (name && (!strcmp(name, "fetch") || !strcmp(name, "[]")) && an >= 1) {
36463646 /* hash.fetch(key,..) / hash[key]: promote TY_UNKNOWN local to a typed hash.
@@ -3688,6 +3688,7 @@
36883688 else continue;
36893689 }
36903690 else if (!strcmp(ty, "IndexOperatorWriteNode")) {
3691+ is_idx_write = 1;
36913692 recv = nt_ref(nt, id, "receiver");
36923693 int args = nt_ref(nt, id, "arguments");
36933694 int an = 0;
@@ -3723,7 +3724,7 @@
37233724 (but allow push-driven array promotion through). Without this guard,
37243725 @free[0] read promotes @free to poly_poly_hash before @free = []
37253726 has been processed as an array. */
3726- if (!is_push && *slot == TY_UNKNOWN && inm) {
3727+ if (!is_push && !is_idx_write && *slot == TY_UNKNOWN && inm) {
37273728 int has_typed_write = 0;
37283729 for (int _wi = 0; _wi < nt->count && !has_typed_write; _wi++) {
37293730 if (!nt_type(nt, _wi) || strcmp(nt_type(nt, _wi), "InstanceVariableWriteNode")) continue;
@@ -3987,6 +3988,64 @@
39873988 return 0;
39883989 }
39893990
3991+/* Infer still-unknown params from ivar hash operations in the method body.
3992+ For `def []=(key, val); @h[key] = val; end` where @h is a known hash type,
3993+ infer key/val from the hash's key/value types. Also handles `[]` reads.
3994+ Runs post-fixpoint so ivar types are stable before this fires. */
3995+static int infer_params_from_ivar_hash_ops(Compiler *c) {
3996+ const NodeTable *nt = c->nt;
3997+ int changed = 0;
3998+ for (int id = 0; id < nt->count; id++) {
3999+ const char *ty = nt_type(nt, id);
4000+ if (!ty || strcmp(ty, "CallNode")) continue;
4001+ const char *name = nt_str(nt, id, "name");
4002+ if (!name) continue;
4003+ int is_set = !strcmp(name, "[]=");
4004+ int is_get = !strcmp(name, "[]");
4005+ if (!is_set && !is_get) continue;
4006+ int recv = nt_ref(nt, id, "receiver");
4007+ if (recv < 0) continue;
4008+ const char *rty = nt_type(nt, recv);
4009+ if (!rty || strcmp(rty, "InstanceVariableReadNode")) continue;
4010+ const char *inm = nt_str(nt, recv, "name");
4011+ if (!inm) continue;
4012+ Scope *s = comp_scope_of(c, id);
4013+ if (!s || s->class_id < 0) continue;
4014+ ClassInfo *ci = &c->classes[s->class_id];
4015+ int iv = comp_ivar_index(ci, inm);
4016+ if (iv < 0) continue;
4017+ TyKind ht = ci->ivar_types[iv];
4018+ if (!ty_is_hash(ht) || ht == TY_POLY_POLY_HASH) continue;
4019+ TyKind hk = ty_hash_key(ht);
4020+ TyKind hv = ty_hash_val(ht);
4021+ int args = nt_ref(nt, id, "arguments");
4022+ int an = 0;
4023+ const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
4024+ /* [](key) => key is hash key type; []=(key, val) => key + val */
4025+ if (an >= 1 && argv && hk != TY_UNKNOWN) {
4026+ const char *aty = nt_type(nt, argv[0]);
4027+ if (aty && !strcmp(aty, "LocalVariableReadNode")) {
4028+ const char *anm = nt_str(nt, argv[0], "name");
4029+ LocalVar *lv = anm ? scope_local(s, anm) : NULL;
4030+ if (lv && lv->is_param && lv->type == TY_UNKNOWN) {
4031+ lv->type = hk; changed = 1;
4032+ }
4033+ }
4034+ }
4035+ if (is_set && an >= 2 && argv && hv != TY_UNKNOWN) {
4036+ const char *aty = nt_type(nt, argv[1]);
4037+ if (aty && !strcmp(aty, "LocalVariableReadNode")) {
4038+ const char *anm = nt_str(nt, argv[1], "name");
4039+ LocalVar *lv = anm ? scope_local(s, anm) : NULL;
4040+ if (lv && lv->is_param && lv->type == TY_UNKNOWN) {
4041+ lv->type = hv; changed = 1;
4042+ }
4043+ }
4044+ }
4045+ }
4046+ return changed;
4047+}
4048+
39904049 /* Infer a still-unknown parameter as a typed hash when the body indexes
39914050 it with a literal key: `param["key"]` → str_poly_hash,
39924051 `param[:sym]` → sym_poly_hash. Runs in the fixpoint alongside
@@ -4535,6 +4594,12 @@
45354594 int cid = cname ? comp_class_index(c, cname) : -1;
45364595 if (cid >= 0) mi = comp_method_in_chain(c, cid, "initialize", NULL);
45374596 }
4597+ /* Class.method { ... }: look up the class method */
4598+ if (mi < 0 && nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ConstantReadNode")) {
4599+ const char *cname = nt_str(nt, recv, "name");
4600+ int cid = cname ? comp_class_index(c, cname) : -1;
4601+ if (cid >= 0) mi = comp_cmethod_in_chain(c, cid, name, NULL);
4602+ }
45384603 }
45394604 if (mi >= 0 && c->scopes[mi].yields) {
45404605 int yn = first_yield(c, mi);
@@ -5754,7 +5819,7 @@
57545819 from how it is used inside the method body (hash subscript patterns,
57555820 array-specific calls). Runs after the main fixpoint so caller-side
57565821 types always win; the mini-loop below propagates the new types. */
5757- if (infer_hash_params(c) | infer_array_params(c)) {
5822+ if (infer_hash_params(c) | infer_array_params(c) | infer_params_from_ivar_hash_ops(c)) {
57585823 for (int iter = 0; iter < 16; iter++) {
57595824 int ch = 0;
57605825 ch |= infer_param_types(c);