Opened 8 years ago
Closed 8 years ago
#1297 closed change request (duplicate)
variables defined inside closure survive beyond closure!
Reported by: | ai-artisan | Owned by: | |
---|---|---|---|
Priority: | not urgent at all | Milestone: | someday |
Component: | core libraries | Version: | 4.11.0 |
Keywords: | closure scope begin | Cc: | |
Estimated difficulty: |
Description
;;debug.scm
(require-extension r7rs)
(define r #t)
(define test (lambda ()
(if r
(begin
(set! r #f)
(define v 123)
(test)
(display v)
(newline)
)
(begin
(define v 234)
)
)
))
(test)
;; output: 234
;; The question is: Each time a procedure or partial block (like "let") is called, a closure should be created. Variables defined inside a closure should have a limited scope inside the closure unless "call/cc" or "call-with-values" is used. In the case above, none of them is used but the variable "v" survives beyond its closure created by "test" the 2nd time.
;; Further more, I found that only when "begin" is used will this bug appear.
Attachments (1)
Change History (5)
Changed 8 years ago by
comment:1 follow-up: 2 Changed 8 years ago by
Priority: | critical → minor |
---|
Internal definitions are only valid at the start of a body (see R5RS). You have definitions in invalid locations (begin
does not open a body), which CHICKEN allows but treats as global assignments.
The following, where internal definitions are correctly placed, prints 123 as you expect:
(define test (lambda () (if r (begin (set! r #f) (let () (define v 123) (test) (display v) (newline))) (let () (define v 234)))))
comment:3 Changed 8 years ago by
Priority: | minor → not urgent at all |
---|
comment:4 Changed 8 years ago by
Resolution: | → duplicate |
---|---|
Status: | new → closed |
This is exactly the same underlying issue as #1309
csi -w -s debug.scm