Commit 136c30b1

136c30b19e91Merged

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

Description

Fix block-param hash promotion: skip [] read folding when receiver has no write site

In infer_write_types, the container-usage folding code was promoting a local variable from TY_UNKNOWN to TY_STR_POLY_HASH whenever it saw row["k"], even if row was a pure block param with no write site in its scope. The promotion happened before infer_block_params set is_block_param=1, so ty_unify then saw TY_STR_POLY_HASH unified with the actual yield arg type TY_STR_STR_HASH and widened to TY_POLY, which can never narrow again.

Fix: after the rslot != TY_UNKNOWN guard, scan the AST for write nodes (LocalVariableWriteNode and variants) for the same name in the same scope. If none exist the variable is a pure block param; skip the [] read promotion and let infer_block_params handle it. Regular locals (h = {}; x = h["k"]) have a write site and continue to promote correctly.

Fixes yield_typed_hash_block_local (CC error -> pass).

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

Details

Commit
136c30b19e91
Parent
da4b80006edc

src/analyze.cmodifiedView file

@@ -2777,8 +2777,9 @@
27772777 Only fires when the slot is currently TY_UNKNOWN (empty hash). */
27782778 TyKind rslot = TY_UNKNOWN;
27792779 const char *rrty = nt_type(nt, recv);
2780+ const char *rnm2 = NULL;
27802781 if (rrty && !strcmp(rrty, "LocalVariableReadNode")) {
2781- const char *rnm2 = nt_str(nt, recv, "name");
2782+ rnm2 = nt_str(nt, recv, "name");
27822783 LocalVar *lv2 = rnm2 ? scope_local(comp_scope_of(c, recv), rnm2) : NULL;
27832784 if (lv2) rslot = lv2->type;
27842785 }
@@ -2786,6 +2787,28 @@
27862787 /* handled via slot below if it's TY_UNKNOWN */
27872788 }
27882789 if (rslot != TY_UNKNOWN) continue; /* already typed, skip */
2790+ /* Only promote via [] read if the receiver local has at least one
2791+ write site in its scope. Pure block params have no write site and
2792+ get their type from infer_block_params; promoting them here to
2793+ TY_STR_POLY_HASH before is_block_param is set creates a TY_POLY
2794+ that ty_unify can never narrow back to the yield arg type. */
2795+ if (rrty && !strcmp(rrty, "LocalVariableReadNode") && rnm2) {
2796+ Scope *recv_scope = comp_scope_of(c, recv);
2797+ int has_write = 0;
2798+ for (int _wi = 0; _wi < nt->count && !has_write; _wi++) {
2799+ const char *_wty = nt_type(nt, _wi);
2800+ if (!_wty) continue;
2801+ if ((!strcmp(_wty, "LocalVariableWriteNode") ||
2802+ !strcmp(_wty, "LocalVariableOrWriteNode") ||
2803+ !strcmp(_wty, "LocalVariableAndWriteNode") ||
2804+ !strcmp(_wty, "LocalVariableOperatorWriteNode")) &&
2805+ comp_scope_of(c, _wi) == recv_scope) {
2806+ const char *_wnm = nt_str(nt, _wi, "name");
2807+ if (_wnm && !strcmp(_wnm, rnm2)) has_write = 1;
2808+ }
2809+ }
2810+ if (!has_write) continue;
2811+ }
27892812 kt = infer_type(c, argv[0]);
27902813 if (kt == TY_SYMBOL) { vt = TY_INT; /* dummy: sym hash val is always poly */ }
27912814 else if (kt == TY_STRING) { vt = TY_POLY; /* will map to STR_POLY */ }
@@ -4252,6 +4275,7 @@
42524275 for (int id = 0; id < c->nt->count; id++)
42534276 infer_type(c, id);
42544277
4278+
42554279 /* Re-infer nodes inside instance_eval block bodies with the receiver's class
42564280 context, so ivar reads get correct types in the final c->ntype cache.
42574281 Call infer_type on each body statement: it recursively re-infers all