From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Why is Elisp's defvar weird? And is eval_sub broken? Date: Fri, 13 Feb 2015 14:03:53 -0500 Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1423854278 4604 80.91.229.3 (13 Feb 2015 19:04:38 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 13 Feb 2015 19:04:38 +0000 (UTC) Cc: emacs-devel@gnu.org To: Kelly Dean Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Feb 13 20:04:29 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 1YMLXQ-0001gD-3G for ged-emacs-devel@m.gmane.org; Fri, 13 Feb 2015 20:04:28 +0100 Original-Received: from localhost ([::1]:57158 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YMLXP-0004JY-Ej for ged-emacs-devel@m.gmane.org; Fri, 13 Feb 2015 14:04:27 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:51605) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YMLXM-0004JG-U1 for emacs-devel@gnu.org; Fri, 13 Feb 2015 14:04:25 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YMLXH-0001ET-NE for emacs-devel@gnu.org; Fri, 13 Feb 2015 14:04:24 -0500 Original-Received: from mercure.iro.umontreal.ca ([132.204.24.67]:49031) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YMLXH-0001EC-Ic for emacs-devel@gnu.org; Fri, 13 Feb 2015 14:04:19 -0500 Original-Received: from hidalgo.iro.umontreal.ca (hidalgo.iro.umontreal.ca [132.204.27.50]) by mercure.iro.umontreal.ca (Postfix) with ESMTP id 16D6F85CDD; Fri, 13 Feb 2015 14:04:17 -0500 (EST) Original-Received: from lechon.iro.umontreal.ca (lechon.iro.umontreal.ca [132.204.27.242]) by hidalgo.iro.umontreal.ca (Postfix) with ESMTP id B2A311E5B8A; Fri, 13 Feb 2015 14:03:53 -0500 (EST) Original-Received: by lechon.iro.umontreal.ca (Postfix, from userid 20848) id 98A0DB4102; Fri, 13 Feb 2015 14:03:53 -0500 (EST) In-Reply-To: (Kelly Dean's message of "Thu, 12 Feb 2015 21:32:01 +0000") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) X-DIRO-MailScanner-Information: Please contact the ISP for more information X-DIRO-MailScanner: Found to be clean X-DIRO-MailScanner-SpamCheck: n'est pas un polluriel, SpamAssassin (score=-2.82, requis 5, autolearn=not spam, ALL_TRUSTED -2.82, MC_TSTLAST 0.00) X-DIRO-MailScanner-From: monnier@iro.umontreal.ca X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 132.204.24.67 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:183017 Archived-At: > But that's wrong. If INITVALUE is missing, and lexical-binding is > t (as is the case in desktop.el), then not only is the value not set, > but also the variable is _not_ declared special, even if the defvar > is at top level. The declaration of the var as being dynamically-scoped (aka "special") is *local* to the (rest of the) current scope (typically the current file). This is indispensable so that one package can use a dynamically-bound variable `foo' without breaking some other package that expects `foo' to be lexically-bound. Normally, such conflicts should never happen because all special vars should be named with a "package prefix", but sadly, reality is different, so it was indispensable to make this effect local, to allow lexical-binding code to work reliably. > That means that even after loading desktop.el, if you let-bind the > three variables above in a function defined in a file other than > desktop.el, and lexical-binding is t in that other file, then those > variables will be bound lexically, not dynamically. That's right. If you're lucky (more specifically, if you only let-bind those vars but you don't use them locally), the byte-compiler will emit a warning that those let-bindings aren't used (which is usually a sign that you need to add a (defvar ) earlier in the file). > That's because eval_sub in eval.c looks up the variable in the lexical > environment using only Fassq, without first using Fmemq to check for > a local dynamic binding. Is that behavior actually correct? I wouldn't argue it's correct, but I'd rather not pay the price of an additional memq check to cater to such brain-dead misuse of defvar. Arguably, the byte-compiler should flag such misuse, tho currently it misses it (tho it does catch the case of: (let ((my-foo 0)) (let ((my-foo 1)) my-foo)) (defvar my-foo) -- Stefan