diff --git a/semen.scm b/semen.scm index 877aec74..d5af7811 100644 --- a/semen.scm +++ b/semen.scm @@ -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 `_': ;;; diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index 05fabfef..c8555465 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -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.