From 541d9cd610d6a591fa422736ea86851c5a73a788 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 9 Oct 2026 18:21:11 +0300 Subject: [PATCH] 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. --- sex-modules.scm | 28 ++++++++++++++++++++-------- tests/encapsulation.scm | 15 ++++++++++++++- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/sex-modules.scm b/sex-modules.scm index 976e66e..76bc34f 100644 --- a/sex-modules.scm +++ b/sex-modules.scm @@ -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)) diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index 060ecef..b7566a7 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -553,4 +553,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])))"))))