From 2024d4d0f433560722ebdff2ef33dcad2b884b92 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Mon, 19 Oct 2026 13:38:30 +0300 Subject: [PATCH] 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. --- semen.scm | 20 +++++++++++++++++++- tests/codegen.scm | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/semen.scm b/semen.scm index 9871fd1..aa7eed5 100644 --- a/semen.scm +++ b/semen.scm @@ -1392,7 +1392,19 @@ (cond ((method-head? (car expr)) (method-call-type expr env)) ((receiver-closure-type (car expr) env) => third) - (else (and (symbol? (car expr)) (get-return-type (car expr)))))))))) + ;; a function's name, unless a local hides it + ((and (symbol? (car expr)) + (not (lookup-name env (car expr))) + (get-return-type (car expr))) + => identity) + ;; a pointer to one: a local, a parameter, a field, a call's result + (else (function-return (expression-type (car expr) env))))))))) + +;;; (fn ((int)) int) -> int, and through a typedef of one +(define (function-return type) + (cond ((and (pair? type) (eq? (car type) 'fn) (= 3 (length type))) (third type)) + ((and (symbol? type) (get-underlying-type type)) => function-return) + (else #f))) ;;; (.length-sq (.sub a b)) types the inner call to find the outer ;;; one's receiver, so a method call has to answer like any other @@ -1827,6 +1839,12 @@ (unwrap-type arg))) (second type) (iota (length (second type)))))) + ;; what it returns, for `_' to read: it goes out as written, and + ;; nothing else registers it + (add-name-type! helper + `(fn (((struct ,closure)) + ,@(map (lambda (p) (list (second p))) params)) + ,returns)) (unless (hash-table-ref/default +closure-calls+ helper #f) (hash-table-set! +closure-calls+ helper #t) (let ((call `((dot-access ƛc code) diff --git a/tests/codegen.scm b/tests/codegen.scm index 90b26a7..8704a4a 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -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"))