bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#59057: Emacs 29. Byte compiler sometimes forgets about a defvar.


From: Alan Mackenzie
Subject: bug#59057: Emacs 29. Byte compiler sometimes forgets about a defvar.
Date: Tue, 8 Nov 2022 11:01:17 +0000

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 via email to

[Prev in Thread] Current Thread [Next in Thread]