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

From: Stefan Monnier <[email-masked]>
Date: Mon, 07 Nov 2022 08:07:28 -0500

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

(defvar VAR) does not "create a variable".  It just "locally" declares
that this identifier should use dynamic scoping when binding a variable.

Before the commit you mentioned, the above (defvar ,var) would sometimes
incorrectly escape its `when` subexpression and affect subsequent code,
which is why in the past the presence of `when` did not make much
difference whereas it now does.

Note also that for the same reason the above should arguably never work
at all since the effect of (defvar ,var) should not escape the
`eval-when-compile` subexpression.  A lot of code relies on this
misbehavior, tho, so we still support it.  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)))))

> This bug doesn't occur on Emacs 28.
> I have a feeling it was introduced into Emacs 29 very recently.

The Emacs-28 behavior was a misbehavior, which got fixed accidentally
recently :-)

        Stefan