unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: "Mattias Engdegård" <mattiase@acm.org>
Cc: Michael Heerdegen <michael_heerdegen@web.de>, 46387@debbugs.gnu.org
Subject: bug#46387: 28.0.50; Compiled code making a variable dynamic stopped working
Date: Tue, 09 Feb 2021 13:48:50 -0500	[thread overview]
Message-ID: <jwveehpqe7w.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <BF1AED06-83CB-4A0D-B8F8-043E56CF3A29@acm.org> ("Mattias Engdegård"'s message of "Tue, 9 Feb 2021 17:49:20 +0100")

>> 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	other threads:[~2021-02-09 18:48 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-08 17:29 bug#46387: 28.0.50; Compiled code making a variable dynamic stopped working Michael Heerdegen
2021-02-09 10:32 ` Mattias Engdegård
2021-02-09 15:31   ` Stefan Monnier
2021-02-09 16:49     ` Mattias Engdegård
2021-02-09 18:48       ` Stefan Monnier [this message]
2021-02-10  3:20   ` Michael Heerdegen
2021-02-10  8:22     ` Mattias Engdegård
2021-02-10 13:49       ` Mattias Engdegård
2021-02-10 22:44         ` Michael Heerdegen
2021-02-11  8:49           ` Mattias Engdegård

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=jwveehpqe7w.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=46387@debbugs.gnu.org \
    --cc=mattiase@acm.org \
    --cc=michael_heerdegen@web.de \
    /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).