From 724f4dd3e9d6b3e09c1e9494813548b6dd8e186a Mon Sep 17 00:00:00 2001 From: alex-eg Date: Tue, 27 Oct 2026 22:41:06 +0300 Subject: [PATCH] report a module's macro call failing for other than its own names Reading a module's interface dropped any toplevel call that raised, not only one asking after a declaration of the module's, which no importer has yet. Lookups now tell which names they missed, and only those failures give importers nothing. --- sex-modules.scm | 44 ++++++++++++++++++++++++++++-------- tests/modules/check.scm | 7 ++++++ tests/modules/gen/broken.sex | 8 +++++++ types.scm | 19 ++++++++++++---- 4 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 tests/modules/gen/broken.sex diff --git a/sex-modules.scm b/sex-modules.scm index e1021a9c..1f325bb4 100644 --- a/sex-modules.scm +++ b/sex-modules.scm @@ -130,10 +130,11 @@ ;;; macros are the module's own and those its imports give it, lent for ;;; the expanding and taken back after, so the importer is left with the ;;; pub ones alone. One that reads the type database sees what the -;;; importer knows, which a type private to the module is not; a call -;;; that cannot expand for that -- (print-struct box), box being the -;;; module's -- gives importers nothing, as before, and is the module's -;;; own compile's to check +;;; importer knows, which no declaration of the module's is yet; a call +;;; that fails after asking for one -- (field-count box), box being the +;;; module's record -- gives importers nothing, as before, and is the +;;; module's own compile's to check. Any other failure is the importer's +;;; too: (boom) raising "nope" says so wherever it is read (define +modules-expanded+ (list)) (define (module-forms raw-forms) @@ -144,7 +145,8 @@ forms)))) (define (expanded raw-forms) - (let ((lent (list))) + (let ((lent (list)) + (declared (declared-names raw-forms))) (define (lend! form) (let ((name (macro-name form))) (unless (assq name lent) @@ -154,15 +156,37 @@ (cond ((macro-name form) (lend! form) (list form)) ((splice? form) (append-map expand (cdr form))) ((macro? form) - (let ((result (handle-exceptions exn #f (apply-macro form)))) + (let ((result (expand-call form declared))) (cond ((or (not result) (null? result)) (list)) (else (stamp-form-source! result (form-source form)) (expand result))))) (else (list form)))) - (for-each lend! (imported-macro-forms raw-forms)) - (let ((forms (append-map expand raw-forms))) - (for-each (lambda (entry) (restore-macro! (car entry) (cdr entry))) lent) - forms))) + (dynamic-wind + void + (lambda () + (for-each lend! (imported-macro-forms raw-forms)) + (append-map expand raw-forms)) + (lambda () + (for-each (lambda (entry) (restore-macro! (car entry) (cdr entry))) + lent))))) + +(define (expand-call form declared) + (let ((unknown (list))) + (handle-exceptions exn + (if (any (lambda (name) (memq name declared)) unknown) #f (abort exn)) + (parameterize ((unknown-name (lambda (name) (set! unknown (cons name unknown))))) + (apply-macro form))))) + +;;; The names FORMS declare, types and all +(define (declared-names forms) + (append (map car (declared-types forms)) + (filter-map (lambda (form) + (match form + ((or ('pub (or 'fn 'var) (? symbol? name) . _) + ((or 'fn 'var) (? symbol? name) . _)) + name) + (_ #f))) + forms))) (define (splice? form) (and (pair? form) (eq? (car form) '$) (list? form))) diff --git a/tests/modules/check.scm b/tests/modules/check.scm index cf2de879..3775bc64 100644 --- a/tests/modules/check.scm +++ b/tests/modules/check.scm @@ -38,6 +38,7 @@ ;;; ;;; gen/made writes its pub forms by macro calls, which are expanded when ;;; its interface is read; the macros it used for that stay its own. +;;; gen/broken's call raises, which reading its interface reports. ;;; ;;; make check-modules @@ -138,6 +139,12 @@ '("made 42 7 1 2") (output-of (built "made-app"))) +;;; ...but a call failing for other than a name its module declares fails +;;; there as well +(test-assert "a toplevel macro call that raises fails the interface too" + (let ((err (error-of sexc "--public-interface" "gen/broken.sex"))) + (and err (irregex-search "broken\\.sex:8: expanding boom: nope" err)))) + ;;; ...while a realization of a spec importers cannot see is refused by ;;; the module that writes it, not by an importer naming it first (test-assert "a pub realization of a spec imported without pub is refused" diff --git a/tests/modules/gen/broken.sex b/tests/modules/gen/broken.sex new file mode 100644 index 00000000..b7063f90 --- /dev/null +++ b/tests/modules/gen/broken.sex @@ -0,0 +1,8 @@ +;;; A toplevel macro call failing for what it is, not for asking after a +;;; name declared here: an importer is told so, as this module is +(defmacro (boom) + (error "nope")) + +(pub fn fine () int (return 1)) + +(boom) diff --git a/types.scm b/types.scm index 8a73cccf..035f615a 100644 --- a/types.scm +++ b/types.scm @@ -49,6 +49,7 @@ add-name-type! get-name-type + unknown-name type-of current-type-of get-return-type @@ -156,11 +157,21 @@ (define (add-name-type! name type) (hash-table-set! +name-db+ name type)) +;;; Called with each name a lookup finds nothing for. A module's +;;; interface is read before its declarations are, so a macro call there +;;; asking after one -- (field-count box), box being its record -- is +;;; told nothing, and fails for that rather than for being wrong +(define unknown-name (make-parameter void)) + +(define (known name info) + (unless info ((unknown-name) name)) + info) + ;;; #f for a name never declared, which is what `printf' looks like ;;; until something parses stdio.h. Not an error here; the caller ;;; decides. (define (get-name-type name) - (hash-table-ref/default +name-db+ name #f)) + (known name (hash-table-ref/default +name-db+ name #f))) ;;; What `(make-adder 10)' has for a type: `make-adder's return type, ;;; or #f when NAME is not a function with a signature on record @@ -172,7 +183,7 @@ (third type)))) (define (get-tag-info name) - (hash-table-ref/default +tag-db+ name #f)) + (known name (hash-table-ref/default +tag-db+ name #f))) ;;; First look up the ordinary identifier, then tag of that id when ;;; no ordinary one was declared, as C does @@ -296,7 +307,7 @@ (hash-table-set! +ops-db+ (cons (type-key type) method) qualified)) (define (get-method type method) - (hash-table-ref/default +ops-db+ (cons (type-key type) method) #f)) + (known type (hash-table-ref/default +ops-db+ (cons (type-key type) method) #f))) ;;; vec2 + length -> vec2/length, which `unkebabify' spells vec2ːlength (define (qualify-op type name) @@ -338,7 +349,7 @@ ;;; What a generic macro asks before it expands: (realizes? 'ord 'circle) (define (realizes? spec type) - (hash-table-exists? +realize-db+ (cons spec (type-key type)))) + (known type (hash-table-exists? +realize-db+ (cons spec (type-key type))))) ;;; Sums, constructors, methods and specs refuse a second declaration ;;; of the same name, which is per translation unit rather than per