GNU bug report logs -
#46834
28.0.50; byte-compiling the standard counter closure fails
Previous Next
Reported by: Pip Cet <pipcet <at> gmail.com>
Date: Sun, 28 Feb 2021 18:08:02 UTC
Severity: normal
Found in version 28.0.50
Done: Pip Cet <pipcet <at> gmail.com>
Bug is archived. No further changes may be made.
Full log
Message #20 received at 46834 <at> debbugs.gnu.org (full text, mbox):
>>> (byte-compile (let ((l 0)) (lambda () (cl-incf l))))
> Huh, that's such a standard example of using closures that it's
> surprising that we haven't tripped on this before... but I guess we
> don't really write code like that much in Emacs. (I can confirm that
> the test case doesn't work in Emacs 28.)
The problem is not in the way the actual byte compiler handles such code
(e.g. when you compile a whole file, which works just fine), it's just
for the special case where we pass to `byte-compile` a function *value*
rather than an *expression* (in which case, `byte-compile` first needs
to turn this value back into a corresponding expression).
This is a very special situation, whose main use case is when you
do `(byte-compile 'SYMBOL)` and which we don't handle 100%
correctly, anyway.
E.g. (using lexical-binding, of course):
M-: (let ((x 1))
(defun counter1 () (cl-incf x))
(defun counter2 () (cl-incf x))) RET
then do
M-x byte-compile RET counter1 RET
and then try
M-: (list (counter1) (counter2) (counter1) (counter2))
and notice that the counter is not shared between the two functions :-(
> I've added Stefan M to the CCs; perhaps he has some comments.
Thanks,
Stefan
This bug report was last modified 4 years and 86 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.