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:
19
semen.scm
19
semen.scm
@@ -1822,6 +1822,7 @@
|
|||||||
(define (lift-closure base form env)
|
(define (lift-closure base form env)
|
||||||
(match form
|
(match form
|
||||||
(('closure arglist ret-type captures . body)
|
(('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))
|
(let* ((caps (map (lambda (c) (capture-binding c form env)) captures))
|
||||||
(record (suffixed base "_captures"))
|
(record (suffixed base "_captures"))
|
||||||
(code (suffixed base "_code"))
|
(code (suffixed base "_code"))
|
||||||
@@ -1864,6 +1865,24 @@
|
|||||||
(return ƛc))))))))
|
(return ƛc))))))))
|
||||||
(else (sex-error form "malformed closure" form))))
|
(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
|
;;; 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
|
;;; 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.
|
;;; const one. (* const char) points at const, and is left alone.
|
||||||
|
|||||||
@@ -421,6 +421,18 @@
|
|||||||
(test-assert "the type becomes a struct named for its signature"
|
(test-assert "the type becomes a struct named for its signature"
|
||||||
(emits? "(fn f ((c (closure ((int)) int))) int (return (c 1)))"
|
(emits? "(fn f ((c (closure ((int)) int))) int (return (c 1)))"
|
||||||
"struct ƛint_int"))
|
"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 --
|
;; The environment is one shared union, declared in the prelude --
|
||||||
;; its layout is part of the ABI two units agree on, so it cannot
|
;; 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
|
;; depend on what either file contains. `sex->c' has no prelude, so
|
||||||
|
|||||||
Reference in New Issue
Block a user