From defc58d5035c1ad2b5e3e2b79fb309447b1bdecf Mon Sep 17 00:00:00 2001 From: alex-eg Date: Mon, 19 Oct 2026 14:18:56 +0300 Subject: [PATCH] refuse a pub realization of a spec importers cannot see It failed in whichever importer named the module before the spec's, pointing at the module, where a hidden type is refused in the module already: so is this, the remedy the same. --- sex-modules.scm | 28 ++++++++++++++++++++++++---- tests/encapsulation.scm | 11 +++++++++++ tests/modules/check.scm | 14 ++++++++++++++ tests/modules/specs/money.sex | 9 +++++++++ tests/modules/specs/shown.sex | 2 ++ 5 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 tests/modules/specs/money.sex create mode 100644 tests/modules/specs/shown.sex diff --git a/sex-modules.scm b/sex-modules.scm index 7e335a3..c4b4539 100644 --- a/sex-modules.scm +++ b/sex-modules.scm @@ -179,6 +179,7 @@ (fold (lambda (form acc) (check-written-out form) + (check-realized-spec form visible hidden) (fold (lambda (use acc) (check-type-use form use visible hidden acc)) acc (append-map (lambda (type) (type-uses type #f)) @@ -200,6 +201,22 @@ (_ #f))) (_ #f))) +;;; A pub realization goes to importers, who look its spec up; one they +;;; cannot see failed there, in whichever named this module before the +;;; spec's, and said nothing of where it was written +(define (check-realized-spec form visible hidden) + (match form + (('pub 'realize ((? symbol? spec) _) . _) + (let ((kept (assq spec hidden))) + (when (and kept (not (assq spec visible))) + (sex-error form + (string-append "importers cannot see this spec; " + (if (fourth kept) + "import its module with pub" + "make it pub")) + spec)))) + (_ #f))) + ;;; USE is (name . behind-a-pointer?) (define (check-type-use form use visible hidden acc) (let* ((name (car use)) @@ -300,25 +317,28 @@ ((pair? bound) (append-map bound-uses (cdr bound))) (else (list)))) -;;; The types and constants FORMS declare, as (name kind body?): +;;; The types, constants and specs 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) +;;; (spec (shown T) ...) -> (shown spec #t) (define (declared-types forms) (filter-map (lambda (form) (and (pair? form) (let* ((core (strip-pub form)) (name (and (pair? core) (pair? (cdr core)) - (if (and (eq? (car core) 'define) (pair? (second core))) + (if (and (memq (car core) '(define spec)) + (pair? (second core))) (car (second core)) (second core))))) - (and (memq (car core) '(record struct union enum sum typedef define)) + (and (memq (car core) + '(record struct union enum sum typedef define spec)) (symbol? name) (list name (car core) - (or (memq (car core) '(typedef define)) + (or (memq (car core) '(typedef define spec)) (any list? (remove comment-form? (cddr core))))))))) forms)) diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index b666c2a..a315e81 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -536,6 +536,17 @@ ahead of its pub forms." (interface-reports? "(pub var n _ 42)" "write it out")) (test-assert "all of it" (interface-reports? "(pub var p (* _) 0)" "write it out")) + ;; a spec is seen by importers as a type is: pub, or not at all + (test-assert "a pub realization of a private spec is refused" + (interface-reports? (string-append "(spec (shown T) (fn show ((self (* const T))) int))" + "(pub record m ((c int)))" + "(pub realize (shown m) (fn show ((self (* const m))) int (return 1)))") + "importers cannot see this spec; make it pub")) + (test "and of a pub one is not" + '() + (interface-of (string-append "(pub spec (shown T) (fn show ((self (* const T))) int))" + "(pub record m ((c int)))" + "(pub realize (shown m) (fn show ((self (* const m))) int (return 1)))"))) (test "a private one is solved where it is" '() (interface-of "(var n _ 42)")) diff --git a/tests/modules/check.scm b/tests/modules/check.scm index 04ae6e2..860011d 100644 --- a/tests/modules/check.scm +++ b/tests/modules/check.scm @@ -56,6 +56,14 @@ (define scratch (create-temporary-directory)) (define (built name) (make-pathname scratch name)) +;;; What PROGRAM run with ARGS writes to stderr when it fails, or #f +(define (error-of program . args) + (let* ((proc (process* program args)) + (err (begin (read-string #f (process-output-port proc)) + (read-string #f (process-error-port proc))))) + (process-wait proc) + (and (not (zero? (process-exit-status proc))) (string? err) err))) + ;;; The lines PROGRAM prints when run with ARGS, or #f when it fails (define (output-of program . args) (let* ((proc (process program args)) @@ -112,6 +120,12 @@ '("cycle 1 2") (output-of (built "cycle-app"))) +;;; ...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" + (let ((err (error-of sexc "specs/money.sex" "-C" "-o" "-"))) + (and err (irregex-search "money.sex:8: importers cannot see this spec" err) #t))) + (test "the app runs through both" '("hello, world" "hello, sex" diff --git a/tests/modules/specs/money.sex b/tests/modules/specs/money.sex new file mode 100644 index 0000000..2fd5d59 --- /dev/null +++ b/tests/modules/specs/money.sex @@ -0,0 +1,9 @@ +;;; Imports shown without pub, so its importers cannot see the spec +;;; this realizes: refused here, rather than in whichever importer +;;; names money before shown +(import shown) + +(pub record money ((cents int))) + +(pub realize (shown money) + (fn show ((self (* const money))) int (return (. self cents)))) diff --git a/tests/modules/specs/shown.sex b/tests/modules/specs/shown.sex new file mode 100644 index 0000000..0c8ce9a --- /dev/null +++ b/tests/modules/specs/shown.sex @@ -0,0 +1,2 @@ +(pub spec (shown T) + (fn show ((self (* const T))) int))