From 671f1a3207c6b41769cfa084fc64e8bf6b52581e Mon Sep 17 00:00:00 2001 From: alex-eg Date: Mon, 19 Oct 2026 14:48:43 +0300 Subject: [PATCH] refuse a macro defined after a call to it The call was compiled as a function's, and the macro went unused there without a word. --- semen.scm | 23 ++++++++++++++++++++++- tests/codegen.scm | 8 ++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/semen.scm b/semen.scm index 2e66d7c..2269453 100644 --- a/semen.scm +++ b/semen.scm @@ -36,8 +36,22 @@ ;;; in auxiliary structures and functions. (define (process raw-sex-forms) (reset-declarations!) + (set! +called-as-functions+ (make-hash-table)) (process-rec raw-sex-forms (list))) +;;; The first call of each name compiled as a function's, so that a macro +;;; of that name defined after it is told of rather than going unused +;;; there without a word +(define +called-as-functions+ (make-hash-table)) + +(define (check-not-called-before form name) + (let ((call (hash-table-ref/default +called-as-functions+ name #f))) + (when call + (sex-error form "a macro defined after a call to it, which is a function's" + name + (string-append "called at " (form-file call) ":" + (number->string (form-line call))))))) + (define (process-rec forms acc) (cond ((null? forms) (reverse acc)) @@ -130,7 +144,11 @@ (process-imports (get-modules-public-forms sex-form) acc)) ((or ('defmacro . rest) - ('pub 'defmacro . rest)) (defmacro rest) acc) + ('pub 'defmacro . rest)) + (when (and (pair? rest) (pair? (car rest))) + (check-not-called-before sex-form (caar rest))) + (defmacro rest) + acc) ((or ('typedef new-type target) ('pub 'typedef new-type target)) @@ -737,6 +755,9 @@ ,receiver ,@(walk-operands form (cdr form) env)))))) (else + (when (and (symbol? (car form)) (form-source form) + (not (hash-table-exists? +called-as-functions+ (car form)))) + (hash-table-set! +called-as-functions+ (car form) form)) (convert-arguments (copy-form-source! form (walk-operands form form env)) env)))))))) diff --git a/tests/codegen.scm b/tests/codegen.scm index 059f681..58098a6 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -405,6 +405,14 @@ whitespace aside, THEN?" "macro takes at least this many arguments many 1")) (test-assert "while it may take any more" (emits? "(defmacro (many x . more) x) (fn f () int (return (many 1 2 3)))" "return 1;")) + ;; a call before the macro is defined is a function call, and the + ;; macro defined after it went unused there without a word + (test-assert "a macro defined after a call to it is refused" + (reports? "(fn f () int (return (later 1)))\n(defmacro (later x) x)" + "codegen.sex:2: a macro defined after a call to it")) + (test-assert "naming where the call is" + (reports? "(fn f () int\n (return (later 1)))\n(defmacro (later x) x)" + "called at codegen.sex:2")) (test-assert "an error in a macro's body names the call" (reports? "(defmacro (boom x) (error \"no good\" x))\n(fn f () int (return (boom 1)))" "codegen.sex:2: expanding boom: no good")))