From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.devel Subject: Trouble with lexical-binding. Date: Mon, 13 Apr 2015 22:03:17 +0000 Message-ID: <20150413220317.GC6324@acm.fritz.box> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1428962625 14568 80.91.229.3 (13 Apr 2015 22:03:45 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 13 Apr 2015 22:03:45 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Apr 14 00:03:37 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YhmS7-00062w-N5 for ged-emacs-devel@m.gmane.org; Tue, 14 Apr 2015 00:03:35 +0200 Original-Received: from localhost ([::1]:53528 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YhmS6-0004Nw-Oj for ged-emacs-devel@m.gmane.org; Mon, 13 Apr 2015 18:03:34 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:42844) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YhmRu-0004No-HL for emacs-devel@gnu.org; Mon, 13 Apr 2015 18:03:23 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YhmRr-000823-CR for emacs-devel@gnu.org; Mon, 13 Apr 2015 18:03:22 -0400 Original-Received: from colin.muc.de ([193.149.48.1]:31341 helo=mail.muc.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YhmRr-00081x-2L for emacs-devel@gnu.org; Mon, 13 Apr 2015 18:03:19 -0400 Original-Received: (qmail 54910 invoked by uid 3782); 13 Apr 2015 22:03:17 -0000 Original-Received: from acm.muc.de (p548A4102.dip0.t-ipconnect.de [84.138.65.2]) by colin.muc.de (tmda-ofmipd) with ESMTP; Tue, 14 Apr 2015 00:03:17 +0200 Original-Received: (qmail 7349 invoked by uid 1000); 13 Apr 2015 22:03:17 -0000 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x X-Received-From: 193.149.48.1 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:185387 Archived-At: Hello, Emacs. I recently tried out M-x auto-insert in a file.el, and it caused a testing macro to fail. The critical point was the default setting of lexical-binding to t in the first line. Reduced to its essentials, my problem is a file looking like this: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; lexical-bug.el --- lexical bug -*- lexical-binding: t; -*- (eval-when-compile (defmacro test-ptr (x) `(let* ((ptr (copy-tree ,x)) (form '(setcar ptr 'a)) (result (eval form))) (message "result is %s, ptr is %s" result ptr)))) (test-ptr '(x y z)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; When I byte-compile it, I expect on loading it to get the result message in the message area. This works fine without the lexical binding. With the LB, instead I get an error about `ptr' being unbound - clearly, in the form `(eval form)', i.e. `(eval `(setcar ptr 'a))', there is no `ptr' in `eval''s stack frame. Obviously, this bit of the code needs dynamic binding. So I should be able to bind `lexical-binding' to nil at some strategic place to achieve this. I'm not quite sure where this place is, but nowhere seems to work. For example, if I change the file to the following: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; lexical-bug.el --- lexical bug -*- lexical-binding: t; -*- (setq lexical-binding nil) ; 1 (eval-when-compile (let (lexical-binding) ; 2 (defmacro test-ptr (x) (let (lexical-binding ; 3 ) `(let* (lexical-binding ; 4 (ptr (copy-tree ,x)) (form '(setcar ptr 'a)) (result (eval form))) (message "result is %s, ptr is %s" result ptr)))))) (eval-when-compile (setq lexical-binding nil)) ; 5 (setq lexical-binding nil) ; 6 (test-ptr '(x y z)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; , I still get an error with `ptr' being unbound. What is going on here? Why is the byte compiler continuing to use lexical binding despite the variable being set to nil in six different ways? What do I have to do to get `ptr' in this context a special variable (whilst still having it lexically bound in other code)? -- Alan Mackenzie (Nuremberg, Germany).