Files
sx/examples/closures/0312-closure-optional-param-arg-coercion.sx
agra 959845bd30 style: migrate arrow-block lambdas () => { .. } to () { .. }
The canonical sx block-body lambda is `(params) { stmts }` (and
`(params) -> Ret { stmts }`); the arrow form `=>` is for EXPRESSION bodies
(`(params) => expr`). The arrow-block hybrid `(params) => { .. }` was being
used in 33 files — convert all of them by dropping the `=>`. The two forms are
exactly equivalent (verified: identical IR and identical runtime values — the
block tail is the value with or without a `-> Ret`), so this is a pure source
cleanup: no `.ir` churn, and the only snapshot change is 0923's diagnostic
COLUMN (a negative narrowing test whose error span shifted by the removed `=> `).

Arrow EXPRESSION bodies (`=> expr`, `=> .{..}`, `=> [..]`) and `=>` inside
comments/strings were left untouched. Migrated across examples/concurrency,
examples/{closures,ffi-objc,generics,optionals,types}, issues/, and the stdlib
(io.sx, sched.sx). Suite 855/0.
2026-06-28 16:39:51 +03:00

34 lines
1.4 KiB
Plaintext

// Calling a closure VALUE whose parameter is `?T` coerces the argument to the
// param type, just like a call to a top-level function does: a concrete arg
// wraps to a present optional, and `null` becomes an absent optional.
//
// Regression (issue 0186): the closure-value call path lowered args without
// coercing to the closure's declared param types, so a concrete `7` arrived as
// a bare payload (read ABSENT) and `null` reached a `{T,i1}` slot as a bare
// pointer (LLVM verifier failure). Fixed by typing args against the closure's
// params (`resolveCallParamTypes`) AND coercing them at the call site
// (`coerceClosureCallArgs`).
#import "modules/std.sx";
main :: () {
pick := (p: ?i64) -> i64 {
if p == null { return -1; }
return p; // narrowed inside the lambda body
};
print("pick 7: {}\n", pick(7)); // 7 (concrete arg wraps present)
print("pick null: {}\n", pick(null)); // -1 (null arg → absent)
// also via a closure stored in a struct field
Holder :: struct { f: Closure(?i64) -> i64; }
h := Holder.{ f = pick };
print("h 5: {}\n", h.f(5)); // 5
print("h null: {}\n", h.f(null)); // -1
// and via a plain function-pointer VALUE (same coercion contract)
fp : (?i64) -> i64 = target;
print("fp 8: {}\n", fp(8)); // 8
print("fp null: {}\n", fp(null)); // -1
}
target :: (p: ?i64) -> i64 { if p == null { return -1; } return p; }