From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.bugs Subject: bug#29520: peval leaves behind dangling lexical reference Date: Sun, 03 Dec 2017 17:05:14 -0500 Message-ID: <87shcrxyat.fsf@netris.org> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1512338777 30042 195.159.176.226 (3 Dec 2017 22:06:17 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 3 Dec 2017 22:06:17 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) Cc: 29520@debbugs.gnu.org To: Stefan Israelsson Tampe Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Sun Dec 03 23:06:13 2017 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eLcOn-0007J2-9n for guile-bugs@m.gmane.org; Sun, 03 Dec 2017 23:06:09 +0100 Original-Received: from localhost ([::1]:40321 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eLcOt-0000kJ-4J for guile-bugs@m.gmane.org; Sun, 03 Dec 2017 17:06:15 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55114) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eLcOj-0000jE-5I for bug-guile@gnu.org; Sun, 03 Dec 2017 17:06:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eLcOf-0006EH-WA for bug-guile@gnu.org; Sun, 03 Dec 2017 17:06:05 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:35219) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eLcOf-0006EC-Ry for bug-guile@gnu.org; Sun, 03 Dec 2017 17:06:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1eLcOf-0000og-KE for bug-guile@gnu.org; Sun, 03 Dec 2017 17:06:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Mark H Weaver Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Sun, 03 Dec 2017 22:06:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 29520 X-GNU-PR-Package: guile X-GNU-PR-Keywords: Original-Received: via spool by 29520-submit@debbugs.gnu.org id=B29520.15123387443114 (code B ref 29520); Sun, 03 Dec 2017 22:06:01 +0000 Original-Received: (at 29520) by debbugs.gnu.org; 3 Dec 2017 22:05:44 +0000 Original-Received: from localhost ([127.0.0.1]:43900 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eLcOO-0000o5-9q for submit@debbugs.gnu.org; Sun, 03 Dec 2017 17:05:44 -0500 Original-Received: from world.peace.net ([50.252.239.5]:60062) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eLcOI-0000ng-W1; Sun, 03 Dec 2017 17:05:39 -0500 Original-Received: from pool-72-93-29-217.bstnma.east.verizon.net ([72.93.29.217] helo=jojen) by world.peace.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1eLcOC-0007RG-Ev; Sun, 03 Dec 2017 17:05:32 -0500 In-Reply-To: (Stefan Israelsson Tampe's message of "Fri, 1 Dec 2017 13:08:46 +0100") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.org gmane.lisp.guile.bugs:8922 Archived-At: retitle 29520 peval leaves behind dangling lexical reference thanks Stefan Israelsson Tampe writes: > Consider the code at the end of this post. un-commenting f-scope > reveals the compiler error: > > ;;; ERROR: unbound lexical # Indeed, I can confirm that 'peval' has a faulty optimization that leaves behind a dangling lexical reference to 'x' with no definition. Here's the relevant excerpt of Stefan's example code, indented: (define-syntax-rule ( (c) code ...) (lambda (a b cc d c) code .= ..)) (define-syntax .. (syntax-rules () ((.. (f a ...)) (f x y z a ...)) ((.. (s ...) (f a ...)) (f x y z a ...)))) =20=20 (define (f-scope f) (define (g f x3) (define (h x2 n m) (lambda xx (apply (f-skip n m) x2))) ( (c) (.. (c2) (f c)) (let ((n N) (m M)) (.. ((h x3 n m) c2))))) =20=20 (lambda x (apply (g f x) x))) When 'peval' optimizes 'f-scope', it inlines the calls to 'g' and 'h', but somewhere along the way the binding for 'x' gets lost, although a reference to 'x' still remains within the inlined instances of 'g' and 'h'. This error in the result of 'peval' is detected by 'verify-tree-il'. See below for a debugging transcript. --8<---------------cut here---------------start------------->8--- mhw@jojen ~$ guile GNU Guile 2.2.2 Copyright (C) 1995-2017 Free Software Foundation, Inc. Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'. This program is free software, and you are welcome to redistribute it under certain conditions; type `,show c' for details. Enter `,help' for help. scheme@(guile-user)> (define-syntax-rule ( (c) code ...) (lambda = (a b cc d c) code ...)) scheme@(guile-user)> (define-syntax .. (syntax-rules () ((.. (f a ...)) (f x y z a ...)) ((.. (s ...) (f a ...)) (f x y z a ...)))) scheme@(guile-user)> (define (f-scope f) (define (g f x3) (define (h x2 n m) (lambda xx (apply (f-skip n m) x2))) ( (c) (.. (c2) (f c)) (let ((n N) (m M)) (.. ((h x3 n m) c2))))) =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 (lambda x (apply (g f x) x))) ;;; :11:45: warning: possibly unbound variable `f-skip' ;;; :13:37: warning: possibly unbound variable `x' ;;; :13:37: warning: possibly unbound variable `y' ;;; :13:37: warning: possibly unbound variable `z' ;;; :14:37: warning: possibly unbound variable `N' ;;; :14:37: warning: possibly unbound variable `M' ;;; :15:39: warning: possibly unbound variable `x' ;;; :15:39: warning: possibly unbound variable `y' ;;; :15:39: warning: possibly unbound variable `z' ;;; :15:39: warning: possibly unbound variable `c2' While compiling expression: ERROR: unbound lexical # scheme@(guile-user)> (use-modules (system base compile) (language tree-il) (language tree-il primitives) (language tree-il peval) (language tree-il debug)) scheme@(guile-user)> (compile '(define (f-scope f) (define (g f x3) (define (h x2 n m) (lambda xx (apply (f-skip n m) x2))) ( (c) (.. (c2) (f c)) (let ((n N) (m M)) (.. ((h x3 n m) c2))))) =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20 (lambda x (apply (g f x) x))) #:to 'tree-il #:env (current-module)) $1 =3D # scheme@(guile-user)> (expand-primitives (resolve-primitives $1 (current-mod= ule))) $2 =3D # scheme@(guile-user)> ,pp (decompile $2 #:env (current-module)) $3 =3D (define (f-scope f) (define (g f x3) (define (h x2 n m) (lambda xx (apply (f-skip n m) x2))) (lambda (a b cc d c) (f x y z c) (let ((n N) (m M)) ((h x3 n m) x y z c2)))) (lambda x (apply (g f x) x))) $4 =3D # scheme@(guile-user)> (peval $2) $5 =3D # scheme@(guile-user)> (verify-tree-il $5) ERROR: In procedure scm-error: ERROR: unbound lexical # Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue. scheme@(guile-user) [1]> ,q scheme@(guile-user)> (make-let #f '(x) '(#{x 945}#) (list (make-const #f 'D= UMMY)) $5) $6 =3D # scheme@(guile-user)> (verify-tree-il $6) $7 =3D # scheme@(guile-user)> ,pp (decompile $6 #:env (current-module)) $8 =3D (let ((x-1 'DUMMY)) (define (f-scope f) (lambda (a b cc d c) (f x y z c) (let ((n N) (m M)) (begin x y z c2 (if #f #f)) (apply (f-skip n m) x-1))))) $9 =3D # scheme@(guile-user)>=20 --8<---------------cut here---------------end--------------->8--- Above, I wrapped the faulty code with an extra 'let' to bind the dangling reference, to allow the decompiler to run. So, peval is optimizing this: (define (f-scope f) (define (g f x3) (define (h x2 n m) (lambda xx (apply (f-skip n m) x2))) (lambda (a b cc d c) (f x y z c) (let ((n N) (m M)) ((h x3 n m) x y z c2)))) (lambda x (apply (g f x) x))) into this: (define (f-scope f) (lambda (a b cc d c) (f x y z c) (let ((n N) (m M)) (begin x y z c2 (if #f #f)) (apply (f-skip n m) x-1)))) To be continued... Mark