unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Kelly Dean <kelly@prtime.org>
Cc: emacs-devel@gnu.org
Subject: Re: Why is Elisp's defvar weird? And is eval_sub broken?
Date: Fri, 13 Feb 2015 14:03:53 -0500	[thread overview]
Message-ID: <jwvmw4h7ie6.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <ahbMLilkT3oQCaVG8BAEdfi0yuqdTKYRS7W2elK3K4r@local> (Kelly Dean's message of "Thu, 12 Feb 2015 21:32:01 +0000")

> 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 <foo>) 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



  reply	other threads:[~2015-02-13 19:03 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-12 21:32 Why is Elisp's defvar weird? And is eval_sub broken? Kelly Dean
2015-02-13 19:03 ` Stefan Monnier [this message]
2015-02-14  7:35   ` Kelly Dean
2015-02-14 14:36     ` Stefan Monnier
2015-02-15 14:17       ` Daniel Colascione
2015-02-16  5:42       ` Kelly Dean
2015-02-16  7:40         ` Stefan Monnier
2015-02-17 23:39           ` Kelly Dean
2015-02-18 22:29             ` Stefan Monnier
2015-02-19 10:32               ` Kelly Dean
2015-02-19 13:23                 ` Stefan Monnier
2015-02-20  0:11                   ` Kelly Dean
2015-02-20  2:02                     ` Stefan Monnier
2015-02-22  4:11                       ` Proposal for a closed-buffer tracker Kelly Dean
2015-02-22 15:53                         ` Eli Zaretskii
2015-02-22 22:03                           ` Stefan Monnier
2015-02-22 22:23                             ` Dmitry Gutov
2015-02-23 13:53                               ` Artur Malabarba
2015-02-23 16:44                                 ` Eli Zaretskii
2015-02-22 21:59                         ` Stefan Monnier
2015-02-28 10:15                         ` Artur Malabarba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvmw4h7ie6.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=emacs-devel@gnu.org \
    --cc=kelly@prtime.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).