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

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

bug#46387: 28.0.50; Compiled code making a variable dynamic stopped work


From: Stefan Monnier
Subject: bug#46387: 28.0.50; Compiled code making a variable dynamic stopped working
Date: Tue, 09 Feb 2021 13:48:50 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

>> Adding a variable to the context creates a new scope.
>> A `progn` should not introduce a new scope.
>
> All right, `defvar` modifies the current scope and only let, let* and
>  lambda create new scopes. Fine, but it leaves questions unanswered.

Oh, yes.

> * Does `defvar` affect new bindings only, or variable references in the 
> current scope as well?

Subsequent bindings.

>  (let ((my-var EXPR))
>    (defvar my-var)
>    (use my-var))

`defvar` here is "too late" to affect the earlier `let`.

> does the last line refer to the lexical my-var bound in the first
> line, or to the dynamic my-var?

If there's a lexical var in scope with the right name, that's what is
used, regardless if the variable is (currently) declared as dynamically
scoped or not.  E.g.

    (funcall (lambda (default-directory)
               (cd "/")
               default-directory)
             3)

returns 3 (in `lexical-binding` mode).

> * Does the defvar have to be 'executed' to be effective? That's how
> the interpreter works, but it clearly can't work in the compiler.

Your (if foo (defvar bar)) example indeed points to some of the other
murkiness, where the behavior differs between the interpreter and the
compiler, yes.

If it hurts, don't do that ;-)

> The defvar form probably has to 'precede' the binding form which it
> tries to affect, in some way.

A `defvar` should be effective on all subsequent equally or more
deeply-nested code.  Whether it also affects other subsequent code
is the less clear part.  `progn` (and macros to expand to `progn`, of
course) is the only "sure" exception (i.e. a `defvar` also affects the
code after its surrounding `progn`, if any).

>> The (with-suppressed-warnings (...) (defvar)) form is used at
>> several places.  It's the preferred way to declare a variable
>> dynamically scoped without incurring the "not prefixed" warning and
>> without making the `with-suppressed-warnings` silencer cover more code
>> than intended.
> Yes, but it does (currently) work if used on a single variable at a time,
> which is the suggested workaround for the time being.

It's better than nothing, but I think we really should fix the `progn`
case, because it's important.


        Stefan






reply via email to

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