1
0
forked from alex-eg/sex

type a method call, so one can be another's receiver

expression-type knew nothing about a .method head, so the receiver of
(.length-sq (.sub a b)) had no type and the outer call could not
resolve. Vector arithmetic is nothing but nested calls like that one.
This commit is contained in:
2026-10-04 14:04:01 +03:00
parent f071684aec
commit 7f3f22e853
2 changed files with 27 additions and 7 deletions

View File

@@ -970,13 +970,25 @@
;; ...and an increment is the operand unpromoted:
;; (++ c), c : char -> char
((++ --) (expression-type (second expr) env))
;; otherwise a call: a closure answers with its own return type,
;; anything else with what its signature says
;; otherwise a call: a method answers for the name it resolves
;; to, a closure with its own return type, anything else with
;; what its signature says
(else
(let ((closure (receiver-closure-type (car expr) env)))
(if closure
(third closure)
(and (symbol? (car expr)) (get-return-type (car expr))))))))))
(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))))))))))
;;; (.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
(define (method-call-type expr env)
(and (pair? (cdr expr))
(let* ((written (expression-type (second expr) env))
(target (pointer-target written))
(key (ops-key (or target written))))
(and key
(let ((method (get-method key (method-head-name (car expr)))))
(and method (get-return-type method)))))))
;;; C's usual arithmetic conversions, far enough to answer `_':
;;;

View File

@@ -109,7 +109,15 @@
(test-assert "and so is a receiver with no type to dispatch on"
(reports? "(fn f () void (var n int 1) (.get n))" "no such method on"))
(test-assert "an address cannot be taken of a call"
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue")))
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue"))
;; the inner call has to be typed to find the outer one's receiver
(test-assert "a method call can be another's receiver"
(emits? (string-append
"(record w ((x int)))"
"(ops w (fn twice ((a w)) w (return a))"
" (fn get ((a w)) int (return (. a x))))"
"(fn f () int (var a w #(w : 1)) (return (.get (.twice a))))")
"wːget(wːtwice(a))")))
;; A sum is the tag enum, a payload struct per variant that carries
;; fields, the record holding both, and a constructor each.