From 2e22983edc497b763769813c7dcb48560d7d859f Mon Sep 17 00:00:00 2001 From: alex-eg Date: Mon, 19 Oct 2026 14:48:43 +0300 Subject: [PATCH] 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. --- semen.scm | 12 ++++++++++++ tests/encapsulation.scm | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/semen.scm b/semen.scm index 0eeccbb..2e66d7c 100644 --- a/semen.scm +++ b/semen.scm @@ -2093,8 +2093,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 , 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. diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index 5c6a39a..07141af 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -506,6 +506,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)))")