unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Alan Mackenzie <acm@muc.de>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 59057@debbugs.gnu.org
Subject: bug#59057: Emacs 29. Byte compiler sometimes forgets about a defvar.
Date: Tue, 8 Nov 2022 11:01:17 +0000	[thread overview]
Message-ID: <Y2o2/UJeAShsSP+8@ACM> (raw)
In-Reply-To: <jwvbkpiv4qh.fsf-monnier+emacs@gnu.org>

Hello, Stefan.

On Mon, Nov 07, 2022 at 16:29:15 -0500, Stefan Monnier wrote:
> >> (defvar VAR) does not "create a variable".

> > According to both its doc string and the Elisp manual it does.

> Where?  I'm talking specifically about (defvar VAR) not about
> `defvar` in general.

The entry in the Elisp manual starts off:

     This special form defines SYMBOL as a variable.  Note that SYMBOL
     is not evaluated; the symbol to be defined should appear explicitly
     in the `defvar' form.  The variable is marked as "special", meaning
     that it should always be dynamically bound (see Variable
     Scoping).

"This special form defines SYMBOL as a variable.".  There are no ifs,
buts, whens, or whenevers in that sentence.  This is, at best, somewhat
confusing.

Note also: "... that it should ALWAYS be dynamically bound ...".

I suppose you're now going to say that "defining" a variable is
different from "creating" it.  And that you're going to say "always"
doesn't mean always.

From defvar's doc string:

    The `defvar' form also declares the variable as "special",
    so that it is ALWAYS dynamically bound even if `lexical-binding' is t.

Here, again, you're going to say that "always" only means "sometimes", I
think.

> >> It just "locally" declares that this identifier should use dynamic
> >> scoping when binding a variable.
> > Why "locally"?  There's just one obarray involved, which is global.

> No, we want to allow `defvar` to have a lexically-local effect.

Why?  What's the use case?  This, again, is contradictory: a defvar with
a value has global effect, but without a value has only lexical scope.
This doesn't make sense.  Why should dynamically bound variables have
lexical scope?  Could it be the enthusiasm for lexical binding has burst
beyond its natural limits?

> >> But the better way to write the above code is:

> >>     (defmacro acm-defvar (var)
> >>       `(progn
> >>          (defvar ,var)
> >>          (eval-when-compile
> >>            (when (version-check)
> >>              (setq ,var emacs-major-version)))))

> > There are workarounds, yes.  But surely it would be better to fix the
> > bug.

> It's not a workaround.  It's how the programmers say whether they want
> the (defvar ,var) declaration to affect all the rest of the code or only
> the code within the `when`.

I think programmers just want defvar to behave sensibly and
consistently, and in accordance with the documentation.  The recently
introduced special handling of defvar without a value is not welcome.
Why should anybody want a dynamic variable later to be a lexically
scoped variable?.  

> Why put the (defvar ,var) within the `when` if not to limit
> its scope?

To limit when it will create the variable, according to the value of the
enclosing `if' form.  Just like if you do a (setq my-global some-value),
that has effect outside of the lexical scope.

In my test program, l-s-p is created with a dynamic binding, but later
on it got a confusing warning about it being unused as a lexically bound
variable.  Nobody wants this, surely?

>         Stefan

-- 
Alan Mackenzie (Nuremberg, Germany).





  reply	other threads:[~2022-11-08 11:01 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-05 18:46 bug#59057: Emacs 29. Byte compiler sometimes forgets about a defvar Alan Mackenzie
2022-11-07 13:07 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-07 20:20   ` Alan Mackenzie
2022-11-07 21:29     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-08 11:01       ` Alan Mackenzie [this message]
2022-11-08 14:12         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-07 21:31     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-09 13:40       ` Alan Mackenzie
2022-11-09 17:54         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-24 19:29           ` Stefan Kangas
2022-11-24 19:33             ` Eli Zaretskii
2022-11-24 20:36             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-24 21:12               ` Stefan Kangas
2022-11-25 10:36                 ` Alan Mackenzie

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=Y2o2/UJeAShsSP+8@ACM \
    --to=acm@muc.de \
    --cc=59057@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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).