require case labels to be values written once

(case (1 2) ...) is a list of labels, so (case (+ 1 1) ...) came out
case +: case 1: case 1:, and a value repeated in a switch was left to C
to report, without saying where.
This commit is contained in:
2026-10-16 15:27:07 +03:00
parent ef9aa8f413
commit 39a5f4de6b
2 changed files with 41 additions and 0 deletions

View File

@@ -12,6 +12,7 @@
sex-fmt-c
matchable
(chicken irregex) ; unkebabify
reader ; c-literal?
srfi-1 ; lists
srfi-13 ; strings
types ; array-bound?, named-arg?
@@ -278,6 +279,7 @@ forms, and what remains."
;; clause to be a case/default form and errors on anything else.
(('switch . rest)
(let-values (((comments slots clauses) (take-slots rest 1)))
(check-case-labels form clauses)
(with-comments (append comments (filter comment-form? clauses))
(cons* 'switch (walk-expr (car slots))
(map walk-expr (remove comment-form? clauses))))))
@@ -335,6 +337,28 @@ forms, and what remains."
(if most "" " or more")))
(car form)))))))
;;; (case (1 2) ...) is two labels, so anything but a value among them
;;; was taken for one: (case (+ 1 1) ...) came out `case +: case 1:
;;; case 1:'. A value written twice in one switch C reports too, but not
;;; where.
(define (check-case-labels form clauses)
(fold (lambda (clause seen)
(let ((parts (and (pair? clause) (memq (car clause) '(case case/fallthrough))
(remove comment-form? (cdr clause)))))
(if (pair? parts)
(fold (lambda (value seen)
(unless (or (number? value) (char? value) (c-literal? value)
(and (symbol? value) (not (operator-arity value))))
(sex-error form "a case label is a value" (car parts)))
(when (member value seen)
(sex-error form "case label written twice" value))
(cons value seen))
seen
(if (pair? (car parts)) (car parts) (list (car parts))))
seen)))
(list)
clauses))
;;; #(a b c) is a brace initializer. A `:' inside one ends a type and
;;; turns the whole thing into a C99 compound literal:
;;; #(struct point : 1 2) is (struct point){1, 2}, and the type is

View File

@@ -109,6 +109,23 @@ whitespace aside, THEN?"
(not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))")
"{" "{"))))
;; (case (1 2) ...) is two labels, so (case (+ 1 1) ...) came out as
;; three, the first of them `case +:'
(test-group "case labels"
(test-assert "a list of them is a case each"
(followed-by? (in-fn "(switch a (case (1 B) (g a)))") "case 1:" "case B:"))
(test-assert "an operator is not one"
(reports? (in-fn "(switch a (case (+ 1 1) (g a)))") "a case label is a value"))
(test-assert "nor is an expression in the list"
(reports? (in-fn "(switch a (case (1 (- 2)) (g a)))") "a case label is a value"))
(test-assert "a value is written once"
(reports? (in-fn "(switch a (case (1 2 1) (g a)))") "case label written twice"))
(test-assert "in the whole switch"
(reports? (in-fn "(switch a (case 1 (g a)) (case/fallthrough (2 1)) (default (g b)))")
"case label written twice"))
(test-assert "each switch apart"
(emits? (in-fn "(switch a (case 1 (g a))) (switch b (case 1 (g b)))") "case 1:")))
(test-group "switch scrutinee"
(test-assert "member access"
(emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))"