From c77255b705984d0d879966a16655b6a0ed0d770e Mon Sep 17 00:00:00 2001 From: alex-eg Date: Sun, 18 Oct 2026 22:02:55 +0300 Subject: [PATCH] refuse a capture a parameter or the body names too Captures are a closure's other parameters, and C's redefinition error was the first anyone heard of one sharing a name with a parameter, or being redeclared at the top of the body. --- semen.scm | 19 +++++++++++++++++++ tests/codegen.scm | 12 ++++++++++++ 2 files changed, 31 insertions(+) diff --git a/semen.scm b/semen.scm index a862a01..f506240 100644 --- a/semen.scm +++ b/semen.scm @@ -1833,6 +1833,7 @@ (define (lift-closure base form env) (match form (('closure arglist ret-type captures . body) + (check-capture-names form arglist (map capture-name captures) body) (let* ((caps (map (lambda (c) (capture-binding c form env)) captures)) (record (suffixed base "_captures")) (code (suffixed base "_code")) @@ -1875,6 +1876,24 @@ (return ƛc)))))))) (else (sex-error form "malformed closure" form)))) +;;; Captures are a closure's other parameters, and C's rule for those +;;; holds of them: no two alike, and none redeclared at the top of the +;;; body. A block inside may shadow one, as it may a parameter. +(define (check-capture-names form arglist names body) + (let ((parameters (filter-map (lambda (arg) (and (named-arg? arg) (car arg))) + (remove comment-form? arglist)))) + (for-each (lambda (name) + (when (memq name parameters) + (sex-error form "a capture and a parameter share a name" name))) + names) + (for-each (lambda (statement) + (match statement + (('var (? symbol? name) . _) + (when (memq name names) + (sex-error statement "the body redeclares a capture" name))) + (_ #f))) + body))) + ;;; A capture's field is assigned to, so it drops what qualifies the ;;; capture itself: (const int) is held as an int, and read back into a ;;; const one. (* const char) points at const, and is left alone. diff --git a/tests/codegen.scm b/tests/codegen.scm index 7429d0d..afe40e6 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -492,6 +492,18 @@ whitespace aside, THEN?" (test-assert "the type becomes a struct named for its signature" (emits? "(fn f ((c (closure ((int)) int))) int (return (c 1)))" "struct ƛint_int")) + ;; captures are a closure's other parameters, under C's rule for + ;; those: no two alike, and none redeclared at the body's top level. + ;; Both were C's redefinition errors + (test-assert "a capture named like a parameter is refused" + (reports? (in-fn "(var c (closure ((int)) int) (closure ((a int)) int (a) (return a)))") + "a capture and a parameter share a name")) + (test-assert "so is one redeclared at the top of the body" + (reports? (in-fn "(var c (closure () int) (closure () int (a) (var a int 5) (return a)))") + "the body redeclares a capture")) + (test-assert "while a block inside may shadow it" + (emits? (in-fn "(var c (closure () int) (closure () int (a) (do (var a int 5) (g a)) (return a)))") + "int a = 5;")) ;; The environment is one shared union, declared in the prelude -- ;; its layout is part of the ABI two units agree on, so it cannot ;; depend on what either file contains. `sex->c' has no prelude, so