type a call by the type of what it calls

Only a function's name was asked what it returns, so a call through a
local or a field holding a pointer had no type, a local named like a
function took the function's, and a closure's call helper, which
nothing registered, left _ nothing to read.
This commit is contained in:
2026-10-19 13:38:30 +03:00
parent 289f26a5b9
commit 06182e442f
2 changed files with 38 additions and 1 deletions

View File

@@ -764,6 +764,25 @@
(test-assert "and a closure's"
(reports? (in-fn "(var c (closure ((int)) int) (closure ((x int)) _ () (return x)))")
"a function's types are written out"))
;; a call was typed by asking the global table what a function of
;; that name returns, which a local pointer is not
(test-assert "a call through a local function pointer"
(emits? (string-append "(fn area ((w int) (h int)) int (return (* w h)))"
"(fn f () void (var p (fn ((int) (int)) int) area) (var r _ (p 3 4)))")
"int r = p(3, 4)"))
(test-assert "through a parameter"
(emits? "(fn ap ((g (fn ((int)) double)) (x int)) void (var r _ (g x)))" "double r = g(x)"))
(test-assert "through a field"
(emits? (string-append "(struct ops ((op (fn ((int)) float))))"
"(fn f ((o (struct ops))) void (var r _ ((. o op) 1)))")
"float r = o.op(1)"))
(test-assert "a closure's call, which goes through a helper nothing typed"
(emits? "(fn f ((c (closure ((int)) int))) void (var r _ (c 4)))"
"int r = ƛint_int_call(c, 4)"))
(test-assert "and one returning a closure is called as one"
(emits? (string-append "(fn mk ((k int)) (closure ((int)) int) (return (closure ((b int)) int (k) (return (+ k b)))))"
"(fn f () void (var fp (fn ((int)) (closure ((int)) int)) mk) (var r _ ((fp 3) 4)))")
"int r = ƛint_int_call(fp(3), 4)"))
(test-assert "while a function is the pointer it already is"
(emits? "(fn g ((n int)) int (return n)) (fn f () void (var p _ g))"
"int (*p)(int) = g"))