lang F1 2.5: contextual typing for multi-param closure literals

An untyped lambda (a, b, c) => ... now takes each param's type
positionally from the expected Closure(T0, T1, T2) -> R signature, for
heterogeneous param types, in both assignment and argument position.

Previously only the first param (or all-same-typed params) resolved:
lowerLambda's signature loop applied contextual typing into params, but
the return-type-inference temp scope and the body param binding both
re-resolved each param via resolveParamType -- which defaults an untyped
(inferred_type) param to s64. So b in Closure(s64, string) bound as s64
and b.len errored. Both sites now read the already-resolved signature
types params.items[user_param_base + i].ty (user_param_base skips the
pre-populated ctx/env slots).

Regression: examples/201-closure-contextual-params.sx.

Note: a generic return $R inferred through a closure-typed parameter is
still unresolved (folds into Phase 4 function monomorphization); concrete
returns work.
This commit is contained in:
agra
2026-05-29 22:00:42 +03:00
parent 27c88d4d26
commit 5fd513466f
4 changed files with 41 additions and 4 deletions

View File

@@ -0,0 +1,28 @@
// Step 2.5 — contextual typing for closure literals with N (heterogeneous)
// params. An untyped lambda `(a, b, c) => ...` takes each param's type
// positionally from the expected `Closure(T0, T1, T2) -> R` signature, in both
// assignment and argument position. (Previously only the first param — or
// all-same-typed params — resolved; trailing params silently defaulted to s64.)
#import "modules/std.sx";
// argument-position: lambda typed from the parameter's closure type.
apply2 :: (f: Closure(s64, string) -> s64, x: s64, s: string) -> s64 {
return f(x, s);
}
apply3 :: (f: Closure(s64, s64, string) -> s64, a: s64, b: s64, c: string) -> s64 {
return f(a, b, c);
}
main :: () -> s32 {
// assignment-position, mixed (s64, string) params — `b` is string.
cb : Closure(s64, string) -> s64 = (a, b) => a + b.len;
print("cb={}\n", cb(10, "hello")); // 10 + 5 = 15
// argument-position, 2 params.
print("r={}\n", apply2((a, b) => a + b.len, 10, "hello")); // 15
// argument-position, 3 params (s64, s64, string).
print("q={}\n", apply3((a, b, c) => a + b + c.len, 1, 2, "xyz")); // 1+2+3 = 6
0;
}