Subject: Re: bug#44103: 28.0.50; [feature/native-comp] let-bound dynamic variables are optimized away

From: Andrea Corallo <[email-masked]>
Date: Thu, 22 Oct 2020 20:46:57 +0000

Hi Kisaragi

[email-masked] writes:

> After trying for a few hours to decrease the size of the reproduction, this is all I can do...
>
> 1. Save this in testing.el: (bootstrap straight and install the package I was writing when I ran into the issue)
>
> (defvar bootstrap-version)
> (let ((bootstrap-file
>        (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
>       (bootstrap-version 5))
>   (unless (file-exists-p bootstrap-file)
>     (with-current-buffer
>         (url-retrieve-synchronously
>          "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
>          'silent 'inhibit-cookies)
>       (goto-char (point-max))
>       (eval-print-last-sexp)))
>   (load bootstrap-file nil 'nomessage))
>
> (straight-use-package '(kisaragi-log :host gitlab :repo "kisaragi-hiu/kisaragi-log"))
>
> 2. Evaluate it in a new Emacs instance:
>
> rm /tmp/.emacs.d; env HOME=/tmp emacs -Q -l testing.el
>
> 3. Go to the repository (/tmp/.emacs.d/straight/repos/kisaragi-log/) and check out commit 9cb4338.
>
> 4. Restart emacs and let Straight queue to build that version
>
> env HOME=/tmp emacs -Q -l testing.el
>
> 5. Write the data:
>
> (kisaragi-log-data-write '((a . "a") (b . "b")))
>
> 6. And look at "/tmp/.emacs.d/log.json": it's not pretty printed; this is somewhat reliably reproduced on my PC, but not
> on another commit with literally the same content.
>
> Looking at *Async-native-compile-log*, it does warn that it thinks json-encoding-pretty-print is an unused lexical
> variable.
>
> 7. If I add a `(require 'json)` to kisaragi-log-data.el, this doesn't happen, so perhaps this can be considered an error
> on my package's side.

Ah okay, this is certanly the issue.  If `json' is not required the
compiler doesn't know that `json-encoding-pretty-print' is a special
variable and the let should be dynamic.  You should see the same error
with byte compilation.

> In the org-roam case this can also explain it, because org-roam-db (the file that got org-mode-hook optimized out)
> doesn't require org itself, relying on a macro.
>
> (Despite trying to create a basically equivalent (but simpler) package, I'm still not able to reproduce the issue when I
> use (native-compile-async "/tmp/test" 'recursive t) to compile it. Straight.el compiles with 'late, but I don't know how
> to test that.)

Mmmh I don't think late-load should make a difference here.

So far I do not see bugs on the native compiler side.  Let me know if
there's something I can look into to help.

Thanks

  Andrea