refuse a variant named like a C keyword

One with fields is a member of the sum's union, and C takes no member
named int; one without is held to the same, as one rule.
This commit is contained in:
2026-10-19 14:48:43 +03:00
parent f66eaa7454
commit 8d6d1855e2
2 changed files with 20 additions and 0 deletions

View File

@@ -2085,8 +2085,20 @@
(define (parse-variant form v)
(unless (and (list? v) (= 2 (length v)) (symbol? (car v)) (list? (cadr v)))
(sex-error form "a variant is (name (fields ...))" v))
;; one with fields is a member of the sum's union, and C takes no
;; member named `int'; one without is held to the same, as one rule
(when (memq (car v) +c-keywords+)
(sex-error form "a variant cannot be named like a C keyword" (car v)))
(cons (car v) (normalize-fields (cadr v))))
;;; C11's, and the ones <stdbool.h>, in every unit, makes macros of
(define +c-keywords+
'(auto break case char const continue default do double else enum extern
float for goto if inline int long register restrict return short signed
sizeof static struct switch typedef union unsigned void volatile while
_Alignas _Alignof _Atomic _Bool _Complex _Generic _Imaginary _Noreturn
_Static_assert _Thread_local bool true false))
;;; (sum expr ((neg ((e expr))))) has no size; (neg ((e (* expr)))) does.
;;; An array field is spelled out element by element wherever it is
;;; copied, so its length has to be one sexc can count.

View File

@@ -505,6 +505,14 @@ ahead of its pub forms."
" (match t ((circle (r)) (return 1)) (else (return 0)))))")
"t.tag == shapeǂcircle"))
;; a pointer to one was refused, where `.' reads through one
;; a variant with fields is a member of the sum's union, which C
;; would not take named `int'
(test-assert "a variant named like a C keyword is refused"
(reports? "(sum tok ((int ((v int))) (other ())))" "a variant cannot be named like a C keyword"))
(test-assert "one without fields too, to keep one rule"
(reports? "(sum tok ((default ()) (other ())))" "a variant cannot be named like a C keyword"))
(test-assert "and stdbool's names count"
(reports? "(sum b ((true ()) (false ())))" "a variant cannot be named like a C keyword"))
(test-assert "a pointer to a sum is matched as the sum"
(emits? (sum-src "(var p (* shape) (& s))"
"(match p ((circle (r)) (= out r)) (else (= out 0.0)))")