Commit 972cb88b

972cb88b8079Merged

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

Description

Type a lambda param from its call site before the int default

A lambda parameter (->(t) { ... }) lives in the enclosing scope and was defaulted to int up front, before the call-site arg-type seeding ran. So a ->(t){[t]} later called as f.call("x") unified the premature int default with the string arg into poly -- and a poly param doesn't fit the int-only proc-arg ABI, so the body emitted sp_RbVal lv_t = args[0] (invalid C). A proc literal (proc {|t|...}) was already correct because its int default is applied after the seeding.

Register lambda params first but defer their int default to a pass that runs after the call-site seeding, so a typed call wins and the int default only fills params no call site typed (the arithmetic-lambda fallback). Mirrors the proc-literal ordering.

Fixes #1372

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
972cb88b8079
Parent
1ce1cf3e7444

src/analyze_pass.cmodifiedView file

@@ -1845,8 +1845,10 @@
18451845 int changed = 0;
18461846
18471847 /* `->(x, ...) {}` (LambdaNode): its params live in the enclosing scope (no
1848- separate scope), like block params. Register and type them; default to
1849- int (the proc-literal slice default) until call-site arg inference lands. */
1848+ separate scope), like block params. Register them here; the int default is
1849+ applied later, AFTER the call-site arg-type seeding below, so a `->(t){...}`
1850+ later called as `f.call("x")` types `t` from the call (string) instead of
1851+ unifying a premature int default with it into poly (#1372). */
18501852 for (int id = 0; id < nt->count; id++) {
18511853 const char *ty = nt_type(nt, id);
18521854 if (!ty || strcmp(ty, "LambdaNode")) continue;
@@ -1858,7 +1860,6 @@
18581860 const char *p = nt_str(nt, reqs[k], "name");
18591861 if (!p) continue;
18601862 LocalVar *lv = scope_local_intern(bs, p); lv->is_block_param = 1;
1861- if (lv->type == TY_UNKNOWN) { lv->type = TY_INT; changed = 1; }
18621863 }
18631864 }
18641865
@@ -2096,6 +2097,24 @@
20962097 }
20972098 }
20982099
2100+ /* Lambda param int default, applied AFTER the call-site seeding above so it
2101+ only fills params no call site typed -- the arithmetic-proc fallback,
2102+ matching the proc-literal default loop below (#1372). */
2103+ for (int id = 0; id < nt->count; id++) {
2104+ const char *ty = nt_type(nt, id);
2105+ if (!ty || strcmp(ty, "LambdaNode")) continue;
2106+ int pn = nt_ref(nt, id, "parameters");
2107+ if (pn < 0) continue;
2108+ int rn = 0; const int *reqs = nt_arr(nt, pn, "requireds", &rn);
2109+ Scope *bs = comp_scope_of(c, id);
2110+ for (int k = 0; k < rn; k++) {
2111+ const char *p = nt_str(nt, reqs[k], "name");
2112+ if (!p) continue;
2113+ LocalVar *lv = scope_local(bs, p);
2114+ if (lv && lv->type == TY_UNKNOWN) { lv->type = TY_INT; changed = 1; }
2115+ }
2116+ }
2117+
20992118 for (int id = 0; id < nt->count; id++) {
21002119 const char *ty = nt_type(nt, id);
21012120 if (!ty || strcmp(ty, "CallNode")) continue;