Opened 3 years ago

Closed 3 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)

debug.scm (699 bytes) - added by ai-artisan 3 years ago.
csi -w -s debug.scm

Download all attachments as: .zip

Change History (5)

Changed 3 years ago by ai-artisan

Attachment: debug.scm added

csi -w -s debug.scm

comment:1 Changed 3 years ago by evhan

Priority: criticalminor

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:2 in reply to:  1 Changed 3 years ago by ai-artisan

Thx. I just referred to the R7RS. You r right. I made a mistake. _

comment:3 Changed 3 years ago by ai-artisan

Priority: minornot urgent at all

comment:4 Changed 3 years ago by sjamaan

Resolution: duplicate
Status: newclosed

This is exactly the same underlying issue as #1309

Note: See TracTickets for help on using tickets.