1
0
forked from alex-eg/sex

make a declaring case body a block

A case body shared the switch's scope, so two cases could not each
declare `bits', and a label could not be followed by a declaration
before C23; inference shared the scope too, and typed a later case's
read of an outer name as the earlier case's local.

Fixes #51.
This commit is contained in:
2026-10-09 17:23:42 +03:00
parent 7532c72dec
commit 7f0b194d2b
4 changed files with 47 additions and 18 deletions

View File

@@ -53,6 +53,15 @@ compiles."
(let ((m (error-message source)))
(and m (string-contains m fragment) #t)))
(define (followed-by? source first then)
"Is FIRST in the C compiled from SOURCE, and the next thing past it,
whitespace aside, THEN?"
(let* ((c (silently (lambda () (sex->c source))))
(at (string-contains c first)))
(and at
(let ((next (string-skip c char-whitespace? (+ at (string-length first)))))
(and next (string-prefix? then (substring c next)))))))
(define (in-fn body)
(string-append "(fn f ((a int) (b int)) void " body ")"))
@@ -78,6 +87,22 @@ compiles."
;; c-switch handed its scrutinee straight to `cat', which only works
;; when it is an atom. Anything else was displayed as a raw
;; s-expression: `switch ((%. e type))'.
;; A case body shared the switch's scope, so two cases could not
;; declare one name, and a label could not be followed by a
;; declaration before C23. Only a body that declares needs a block
(test-group "case bodies"
(test-assert "are blocks"
(followed-by? (in-fn "(switch a (case 1 (var x int 1) (g x)))")
"case 1:" "{"))
(test-assert "so is a default"
(followed-by? (in-fn "(switch a (default (var x int 1) (g x)))")
"default:" "{"))
(test-assert "where they declare"
(followed-by? (in-fn "(switch a (case 1 (g a)))") "case 1:" "g(a)"))
(test-assert "and one already a block is not wrapped again"
(not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))")
"{" "{"))))
(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))))"

View File

@@ -7,6 +7,7 @@
"loop: 0 1 2"
"shadowed: 9 then 42"
"still an int: 200"
"case scope: 200"
"partial: 1 2.5"
"joined: 43.5 84 49 1"
"promoted: 200 60000 3705032704"
@@ -67,14 +68,18 @@
(var wide int 200)
(while false (var wide char 1) (printf "%d" wide))
(if false (do (var wide char 1) (printf "%d" wide)))
;; a statement before the declaration: a label may not be followed by
;; one until C23
(switch a (case 1 (printf "") (var wide char 1) (printf "%d" wide) (break)))
(switch a (case 1 (var wide char 1) (printf "%d" wide)))
;; a copy, not a sum: an arithmetic result would be promoted to `int'
;; whatever leaked, and say nothing
(var copy _ wide)
(printf "still an int: %d\n" copy)
;; each case is a block of its own: the second sees the outer `wide',
;; not the first one's
(switch 2
(case 1 (var wide double 1.5) (printf "%g" wide))
(case 2 (var seen _ wide) (printf "case scope: %d\n" seen)))
;; a wildcard inside a written type: only it is solved
(var pp2 (* _) (& p))
(printf "partial: %d %g\n" (. pp2 x) (. pp2 y))