From f9647df009cf02c9e770f08a86ce36135dc84fbb Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 16 Oct 2026 17:04:20 +0300 Subject: [PATCH] refuse _ in a pub var's type Importers declare a pub var from its type alone, with nothing to solve a wildcard against, so (pub var n _ 42) reached them as extern _ n. --- sex-modules.scm | 15 +++++++++++++++ tests/encapsulation.scm | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/sex-modules.scm b/sex-modules.scm index b4efee5..3d0dabb 100644 --- a/sex-modules.scm +++ b/sex-modules.scm @@ -141,6 +141,7 @@ (reverse (fold (lambda (form acc) + (check-written-out form) (fold (lambda (use acc) (check-type-use form use visible hidden acc)) acc (append-map (lambda (type) (type-uses type #f)) @@ -148,6 +149,20 @@ (list) forms)))) +;;; Importers declare a pub var from its type, not its initializer, so +;;; there is nothing on their side to solve a `_' with +(define (check-written-out form) + (match form + (('pub 'var . _) + (match (strip-header-comments form 4) + (('pub 'var name type . _) + (when (let wildcard? ((t type)) + (or (eq? t '_) (and (pair? t) (any wildcard? t)))) + (sex-error form "a pub var's type is what importers declare it with; write it out" + name))) + (_ #f))) + (_ #f))) + ;;; USE is (name . behind-a-pointer?) (define (check-type-use form use visible hidden acc) (let* ((name (car use)) diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index e654799..db56594 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -531,6 +531,15 @@ ahead of its pub forms." (test "nor a private fn's" '() (interface-of "(record cell ((v int))) (fn f ((c cell)) void)")) + ;; importers are given the declaration, not the initializer, so `_' + ;; reached them unsolved: `extern _ n' + (test-assert "a pub var's type is written out" + (interface-reports? "(pub var n _ 42)" "write it out")) + (test-assert "all of it" + (interface-reports? "(pub var p (* _) 0)" "write it out")) + (test "a private one is solved where it is" + '() + (interface-of "(var n _ 42)")) (test-assert "held by value it is an error" (interface-reports? "(record cell ((v int))) (pub record shape ((c cell)))" "held by value"))