;;; 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', `read-source', `fails-with?', `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 (read-source source))) (define (interface-reports? source fragment) (fails-with? (lambda () (interface-of source)) fragment)) (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 scale ((self (* vec2))) void (= (. self x) 2.0)))" "(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 "a self spread over the parameter is a pointer self too" (emits? (string-append "(record w ((x int)))" "(ops w (fn get ((self * const w)) int (return (. self x))))" "(fn f () int (var a w #(w : 1)) (var p (* const w) (& a))" " (return (+ (.get a) (.get p))))") "wːget(&a) + wːget(p)")) (test-assert "a const receiver is not handed to a method that may change it" (reports? (ops-src "(var c (* const vec2) p) (.scale c)") "this one is const")) (test-assert "one that cannot change it takes it" (emits? (ops-src "(var c (* const vec2) p) (var n float (.length c))") "vec2ːlength(c)")) (test-assert "a macro receiver is typed by what it expands to" (emits? (string-append "(defmacro (the a) a)" (ops-src "(var n float (.length (the v)))")) "vec2ːlength(&v)")) (test-assert "and so is the start of a path" (emits? (string-append "(defmacro (the a) a)" (ops-src "(var q float (. (the p) x))")) "p->x")) (test-assert "a typedef of the type has its methods" (emits? (string-append "(record w ((x int)))" "(typedef alias w)" "(ops w (fn get ((self (* const w))) int (return (. self x))))" "(fn f () int (var a alias #(alias : 1)) (return (.get a)))") "wːget(&a)")) (test-assert "and a method written on the typedef is the type's" (emits? (string-append "(record w ((x int)))" "(typedef alias w)" "(ops alias (fn get ((self (* const alias))) int (return 1)))" "(fn f () int (var a w #(w : 1)) (return (.get a)))") "aliasːget(&a)")) (test-assert "a typedef of a scalar keeps its own" (reports? (string-append "(typedef meters float)" "(ops meters (fn half ((self meters)) meters (return self)))" "(fn f () void (var x float 1.0) (.half x))") "no such method on")) (test-assert "which a typedef of it shares" (emits? (string-append "(typedef meters float)" "(typedef km meters)" "(ops meters (fn half ((self meters)) meters (return self)))" "(fn f () void (var x km 1.0) (var y km (.half x)))") "metersːhalf(x)")) (test-assert "a method call inside a literal is still a call" (emits? (ops-src "(var w vec2 #(vec2 : (.length v) 1.0))") "(vec2){vec2ːlength(&v), 1.0}")) (test-assert "however deep" (emits? (ops-src "(var ws [vec2 1] #(#(vec2 : (.length p) 1.0)))") "{(vec2){vec2ːlength(p), 1.0}}")) (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ǂ {")) (test-assert "and its constants after the sum and the variant" (emits? (sum-src "") "shapeǂcircle")) (test-assert "a variant may be called tag" (emits? "(sum t ((tag ((x int))) (none ())))" "struct tː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 "a constructor is written in place" (emits? "(pub sum t ((a ((x int)))))(fn f () t (return (t/a 1)))" "(struct tːa){1}")) (test-assert "and is no function" (not (emits? "(pub sum t ((a ((x int)))))(fn f () t (return (t/a 1)))" "t tːa ("))) (test-assert "so it is a constant at file scope" (emits? "(sum t ((a ((x int)))))(var g t (t/a 1))" "static t g = {.tag = tǂa, .u = {.a = {1}}};")) (test-assert "as is any literal a global starts from" (emits? "(record point ((x int) (y int)))(var g point #(point : 1 2))" "static point g = {1, 2};")) (test-assert "and an lvalue a method can take the address of" (emits? (string-append "(sum t ((a ((x int)))))" "(ops t (fn get ((self (* const t))) int (return 1)))" "(fn f () int (return (.get (t/a 1))))") "tːget(&(t){")) (test-assert "a constructor takes as many arguments as its variant has fields" (reports? "(sum t ((a ((x int)))))(fn f () t (return (t/a 1 2)))" "variant takes this many fields")) (test-assert "and is not a function value" (reports? "(sum t ((a ((x int)))))(fn f () void (var g (fn ((x int)) t) t/a))" "a constructor is not a function")) (test-assert "an array field takes a brace literal as it is" (emits? "(sum t ((a ((xs [int 2])))))(fn f () t (return (t/a #(1 2))))" "(struct tːa){{1, 2}}")) (test-assert "and an array by its elements" (emits? "(sum t ((a ((xs [int 2])))))(fn f () t (var v [int 2] #(1 2)) (return (t/a v)))" "(struct tːa){{v[0], v[1]}}")) (test-assert "which have to be countable" (reports? "(fn n () int (return 2))(sum t ((a ((xs [int (n)])))))" "array field needs a length")) (test-assert "and as many as the field holds" (reports? "(sum t ((a ((xs [int 2])))))(fn f () t (var v [int 3] #(1 2 3)) (return (t/a v)))" "array is not as long as its field")) (test-assert "and so must a literal" (reports? "(sum t ((a ((xs [int 2])))))(fn f () t (return (t/a #(1 2 3))))" "array is not as long as its field")) (test-assert "not filling it with zeros either" (reports? "(sum t ((a ((xs [int 2])))))(fn f () t (return (t/a #(1))))" "array is not as long as its field")) (test-assert "an array field takes an array" (reports? "(sum t ((a ((xs [int 2])))))(fn f () t (var p (* int) 0) (return (t/a p)))" "an array field takes an array")) (test-assert "read from a path, not from a call" (reports? (string-append "(sum t ((a ((xs [int 2])))))(fn g () int (return 0))" "(fn f () t (var m [t 2]) (return (t/a (. [m (g)] u a xs))))") "bind it to a var first")) (test-assert "an array binder is a copy of each element" (emits? (string-append "(sum t ((a ((xs [int 2])))))" "(fn f ((s t)) int (match s ((a (xs)) (return [xs 0]))))") "int xs[2] = {s.u.a.xs[0], s.u.a.xs[1]}")) (test-assert "a sum cannot hold itself" (reports? "(sum box ((inner ((x box)))))" "a sum holds itself through a pointer")) (test-assert "nor an array of itself" (reports? "(sum box ((inner ((x [box 2])))))" "a sum holds itself through a pointer")) (test-assert "though it can point at itself" (emits? "(sum box ((inner ((x (* box))))))" "box * x;")) (test-assert "a function argument becomes the closure its field holds" (emits? (string-append "(fn g ((n int)) int (return n))" "(sum t ((a ((k (closure ((int)) int))))))" "(fn f () t (return (t/a g)))") "ƛint_int_fromfn(g)")) (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")) (test-assert "and is declared once" (reports? "(sum t ((a ((x int))) (a ())))" "variant is declared twice"))) ;; 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 typedef realizes what its type does" (reports? (string-append (spec-src "") "(typedef alias v)(realize (p alias))") "spec is already realized for")) (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 docstring is not a body" (reports? (string-append "(record v ((x int)))" "(spec (p T) (fn s ((a (* const T))) void \"doc\"))" "(realize (p v))") "realization is missing a method of")) (test-assert "nor is a comment" (reports? (string-append "(record v ((x int)))" "(spec (p T) (fn s ((a (* const T))) int\n" " ;; must be implemented\n" " ))" "(realize (p v))") "realization is missing a method of")) (test-assert "a documented default is still a default" (emits? (string-append "(record v ((x int)))" "(spec (p T) (fn s ((a (* const T))) int \"doc\" (return 1)))" "(realize (p v))") "/* doc */")) (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 "but not on how a pointer is spelled" (emits? (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)))") "void vːpːs (const v * a)")) (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 an if over the tag whose arms bind the payload. What ;; it refuses is why it exists. (test-group "match" (test-assert "it tests the tag" (emits? (sum-src "(match s ((circle (r)) (= out r)) ((rect (w h)) (= out w))" " ((empty ()) (= out 0.0)))") "if (s.tag == shapeǂcircle)")) (test-assert "but not for the last variant, which is all that is left" (not (emits? (sum-src "(match s ((circle (r)) (= out r)) ((rect (w h)) (= out w))" " ((empty ()) (= out 0.0)))") "s.tag == shapeǂempty"))) (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 "unless a binder is named like a part of it" (emits? (sum-src "(match s ((circle (s)) (= out s)) (else (= out 0.0)))") "shape ƛm0 = s")) (test-assert "a macro scrutinee is typed by what it expands to" (emits? (string-append "(defmacro (the a) a)" (sum-src "(match (the s) ((circle (r)) (= out r))" " (else (= out 0.0)))")) "shape ƛm0 = s")) (test-assert "a typedef of a sum is matched as the sum" (emits? (string-append "(sum shape ((circle ((r int))) (empty ())))" "(typedef alias shape)" "(fn f () int (var s alias (shape/circle 1))" " (match s ((circle (r)) (return r)) ((empty ()) (return 0))))") "s.tag == shapeǂcircle")) (test-assert "else is the last branch, wherever it is written" (let ((c (silently (lambda () (sex->c (sum-src "(match s (else (= out 2.0)) ((circle (r)) (= out r)))")))))) (< (string-contains c "out = r") (string-contains c "out = 2.0")))) (test-assert "a comment between arms is dropped" (emits? (sum-src "(match s ((circle (r)) (= out r)) ;; gone\n (else (= out 2.0)))") "out = 2.0")) (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)))") "s.tag == shapeǂcircle")) (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 "so is a second else" (reports? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0))" " (else (= out 1.0)))") "more than one else")) (test-assert "so is an arm that is not one" (reports? (sum-src "(match s foo)") "an arm is")) (test-assert "match has no value" (reports? (sum-src "(var n float (match s ((circle (r)) r) (else 0.0)))") "match is a statement")) (test-assert "not even as an argument" (reports? (sum-src "(= out (match s ((circle (r)) r) (else 0.0)))") "match is a statement")) (test-assert "nor as an if's test" (reports? (sum-src "(if (match s ((circle (r)) 1) (else 0)) (= out 1.0))") "match is a statement")) (test-assert "nor as a while's" (reports? (sum-src "(while (match s ((circle (r)) 1) (else 0)) (= out 1.0))") "match is a statement")) (test-assert "nor in a for's header" (reports? (sum-src "(for (var i int 0) (match s ((circle (r)) 1) (else 0)) (++ i)" " (= out 1.0))") "match is a statement")) (test-assert "nor as a case label" (reports? (sum-src "(switch 1 (case (match s ((circle (r)) 1) (else 0)) (= out 1.0)))") "match is a statement")) (test-assert "nor as a case/fallthrough's" (reports? (sum-src "(switch 1 (case/fallthrough (match s ((circle (r)) 1) (else 0))" " (= out 1.0)))") "match is a statement")) (test-assert "but an if's branches are statements, the else among them" (emits? (sum-src "(if (== out 0.0) (match s ((circle (r)) (= out r)) (else (= out 1.0)))" " (match s ((circle (r)) (= out 2.0)) (else (= out 3.0))))") "out = 3.0;")) (test-assert "but a case body is a statement" (emits? (sum-src "(switch 1 (case 1 (match s ((circle (r)) (= out r))" " (else (= out 0.0)))))") "s.tag == shapeǂcircle")) (test-assert "and so is a closure's, wherever the closure stands" (emits? (sum-src "(var c (closure () int) (closure () int ((t s))" " (match t ((circle (r)) (return 1)) (else (return 0)))))") "t.tag == shapeǂcircle")) (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)")) ;; 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")) (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 function a pub one takes" (interface-reports? "(record cell ((v int))) (pub fn take ((cb (fn ((c cell)) void))) void)" "held by value")) (test-assert "or returned by one" (interface-reports? "(record cell ((v int))) (pub fn take ((cb (fn () cell))) void)" "held by value")) (test "but a pointer there is still a pointer" '((record cell)) (interface-of "(record cell ((v int))) (pub fn take ((cb (fn ((c (* cell))) void))) void)")) (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)")) ;; 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])))"))))