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

@@ -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))))"