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 8db435f994
commit 9b503c1017
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))

View File

@@ -546,4 +546,17 @@ ahead of its pub forms."
"only a handle"))
(test "which is all it takes"
'()
(interface-of "(pub record h) (record h ((n int))) (pub fn f ((x (* h))) void)"))))
(interface-of "(pub record h) (record h ((n int))) (pub fn f ((x (* h))) void)"))
;; A constant has no declaration either, and an array bound or a
;; pub define's body hands its name to importers all the same
(test-assert "a constant sizing a pub record's array"
(interface-reports? "(define N 4) (pub record r ((xs [int N])))"
"this define, and C declares one only by defining it"))
(test-assert "or used by a pub define"
(interface-reports? "(define N 4) (pub define M (* 2 N))" "this define"))
(test "is fine when pub"
'()
(interface-of "(pub define N 4) (pub record r ((xs [int N])))"))
(test "as is a literal bound"
'()
(interface-of "(pub record r ((xs [int 4])))"))))