Subject: Re: bug#55137: Different result when interpreted and when evaluating byte-compiled code

From: Paul Pogonyshev <[email-masked]>
Date: Wed, 27 Apr 2022 13:33:57 +0200

I understand what's happening, I just find this extremely confusing. Can
byte-compilation not notice that there is a `defvar' form and thus count
the variable as special also during compilation?

> Try this:

This cannot be done in the real cases where things break, because they
(`iter-defun', for example) _need_ to know if the variable is special at
the time macro gets expanded (they call internal helper functions that call
`special-variable-p' in turn).

The only workaround I'm aware of is putting `defvar' into an
`eval-and-compile' form (or into a different file, as `require' implicitly
does that). I consider this a workaround rather than a proper solution
since it is very confusing and not even apparent to many users (pretty much
to most of those who never stumbled into this issue). Also, resulting
errors are not self-explanatory and can be anything, including
incomprehensible and seemingly unrelated failures at runtime .
Additionally, you need to know if said variable is going to be used in a
macro that uses `special-variable-p' in its expansion code; in particular,
you need to know the list of such macros (I currently know two related
real-word examples: `iter-defun' and friends from the standard feature
`generator' and `iter2-defun' and friends from library `iter2'). Normally,
you don't even think about putting `defvar' into an `eval-and-compile'.

As mentioned, I think that byte-compilation should be improved to mark
variables as special during compilation time too, so that there is no need
in `eval-and-compile'. I.e. even if this is not considered a bug, it should
count as a feature request.

Paul

On Wed, 27 Apr 2022 at 13:20, Phil Sainty <[email-masked]> wrote:

> On 2022-04-27 10:16, Paul Pogonyshev wrote:
> >     (defmacro is-special-as-macro ()
> >       (special-variable-p 'special-variable))
>
> This macro does not expand to code which calls `special-variable-p'.
> Rather it calls `special-variable-p' at expansion time, and expands
> to the return value of that call (nil or t).
>
> If you byte-compile your code without loading it, then your would-be
> `special-variable' doesn't exist, and hence your macro was expanding
> to nil rather than t.
>
> Try this:
>
>       (defmacro is-special-as-macro ()
>         '(special-variable-p 'special-variable))
>
>
> -Phil
>
>