check the constants a module's interface hands its importers

An array bound in a pub record, or a name in a pub define's body, is
the importer's to resolve too, and a private define, or one from an
import without pub, failed there as an undeclared identifier. Found in
vs-map-viewer once its units stopped importing everything. A constant
has no declaration short of itself, so it is an error at the pub form.
This commit is contained in:
2026-10-09 18:21:11 +03:00
parent 5b7ed9793b
commit a8f8b23b49
2 changed files with 34 additions and 9 deletions

View File

@@ -179,6 +179,7 @@
(('pub 'var name type . _) (list type))
(_ (list))))
(('pub 'typedef name target) (list target))
(('pub 'define name . body) body)
(('pub (or 'record 'struct 'union) name . rest)
(field-types (find list? (remove comment-form? rest))))
(('pub 'sum name (variants ...))
@@ -218,7 +219,8 @@
(case (car type)
((*) (append-map (lambda (t) (type-uses t #t)) (cdr type)))
((¤) (let ((element (array-element-type type)))
(if element (type-uses element behind?) (list))))
(append (if element (type-uses element behind?) (list))
(if (array-bound? type) (bound-uses (last type)) (list)))))
((fn closure)
(if (and (pair? (cdr type)) (list? (second type)))
(append-map (lambda (t) (type-uses t #t))
@@ -230,21 +232,31 @@
(list)))
(else (append-map (lambda (t) (type-uses t behind?)) type))))))
;;; The types FORMS declare, as (name kind body?):
;;; [int (* 2 N)] needs N, as a value: there is no declaring it
(define (bound-uses bound)
(cond ((symbol? bound) (list (cons bound #f)))
((pair? bound) (append-map bound-uses (cdr bound)))
(else (list))))
;;; The types and constants FORMS declare, as (name kind body?):
;;;
;;; (pub record tally) -> (tally record #f)
;;; (sum colour ((none ()))) -> (colour sum #t)
;;; (define (sq x) (* x x)) -> (sq define #t)
(define (declared-types forms)
(filter-map
(lambda (form)
(and (pair? form)
(let ((core (if (pub-form? form) (cdr form) form)))
(and (pair? core) (pair? (cdr core))
(memq (car core) '(record struct union enum sum typedef))
(symbol? (second core))
(list (second core)
(let* ((core (if (pub-form? form) (cdr form) form))
(name (and (pair? core) (pair? (cdr core))
(if (and (eq? (car core) 'define) (pair? (second core)))
(car (second core))
(second core)))))
(and (memq (car core) '(record struct union enum sum typedef define))
(symbol? name)
(list name
(car core)
(or (eq? (car core) 'typedef)
(or (memq (car core) '(typedef define))
(any list? (remove comment-form? (cddr core)))))))))
forms))