From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.bugs Subject: bug#51982: Erroneous handling of local variables in byte-compiled nested lambdas Date: Sun, 21 Nov 2021 10:59:15 +0100 Message-ID: <59A729EF-C4D4-47EB-9ADC-19FE8EBE7F10@acm.org> References: <87y25jo2q1.fsf@web.de> <29C3A3F8-CD9F-4AF2-A731-3304FC30E380@acm.org> <87wnl23pnd.fsf@web.de> Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.21\)) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17810"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Paul Pogonyshev , 51982@debbugs.gnu.org, Stefan Monnier To: Michael Heerdegen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Nov 21 11:00:15 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mojdv-0004PM-G5 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 21 Nov 2021 11:00:15 +0100 Original-Received: from localhost ([::1]:53140 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mojdu-0001kc-Bz for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 21 Nov 2021 05:00:14 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:53710) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mojdi-0001hT-Q3 for bug-gnu-emacs@gnu.org; Sun, 21 Nov 2021 05:00:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:60950) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mojdi-0001EG-Fi for bug-gnu-emacs@gnu.org; Sun, 21 Nov 2021 05:00:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mojdi-0001wF-Ak for bug-gnu-emacs@gnu.org; Sun, 21 Nov 2021 05:00:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 21 Nov 2021 10:00:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51982 X-GNU-PR-Package: emacs Original-Received: via spool by 51982-submit@debbugs.gnu.org id=B51982.16374887697381 (code B ref 51982); Sun, 21 Nov 2021 10:00:02 +0000 Original-Received: (at 51982) by debbugs.gnu.org; 21 Nov 2021 09:59:29 +0000 Original-Received: from localhost ([127.0.0.1]:44263 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mojdB-0001uy-Bj for submit@debbugs.gnu.org; Sun, 21 Nov 2021 04:59:29 -0500 Original-Received: from mail1469c50.megamailservers.eu ([91.136.14.69]:49570 helo=mail102c50.megamailservers.eu) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mojd8-0001uc-EQ for 51982@debbugs.gnu.org; Sun, 21 Nov 2021 04:59:28 -0500 X-Authenticated-User: mattiase@bredband.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=megamailservers.eu; s=maildub; t=1637488758; bh=q1l8jKhOV3xDgWeAc2bqx0SGfFYbq+6S/fvRt21tBOg=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=Iqq3oWPSd0OLk+s2S4y0whjK4GiDnvwo8223j5IiAD/rwXZc01npDu/ajak5OGvYF A7XWZJ3X/n5ZR1+iXzUhozNBQjBlzVFxnC3W7OI62xmOhhmM75IxTNjI8W/9elxy4g xE3dDCLvHIwz30hdJz4+fB3m60cacUQic5r/BZUM= Feedback-ID: mattiase@acm.or Original-Received: from [192.168.0.4] (c188-150-171-71.bredband.tele2.se [188.150.171.71]) (authenticated bits=0) by mail102c50.megamailservers.eu (8.14.9/8.13.1) with ESMTP id 1AL9xG6q005184; Sun, 21 Nov 2021 09:59:17 +0000 In-Reply-To: <87wnl23pnd.fsf@web.de> X-Mailer: Apple Mail (2.3445.104.21) X-CTCH-RefID: str=0001.0A742F23.619A1876.0060, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0 X-CTCH-VOD: Unknown X-CTCH-Spam: Unknown X-CTCH-Score: 0.000 X-CTCH-Flags: 0 X-CTCH-ScoreCust: 0.000 X-CSC: 0 X-CHA: v=2.4 cv=Pr3w0yA3 c=1 sm=1 tr=0 ts=619a1876 a=SF+I6pRkHZhrawxbOkkvaA==:117 a=SF+I6pRkHZhrawxbOkkvaA==:17 a=kj9zAlcOel0A:10 a=M51BFTxLslgA:10 a=NivrlyhRsm14kqCvc6QA:9 a=CjuIK1q_8ugA:10 X-Origin-Country: SE X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:220549 Archived-At: 21 nov. 2021 kl. 08.59 skrev Michael Heerdegen = : > If arbitrary fixes would be allowed at any point of the development = cycle > the result would be worse than what we have now. It's also a quite > common policy. Yes, but it does cause some inconvenience in a project with Emacs's = geological release pace. It is difficult to defend an inability to = rapidly release an updated version when a serious bug has been found. Now, regarding the actual bug. Consider the function 1 (defun f (x) 2 (lambda () 3 (let ((g (lambda () x))) 4 (let ((x 'a)) 5 (list x (funcall g)))))) First of all, the variable x is free in the function starting in line 2, = so that function is converted to a closure capturing that variable = explicitly. Next, the function bound to g will be lambda-lifted; ie, converted to = (lambda (x) x) which means that the call to g in line 5 must be amended = to include the value of x. However, we can't just change (funcall g) to = (funcall g x) because x is shadowed by the binding in 4, so a new = variable is introduced for this purpose. The result after cconv is essentially (without the fix): 1 (defun f (x) 2 (internal-make-closure nil (x) nil 3 (let ((g (lambda (x) x))) 4 (let ((x 'a) 5 (closed-x x)) 6 (list x (funcall g closed-x)))))) But x is not the right expression for closed-x, because it is a captured = variable. The patch fixes this: 1 (defun f (x) 2 (internal-make-closure nil (x) nil 3 (let ((g (lambda (x) x))) 4 (let ((x 'a) 5 (closed-x (internal-get-closed-var 0))) 6 (list x (funcall g closed-x)))))) As I mentioned previously, it would be probably be better to elide = closed-x entirely and produce 1 (defun f (x) 2 (internal-make-closure nil (x) nil 3 (let ((g (lambda (x) x))) 4 (let ((x 'a)) 5 (list x (funcall g (internal-get-closed-var 0))))))) In other words, the bug occurs when a variable is captured, = lambda-lifted, and shadowed. I need to take another good look at the code to make sure the change is = correct (more eyes on it would be appreciated).