From bb2f05fa3638bbecfc9d23affeb8f07bf77906e0 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Wed, 14 Oct 2026 16:40:37 +0300 Subject: [PATCH] type a macro call by what it expands to A receiver, a scrutinee or the start of a path was typed before the walk had expanded it, so (.len (the a)) had no type to dispatch on and (. (the p) x) read a pointer as a value. --- semen.scm | 23 +++++++++++++++++------ tests/encapsulation.scm | 13 +++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/semen.scm b/semen.scm index 7ce7935..9adfdca 100644 --- a/semen.scm +++ b/semen.scm @@ -561,12 +561,7 @@ ;; expanded here rather than before the walk, so the macro body can ;; ask `(type-of x)' about a local the walk has already passed ((macro? form) - (let ((expansion (parameterize - ((current-type-of - (lambda (queried) - (unresolve-closure-types - (expression-type queried env))))) - (macroexpand form (list))))) + (let ((expansion (expand-call form env))) (if (and (pair? expansion) (null? (cdr expansion))) (walk-statement (car expansion) env) (cons walk-embed-result (walk-body expansion env))))) @@ -671,6 +666,14 @@ (walk-parts form env)) env))))))))) +;;; The forms a macro call stands for, with `(type-of x)' answered from +;;; ENV +(define (expand-call form env) + (parameterize ((current-type-of + (lambda (queried) + (unresolve-closure-types (expression-type queried env))))) + (macroexpand form (list)))) + ;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'. (define (resolve-wildcard form env) (if (and (>= (length form) 3) (wildcard-type? (third form))) @@ -1029,6 +1032,14 @@ (let ((written (car parts))) (if (= 1 (length written)) (car written) written))))) ((symbol? expr) (or (lookup-name env expr) (get-name-type expr))) + ;; the walk expands `(the a)' before it gets there, so its type is + ;; that of what it expands to: + ;; + ;; (.len (the a)) (match (the s) ...) (. (the a) x) + ((macro? expr) + (let ((expansion (expand-call expr env))) + (and (pair? expansion) (null? (cdr expansion)) + (expression-type (car expansion) env)))) ((not (and (list? expr) (pair? expr))) #f) ;; a call of no arguments is still a call ((< (length expr) 2) diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index cdce09b..0f05f74 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -125,6 +125,14 @@ (test-assert "one that cannot change it takes it" (emits? (ops-src "(var c (* const vec2) p) (var n float (.length c))") "vec2ːlength(c)")) + (test-assert "a macro receiver is typed by what it expands to" + (emits? (string-append "(defmacro (the a) a)" + (ops-src "(var n float (.length (the v)))")) + "vec2ːlength(&v)")) + (test-assert "and so is the start of a path" + (emits? (string-append "(defmacro (the a) a)" + (ops-src "(var q float (. (the p) x))")) + "p->x")) (test-assert "an address cannot be taken of a call" (reports? (ops-src "(.length (mk))") "receiver is not an lvalue")) ;; the inner call has to be typed to find the outer one's receiver @@ -285,6 +293,11 @@ (test-assert "a path scrutinee is not copied" (not (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))") "ƛm0"))) + (test-assert "a macro scrutinee is typed by what it expands to" + (emits? (string-append "(defmacro (the a) a)" + (sum-src "(match (the s) ((circle (r)) (= out r))" + " (else (= out 0.0)))")) + "shape ƛm0 = s")) (test-assert "else becomes default" (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))") "default:"))