take a docstring on a sum

record, struct and enum take one; sum read it where its variants go.
This commit is contained in:
2026-10-19 14:48:43 +03:00
parent 1dd1cf0ae9
commit 2fa1ed21d7
4 changed files with 43 additions and 26 deletions

View File

@@ -248,10 +248,10 @@
;; between name and fields are not skipped, they already confuse the
;; writer
(match form
(('pub (and kind (or 'struct 'union 'enum))
(('pub (and kind (or 'struct 'union 'enum 'sum))
(? symbol? name) (? string? doc) . rest)
(values doc (copy-form-source! form `(pub ,kind ,name ,@rest))))
(((and kind (or 'struct 'union 'enum))
(((and kind (or 'struct 'union 'enum 'sum))
(? symbol? name) (? string? doc) . rest)
(values doc (copy-form-source! form `(,kind ,name ,@rest))))
(_ (values #f form))))
@@ -2045,7 +2045,8 @@
;;; purpose: a record would typedef shapeːcircle into the ordinary
;;; namespace, where the constructor of that name already lives. As a
;;; tag it shares nothing.
(define (process-sum form acc)
(define (process-sum written acc)
(let-values (((doc form) (extract-aggregate-docstring written)))
(let* ((core (if (eq? (car form) 'pub) (cdr form) form))
(name (and (pair? (cdr core)) (cadr core)))
(declared (and (pair? (cddr core)) (caddr core))))
@@ -2053,21 +2054,26 @@
(sex-error form "sum needs a name and a list of variants" form))
(let ((variants (map (lambda (v) (parse-variant form v)) declared)))
(for-each (lambda (v)
(for-each (lambda (field) (check-variant-field form name field))
(for-each (lambda (field)
(check-variant-field form name field))
(cdr v)))
variants)
(let* ((names (map car variants))
(twice (find (lambda (v) (> (count (cut eq? <> v) names) 1)) names)))
(twice (find (lambda (v) (> (count (cut eq? <> v) names) 1))
names)))
(when twice
(sex-error form "variant is declared twice" twice)))
(when (get-sum name)
(sex-error form "sum is already defined" name))
(add-sum! name variants)
(for-each (lambda (v) (add-constructor! (qualify-op name (car v)) name (car v)))
(for-each (lambda (v)
(add-constructor! (qualify-op name (car v)) name (car v)))
variants)
(fold match-sex-form acc
;; the docstring goes ahead of everything the sum is made of
(fold match-sex-form
(if doc (cons (copy-form-source! form (list 'comment doc)) acc) acc)
(map (lambda (f) (copy-form-source! form f))
(sum-forms name variants))))))
(sum-forms name variants)))))))
(define (parse-variant form v)
(unless (and (list? v) (= 2 (length v)) (symbol? (car v)) (list? (cadr v)))

View File

@@ -332,7 +332,8 @@
(('pub 'define name . body) body)
(('pub (or 'record 'struct 'union) name . rest)
(field-types (find list? (remove comment-form? rest))))
(('pub 'sum name (variants ...))
((or ('pub 'sum name (variants ...))
('pub 'sum name (? string?) (variants ...)))
(append-map (lambda (v)
(if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list)))
(remove comment-form? variants)))

View File

@@ -422,6 +422,13 @@
;; A string as the first body form (or after the name of a struct,
;; union or enum) is a docstring: it becomes a comment immediately
;; before the declaration, not a statement inside it.
(test-group "a sum's docstring"
;; record, struct and enum took one, and sum read it as its variants
(test-assert "is a comment ahead of it"
(emits? "(sum shape \"A shape.\" ((dot ()) (box ((w int)))))" "/* A shape. */"))
(test-assert "and the sum is still one"
(emits? "(sum shape \"A shape.\" ((dot ()) (box ((w int)))))" "shapeǂbox")))
(test-group "docstrings"
(test-assert "appears before the function"
(emits? "(fn greet ((name (* char))) void \"Greet NAME.\" (printf \"hi\" name))"

View File

@@ -550,6 +550,9 @@ ahead of its pub forms."
(test "a private one is solved where it is"
'()
(interface-of "(var n _ 42)"))
(test-assert "a pub sum's variants are looked at past its docstring"
(interface-reports? "(record cell ((v int))) (pub sum s \"doc\" ((a ((c cell))) (b ())))"
"held by value"))
(test-assert "held by value it is an error"
(interface-reports? "(record cell ((v int))) (pub record shape ((c cell)))"
"held by value"))