Files
sex/tests/encapsulation.scm
alex-eg 360470d0e0 check what a module's importers can see of its types
An importer is given the pub forms and nothing else, so a pub form
naming a private type, or one from an import without pub, compiled in
its module and failed in every importer with `unknown type name'.

The pub forms are checked as the module itself is compiled. A record,
struct, union or sum behind a pointer needs only declaring, so its
bodyless declaration goes out ahead of them -- the opaque handle that
(pub record T) already gave by hand. Held by value it needs its body,
and a typedef or an enum has no declaration short of its definition:
those are errors at the pub form, saying what would do instead. A name
Sex has no declaration for is left to C.

An importer may now be handed a declaration after the definition it
declares, so a bodyless aggregate no longer replaces known fields.

Fixes #57, #58.
2026-10-09 17:43:09 +03:00

355 lines
18 KiB
Scheme
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
;;; record, ops, sum and match, through the whole pipeline.
;;;
;;; The diagnostics are the point of these forms -- a match that
;;; leaves a variant out is the thing C cannot say -- so most of what
;;; is checked here is what the compiler refuses, and that it refuses
;;; it with the name of whatever is wrong.
;;;
;;; `sex->c', `emits?', `reports?' and `in-fn' come from codegen.scm,
;;; which run.scm includes first.
(import sex-modules)
(define (interface-of source)
"The declarations SOURCE, compiled as a module, hands its importers
ahead of its pub forms."
(check-public-interface
(with-input-from-string source
(lambda ()
(parameterize ((current-source-file "codegen.sex"))
(parse-all (current-input-port)))))))
(define (interface-reports? source fragment)
(let ((m (handle-exceptions e ((condition-property-accessor 'exn 'message) e)
(begin (interface-of source) #f))))
(and m (string-contains m fragment) #t)))
(define (ops-src . body)
(string-append
"(record vec2 ((x float) (y float)))"
"(ops vec2"
" (pub fn length ((self (* const vec2))) float (return 1.0))"
" (fn sum-of ((self vec2)) float (return (. self x))))"
"(fn mk () vec2 (return #(vec2 : 1.0 2.0)))"
"(fn f () void (var v vec2 #(vec2 : 1.0 2.0)) (var p (* vec2) (& v)) "
(apply string-append body) ")"))
(define forward-src
(string-append
"(record pb ((n int)))"
"(ops pb"
" (fn twice ((self (* pb))) int (return (* 2 (.len self))))"
" (pub fn len ((self (* pb))) int (return (. self n))))"))
(define (sum-src . body)
(string-append
"(sum shape ((circle ((r float))) (rect ((w float) (h float))) (empty ())))"
"(fn mk () shape (return (shape/circle 1.0)))"
"(fn f () void (var s shape (shape/circle 1.0)) (var out float 0.0) "
(apply string-append body) ")"))
(define (spec-src . body)
(string-append
"(record v ((x int)))"
"(spec (p T)"
" (fn s ((self (* const T))) void)"
" (fn both ((self (* const T))) void (.s self)))"
"(realize (p v)"
" (fn s ((self (* const v))) void (return)))"
(apply string-append body)))
(test-group "encapsulation"
;; A record is a struct plus a typedef of its own name, so the bare
;; name stands where (struct name) had to.
(test-group "record"
(test-assert "emits the struct"
(emits? "(record point ((x int) (y int)))" "struct point {"))
(test-assert "and a typedef of the same name"
(emits? "(record point ((x int) (y int)))" "typedef struct point point;"))
(test-assert "so the bare name is a type"
(emits? "(record point ((x int)))(fn f () void (var p point))" "point p;"))
(test-assert "and so is a pointer to it"
(emits? "(record point ((x int)))(fn f () void (var p (* const point)))"
"const point * p"))
(test-assert "struct keeps C's meaning"
(not (emits? "(struct raw ((x int)))" "typedef")))
(test-assert "a record needs a name"
(reports? "(record ((x int)))" "record needs a name"))
;; An opaque handle: the declaration is public, the fields are not
(test-assert "declared, then defined, is still defined"
(emits? "(pub record h)(record h ((x int)))" "struct h {")))
;; An ops block is mangled functions plus a registry entry. `pub'
;; inside it means what it means outside.
(test-group "ops"
(test-assert "a method is the type and the name, mangled"
(emits? (ops-src "") "float vec2ːlength"))
(test-assert "a pub method has external linkage"
(not (emits? (ops-src "") "static float vec2ːlength")))
(test-assert "a private one does not"
(emits? (ops-src "") "static float vec2ːsum_of"))
(test-assert "the slash is not a division"
(emits? "(fn f () int (return (/ 10 2)))" "10 / 2"))
(test-assert "ops needs a type name"
(reports? "(ops (fn g ((s int)) void))" "ops needs a type name"))
(test-assert "and takes only fn forms"
(reports? "(record v ((x int)))(ops v (var n int 1))" "ops takes fn forms"))
(test-assert "a method is defined once"
(reports? (string-append "(record v ((x int)))"
"(ops v (fn g ((s v)) int (return 1))"
" (fn g ((s v)) int (return 2)))")
"method is already defined on"))
(test-assert "pub goes on the methods, not the block"
(reports? "(record v ((x int)))(pub ops v)" "pub belongs on the methods"))
;; Every method is known before any body is walked, and C is told of
;; one before a body above it calls it -- and only then
(test-assert "a method can call one written below it"
(emits? forward-src "return 2 * pbːlen(self)"))
(test-assert "which is declared ahead of the call"
(emits? forward-src "int pbːlen (pb * self);"))
(test-assert "and nothing else is"
(not (emits? (ops-src "") "vec2ːsum_of (vec2 self);"))))
;; The method sits in the head, so the shape of a form can be read
;; without knowing the receiver's type.
(test-group "method calls"
(test-assert "a value meeting a pointer self is addressed"
(emits? (ops-src "(.length v)") "vec2ːlength(&v)"))
(test-assert "a pointer meeting a pointer self is passed along"
(emits? (ops-src "(.length p)") "vec2ːlength(p)"))
(test-assert "a pointer meeting a value self is dereferenced"
(emits? (ops-src "(.sum-of p)") "vec2ːsum_of(*p)"))
(test-assert "a value meeting a value self is passed along"
(emits? (ops-src "(.sum-of v)") "vec2ːsum_of(v)"))
(test-assert "a field is still a field"
(emits? (ops-src "(var q float (. v x))") "v.x"))
;; the whole reason the method moved into the head
(test-assert "a field and a method of one name coexist"
(emits? (string-append
"(record w ((x int)))"
"(ops w (fn x ((self (* const w))) int (return 2)))"
"(fn f () void (var a w #(w : 1)) (var b int (. a x))"
" (var c int (.x a)))")
"wːx(&a)"))
(test-assert "and the field reading is untouched"
(emits? (string-append
"(record w ((x int)))"
"(ops w (fn x ((self (* const w))) int (return 2)))"
"(fn f () void (var a w #(w : 1)) (var b int (. a x)))")
"int b = a.x"))
(test-assert "a method that is not there is named"
(reports? (ops-src "(.nope v)") "no such method on"))
(test-assert "and so is a receiver with no type to dispatch on"
(reports? "(fn f () void (var n int 1) (.get n))" "no such method on"))
(test-assert "an address cannot be taken of a call"
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue"))
;; the inner call has to be typed to find the outer one's receiver
(test-assert "a method call can be another's receiver"
(emits? (string-append
"(record w ((x int)))"
"(ops w (fn twice ((a w)) w (return a))"
" (fn get ((a w)) int (return (. a x))))"
"(fn f () int (var a w #(w : 1)) (return (.get (.twice a))))")
"wːget(wːtwice(a))")))
;; A sum is the tag enum, a payload struct per variant that carries
;; fields, the record holding both, and a constructor each.
(test-group "sum"
(test-assert "the tag enum is named after the sum"
(emits? (sum-src "") "enum shapeːtag"))
(test-assert "a variant with fields gets a payload struct"
(emits? (sum-src "") "struct shapeːcircle {"))
(test-assert "the sum is typedef'd"
(emits? (sum-src "") "typedef struct shape shape;"))
;; a typedef here would land in the namespace the constructor holds
(test-assert "a payload struct is not"
(not (emits? (sum-src "") "typedef struct shapeːcircle")))
(test-assert "the typedef comes before the payloads, for a recursive sum"
(emits? "(sum expr ((lit ((v float))) (neg ((e (* const expr))))))"
"typedef struct expr expr;"))
(test-assert "constructors are static even when the sum is pub"
(emits? "(pub sum t ((a ((x int)))))" "static t tːa"))
(test-assert "a tag-only sum has no union at all"
(not (emits? "(sum light ((red ()) (green ())))" "union")))
(test-assert "a sum is declared once"
(reports? "(sum t ((a ())))(sum t ((b ())))" "sum is already defined"))
(test-assert "and needs variants"
(reports? "(sum t)" "sum needs a name and a list of variants"))
(test-assert "a variant is a name and a field list"
(reports? "(sum t (a))" "a variant is")))
;; A spec is a contract that emits nothing; a realization is its
;; methods for one type. The refusals are what make it a contract.
(test-group "spec and realize"
(test-assert "a spec on its own emits nothing"
(not (emits? "(spec (p T) (fn s ((a (* const T))) void))" "p")))
(test-assert "a realization mangles type, spec and method"
(emits? (spec-src "") "void vːpːs"))
(test-assert "a default is copied per realizing type"
(emits? (spec-src "") "vːpːboth"))
(test-assert "and its parameter is substituted"
(emits? (spec-src "") "void vːpːboth (const v * self)"))
(test-assert "a default calls that type's own required method"
(emits? (spec-src "") "vːpːs(self)"))
(test-assert "a default is static even when the realization is pub"
(emits? (string-append "(record v ((x int)))"
"(spec (p T) (fn s ((a (* const T))) void)"
" (fn d ((a (* const T))) void (.s a)))"
"(pub realize (p v)"
" (fn s ((a (* const v))) void (return)))")
"static void vːpːd"))
(test-assert "but a written method is not"
(not (emits? (string-append "(record v ((x int)))"
"(spec (p T) (fn s ((a (* const T))) void))"
"(pub realize (p v)"
" (fn s ((a (* const v))) void (return)))")
"static void vːpːs")))
(test-assert "a spec method answers to .method"
(emits? (spec-src "(fn f () void (var a v #(v : 1)) (.s (& a)))") "vːpːs(&a)"))
(test-assert "a spec is declared once"
(reports? "(spec (p T) (fn s ((a T)) void))(spec (p T) (fn q ((a T)) void))"
"spec is already declared"))
(test-assert "a type realizes a spec once"
(reports? (string-append (spec-src "") "(realize (p v)"
" (fn s ((a (* const v))) void (return)))")
"spec is already realized for"))
(test-assert "a realization names a spec that exists"
(reports? "(record v ((x int)))(realize (nope v))" "no such spec"))
(test-assert "every required method is provided"
(reports? (string-append "(record v ((x int)))"
"(spec (p T) (fn s ((a (* const T))) void)"
" (fn q ((a (* const T))) void))"
"(realize (p v) (fn s ((a (* const v))) void (return)))")
"realization is missing a method of"))
(test-assert "and the missing one is named"
(reports? (string-append "(record v ((x int)))"
"(spec (p T) (fn s ((a (* const T))) void)"
" (fn q ((a (* const T))) void))"
"(realize (p v) (fn s ((a (* const v))) void (return)))")
"q"))
(test-assert "a method the spec has not got is refused"
(reports? (string-append "(record v ((x int)))"
"(spec (p T) (fn s ((a (* const T))) void))"
"(realize (p v) (fn s ((a (* const v))) void (return))"
" (fn z ((a (* const v))) void (return)))")
"spec has no such method"))
(test-assert "a return type has to match the spec"
(reports? (string-append "(record v ((x int)))"
"(spec (p T) (fn s ((a (* const T))) void))"
"(realize (p v) (fn s ((a (* const v))) int (return 1)))")
"return type does not match"))
(test-assert "so do the arguments"
(reports? (string-append "(record v ((x int)))"
"(spec (p T) (fn s ((a (* const T))) void))"
"(realize (p v) (fn s ((a v)) void (return)))")
"arguments do not match"))
(test-assert "a method name already taken on the type is refused"
(reports? (string-append "(record v ((x int)))"
"(ops v (fn s ((a (* const v))) void (return)))"
"(spec (p T) (fn s ((a (* const T))) void))"
"(realize (p v) (fn s ((a (* const v))) void (return)))")
"method is already defined on")))
;; match is a switch over the tag whose arms bind the payload. What
;; it refuses is why it exists.
(test-group "match"
(test-assert "it switches on the tag"
(emits? (sum-src "(match s ((circle (r)) (= out r)) ((rect (w h)) (= out w))"
" ((empty ()) (= out 0.0)))")
"switch (s.tag)"))
(test-assert "an arm binds its payload"
(emits? (sum-src "(match s ((circle (r)) (= out r)) ((rect (w h)) (= out w))"
" ((empty ()) (= out 0.0)))")
"float r = s.u.circle.r"))
(test-assert "_ binds nothing"
(not (emits? (sum-src "(match s ((circle (r)) (= out r))"
" ((rect (w _)) (= out w))"
" ((empty ()) (= out 0.0)))")
").h")))
(test-assert "a scrutinee that is not a path is read once"
(emits? (sum-src "(match (mk) ((circle (r)) (= out r)) (else (= out 0.0)))")
"shape ƛm0 = mk()"))
(test-assert "a path scrutinee is not copied"
(not (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
"ƛm0")))
(test-assert "else becomes default"
(emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
"default:"))
(test-assert "a comment between arms is dropped"
(emits? (sum-src "(match s ((circle (r)) (= out r)) ;; gone\n (else (= out 0.0)))")
"default:"))
(test-assert "a variant left out is an error"
(reports? (sum-src "(match s ((circle (r)) (= out r)))")
"match leaves a variant out"))
(test-assert "and the missing ones are named"
(reports? (sum-src "(match s ((circle (r)) (= out r)))") "rect"))
(test-assert "else excuses the rest"
(emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
"switch (s.tag)"))
(test-assert "a variant the sum does not have is an error"
(reports? (sum-src "(match s ((circle (r)) (= out r)) ((square (w)) (= out w))"
" ((rect (w h)) (= out w)) ((empty ()) (= out 0.0)))")
"not a variant of"))
(test-assert "so is the wrong number of binders"
(reports? (sum-src "(match s ((circle (r q)) (= out r)) (else (= out 0.0)))")
"variant takes this many fields"))
(test-assert "so is the same arm twice"
(reports? (sum-src "(match s ((circle (r)) (= out r)) ((circle (q)) (= out q))"
" (else (= out 0.0)))")
"arm is written twice"))
(test-assert "and a scrutinee that is not a sum"
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
"match wants a sum")))
;; An importer is given the pub forms and nothing else, so a type one
;; of them names has to be among them, or C is told no more of it
;; than a declaration can say
(test-group "what importers can see"
(test "a private record behind a pointer is declared for them"
'((record cell))
(interface-of "(record cell ((v int))) (pub record shape ((c (* cell))))"))
(test "once"
'((record cell))
(interface-of "(record cell ((v int))) (pub fn f ((a (* cell)) (b (* cell))) void)"))
(test "a struct as a struct"
'((struct cell))
(interface-of "(struct cell ((v int))) (pub fn f ((a (* struct cell))) void)"))
(test "a pub one needs nothing"
'()
(interface-of "(pub record cell ((v int))) (pub record shape ((c cell)))"))
(test "nor one only C knows"
'()
(interface-of "(pub fn f ((p (* FILE)) (n size-t)) void)"))
(test "nor a private fn's"
'()
(interface-of "(record cell ((v int))) (fn f ((c cell)) void)"))
(test-assert "held by value it is an error"
(interface-reports? "(record cell ((v int))) (pub record shape ((c cell)))"
"held by value"))
(test-assert "naming the line"
(interface-reports? "(record cell ((v int)))\n(pub record shape ((c cell)))"
"codegen.sex:2:"))
(test-assert "in an array too"
(interface-reports? "(record cell ((v int))) (pub record shape ((cs [cell 4])))"
"held by value"))
(test-assert "returned"
(interface-reports? "(record cell ((v int))) (pub fn f () cell)" "held by value"))
(test-assert "or taken by a pub method"
(interface-reports? "(record cell ((v int))) (ops cell (pub fn get ((self cell)) int))"
"held by value"))
(test-assert "a typedef has no declaration to give, pointer or not"
(interface-reports? "(typedef small int) (pub fn f ((p (* small))) void)"
"this typedef, and C declares one only by defining it"))
(test-assert "nor has an enum"
(interface-reports? "(enum mood (up down)) (pub var m (* enum mood))"
"this enum, and C declares"))
(test-assert "a pub handle is held through a pointer"
(interface-reports? "(pub record h) (record h ((n int))) (pub fn f ((x h)) void)"
"only a handle"))
(test "which is all it takes"
'()
(interface-of "(pub record h) (record h ((n int))) (pub fn f ((x (* h))) void)"))))