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.
This commit is contained in:
2026-10-19 14:48:43 +03:00
parent 7ee0abb992
commit 671f1a3207
2 changed files with 30 additions and 1 deletions

View File

@@ -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))))))))