refuse a closure capturing one name twice

check-capture-names held captures against the parameters and the body,
not against each other: (n n) gave C two members and two parameters n.
This commit is contained in:
2026-10-27 22:41:06 +03:00
parent acbd4928f5
commit 28aaaf637c
2 changed files with 8 additions and 0 deletions

View File

@@ -2031,6 +2031,11 @@
(when (memq name parameters) (when (memq name parameters)
(sex-error form "a capture and a parameter share a name" name))) (sex-error form "a capture and a parameter share a name" name)))
names) names)
(let twice ((names names))
(when (pair? names)
(when (memq (car names) (cdr names))
(sex-error form "two captures share a name" (car names)))
(twice (cdr names))))
(for-each (lambda (statement) (for-each (lambda (statement)
(match statement (match statement
(('var (? symbol? name) . _) (('var (? symbol? name) . _)

View File

@@ -498,6 +498,9 @@
(test-assert "a capture named like a parameter is refused" (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)))") (reports? (in-fn "(var c (closure ((int)) int) (closure ((a int)) int (a) (return a)))")
"a capture and a parameter share a name")) "a capture and a parameter share a name"))
(test-assert "so is one captured twice"
(reports? (in-fn "(var c (closure () int) (closure () int (a a) (return a)))")
"two captures share a name"))
(test-assert "so is one redeclared at the top of the body" (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)))") (reports? (in-fn "(var c (closure () int) (closure () int (a) (var a int 5) (return a)))")
"the body redeclares a capture")) "the body redeclares a capture"))