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.
This commit is contained in:
2026-10-18 22:02:55 +03:00
parent fc5f532c0d
commit 315611ced0
2 changed files with 31 additions and 0 deletions

View File

@@ -421,6 +421,18 @@
(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