From 7f0b194d2b8a77960c8750fa1a63293d9893b7eb Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 9 Oct 2026 17:23:42 +0300 Subject: [PATCH] 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. --- fmt-c-writer.scm | 13 ++++++++++--- semen.scm | 16 ++++------------ tests/codegen.scm | 25 +++++++++++++++++++++++++ tests/sex-programs/wildcards.sex | 11 ++++++++--- 4 files changed, 47 insertions(+), 18 deletions(-) diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index 4fe213b5..7a1fa171 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -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))) @@ -248,12 +255,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))))) diff --git a/semen.scm b/semen.scm index e785362b..e8e5c3d3 100644 --- a/semen.scm +++ b/semen.scm @@ -501,9 +501,8 @@ ;;; (while (< v 0) (var v char 1) ...) ;;; (var m _ (+ v 1)) ; double, not char ;;; -;;; One frame per form, not one per arm: a `case' label opens no scope -;;; in C, and an `if' arm can only declare inside a `do', which brings -;;; its own. +;;; A `case' body gets one too, being written as a block. An `if' arm +;;; gets none: it can only declare inside a `do', which brings its own. (define (declare-name! env name type) (hash-table-set! (car (hash-table-ref env :scopes)) name type)) @@ -584,20 +583,13 @@ (cons walk-embed-result (walk-body expansion env))))) (else (case (car form) - ((do) + ((do default) (parameterize ((operand-position? #f)) (with-scope env (lambda () (walk-parts form env))))) - ((for while if switch) + ((for while if switch case case/fallthrough) (with-scope env (lambda () (walk-control form env)))) - ((case case/fallthrough) - (walk-control form env)) - - ((default) - (parameterize ((operand-position? #f)) - (walk-parts form env))) - ((lambda) (let ((name (aux-name! env make-lambda-name))) (add-aux-code! env (lift-lambda name form)) diff --git a/tests/codegen.scm b/tests/codegen.scm index f8ee0914..db3ba623 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -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))))" diff --git a/tests/sex-programs/wildcards.sex b/tests/sex-programs/wildcards.sex index 8e813da9..558a6d52 100644 --- a/tests/sex-programs/wildcards.sex +++ b/tests/sex-programs/wildcards.sex @@ -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))