From 4a649e5bf4059caae1e3a3d21d5a8293e4729886 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Sat, 3 Oct 2026 23:33:32 +0300 Subject: [PATCH] walk the cdr of a form as a list, not as a form The walker offered walk-fn every cdr as well as every form, so the tail (t) of (fn g () t) read as a call to a macro named t: any function whose return type or parameter type shared a name with a macro expanded that tail and died on the macro's arity. --- semen.scm | 28 ++++++++++++++++++---------- tests/codegen.scm | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/semen.scm b/semen.scm index b5e462f5..8e2d1ade 100644 --- a/semen.scm +++ b/semen.scm @@ -29,6 +29,7 @@ ;;; multiple forms, e.g. lambdas collected from a function may result ;;; in auxiliary structures and functions. (define (process raw-sex-forms) + (reset-declarations!) (process-rec raw-sex-forms (list))) (define (process-rec forms acc) @@ -160,16 +161,23 @@ (define (walk-form form walk-fn env) (if (atom? form) form (let ((new-form (walk-fn form env))) - (cond ((not (eq? form new-form)) - (walk-form new-form walk-fn env)) - (else - (let ((new-car (walk-form (car new-form) walk-fn env)) - (new-cdr (walk-form (cdr new-form) walk-fn env))) - (cond ((and (pair? new-car) - (eq? (car new-car) walk-embed-result)) - (append (cdr new-car) new-cdr)) - (else - (recons new-form new-car new-cdr))))))))) + (if (not (eq? form new-form)) + (walk-form new-form walk-fn env) + (walk-rest new-form walk-fn env))))) + +;;; The cdr of a form is a list of forms and not itself one, so it is +;;; walked without being offered to WALK-FN: otherwise the tail of +;;; (fn g () t) is the list (t), which reads as a call to a macro +;;; named t -- and a function whose return type shares a name with a +;;; macro expanded that tail and died on the arity. +(define (walk-rest form walk-fn env) + (if (atom? form) form + (let ((new-car (walk-form (car form) walk-fn env)) + (new-cdr (walk-rest (cdr form) walk-fn env))) + (if (and (pair? new-car) + (eq? (car new-car) walk-embed-result)) + (append (cdr new-car) new-cdr) + (recons form new-car new-cdr))))) ;;; Typdef diff --git a/tests/codegen.scm b/tests/codegen.scm index 51b0a484..fdb3412d 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -394,6 +394,21 @@ compiles." (fn f ((c (closure ((int)) int))) int (return (g c)))" "g(c)"))) + ;; The walker offered WALK-FN the cdr of every form as well as the + ;; form, so the tail (t) of (fn g () t) read as a call to a macro + ;; named t, and any function returning a type that shared a name + ;; with a macro died on that macro's arity. + (test-group "a form's tail is not a form" + (test-assert "a return type may share a name with a macro" + (emits? "(defmacro (t x) 1)(typedef t int)(fn g () t (return 1))" + "t g (void)")) + (test-assert "so may a parameter's type" + (emits? "(defmacro (u x) 1)(typedef u int)(fn g ((a u)) void (return))" + "void g (u a)")) + (test-assert "and the macro still expands where it is called" + (emits? "(defmacro (t x) x)(fn g () int (return (t 7)))" + "return 7"))) + ;; a macro body reads types as lists, so the srfi-1 accessors are in ;; scope beside the type database (test-group "macro list accessors"