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 6583ed958c
commit 1dc0c1a2fc
4 changed files with 46 additions and 10 deletions

View File

@@ -84,6 +84,13 @@ forms, and what remains."
form
`(%begin ,@(map walk-expr comments) ,form)))
;;; A case body that declares is a block, so two cases can each declare
;;; a `bits', and a declaration can follow the label before C23
(define (walk-case-body body)
(if (any (lambda (form) (and (pair? form) (eq? 'var (car form)))) body)
(list (cons '%block-begin (walk-body body)))
(walk-body body)))
(define (walk-if-clauses clauses)
"(test stmt test stmt ... [else-stmt]) -- tests stay expressions."
(let loop ((cs clauses) (acc (list)))
@@ -249,12 +256,12 @@ forms, and what remains."
(('case . rest)
(let-values (((comments slots body) (take-slots rest 1)))
(with-comments comments
(cons* 'case (walk-expr (car slots)) (walk-body body)))))
(cons* 'case (walk-expr (car slots)) (walk-case-body body)))))
(('case/fallthrough . rest)
(let-values (((comments slots body) (take-slots rest 1)))
(with-comments comments
(cons* 'case/fallthrough (walk-expr (car slots)) (walk-body body)))))
(('default . body) (cons 'default (walk-body body)))
(cons* 'case/fallthrough (walk-expr (car slots)) (walk-case-body body)))))
(('default . body) (cons 'default (walk-case-body body)))
;; Drop comments so they will not generate additional comma
(else (map walk-expr (remove comment-form? form)))))