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
int changed = 0; /* `->(x, ...) {}` (LambdaNode): its params live in the enclosing scope (no separate scope), like block params. Register and type them; default to int (the proc-literal slice default) until call-site arg inference lands. */ separate scope), like block params. Register them here; the int default is applied later, AFTER the call-site arg-type seeding below, so a `->(t){...}` later called as `f.call("x")` types `t` from the call (string) instead of unifying a premature int default with it into poly (#1372). */ for (int id = 0; id < nt->count; id++) { const char *ty = nt_type(nt, id); if (!ty || strcmp(ty, "LambdaNode")) continue; const char *p = nt_str(nt, reqs[k], "name"); if (!p) continue; LocalVar *lv = scope_local_intern(bs, p); lv->is_block_param = 1; if (lv->type == TY_UNKNOWN) { lv->type = TY_INT; changed = 1; } } } } } /* Lambda param int default, applied AFTER the call-site seeding above so it only fills params no call site typed -- the arithmetic-proc fallback, matching the proc-literal default loop below (#1372). */ for (int id = 0; id < nt->count; id++) { const char *ty = nt_type(nt, id); if (!ty || strcmp(ty, "LambdaNode")) continue; int pn = nt_ref(nt, id, "parameters"); if (pn < 0) continue; int rn = 0; const int *reqs = nt_arr(nt, pn, "requireds", &rn); Scope *bs = comp_scope_of(c, id); for (int k = 0; k < rn; k++) { const char *p = nt_str(nt, reqs[k], "name"); if (!p) continue; LocalVar *lv = scope_local(bs, p); if (lv && lv->type == TY_UNKNOWN) { lv->type = TY_INT; changed = 1; } } } for (int id = 0; id < nt->count; id++) { const char *ty = nt_type(nt, id); if (!ty || strcmp(ty, "CallNode")) continue;test/proc_lambda_array_of_param.rbaddedView file
Loading diff…
test/proc_lambda_array_of_param.rb.expectedaddedView file
Loading diff…