Commit 599e0da6

599e0da6e4e7Merged

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

Description

Fix block params beyond yield arity typed as TY_UNKNOWN

In infer_block_params, after binding yield args to block params:

  1. Scan all yields in the method to find the minimum arity. Block params at index >= min_arity can receive nil from any yield, so widen them to TY_POLY. This handles both yield (0 args) with multi-param blocks AND multi-yield methods where some yields provide fewer args than the block expects.

  2. In emit_block_invoke (codegen), when a block param is TY_POLY but the yield arg is a scalar (int/string/etc), box the value before assigning, matching emit_iter_param_assign's behavior.

Together these fix i997 (yield arity mismatch, e.g. show2 yields nothing but block has |a, b|, or multi yields 1 or 2 args with block |p, q|).

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

Details

Commit
599e0da6e4e7
Parent
aa049e24057b

src/analyze.cmodifiedView file

@@ -4530,6 +4530,25 @@
45304530 TyKind m = ty_unify(lv->type, infer_type(c, yargs[k]));
45314531 if (m != lv->type) { lv->type = m; changed = 1; }
45324532 }
4533+ /* Params beyond the first yield's arity might still be nil if there
4534+ are other yields with fewer args. Find the min yield arity. */
4535+ int min_yc = yc;
4536+ for (int _yi = 0; _yi < nt->count; _yi++) {
4537+ if (!nt_type(nt, _yi) || strcmp(nt_type(nt, _yi), "YieldNode")) continue;
4538+ if (c->nscope[_yi] != mi) continue;
4539+ int _ya = nt_ref(nt, _yi, "arguments");
4540+ int _yc = 0;
4541+ if (_ya >= 0) nt_arr(nt, _ya, "arguments", &_yc);
4542+ if (_yc < min_yc) min_yc = _yc;
4543+ }
4544+ /* Block params at index >= min_yc can receive nil — widen to poly. */
4545+ for (int k = min_yc; ; k++) {
4546+ const char *bp = block_param_name(c, block, k);
4547+ if (!bp) break;
4548+ LocalVar *lv = scope_local_intern(bs, bp); lv->is_block_param = 1;
4549+ TyKind m = ty_unify(lv->type, TY_POLY);
4550+ if (m != lv->type) { lv->type = m; changed = 1; }
4551+ }
45334552 continue;
45344553 }
45354554 }