1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-15 10:30:25 -08:00

Compile closures that modify their bound vars correctly (Bug#46834)

* lisp/emacs-lisp/bytecomp.el (byte-compile--reify-function): Don't
move let bindings into the lambda. Don't reverse list of
bindings. (byte-compile): Evaluate the return value if it was
previously reified.
* test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-reify-function):
Add tests.
This commit is contained in:
Pip Cet 2021-02-28 19:43:09 +00:00
parent b9cb3b9040
commit 2b069c67d7
2 changed files with 46 additions and 23 deletions

View file

@ -1199,6 +1199,29 @@ interpreted and compiled."
(should (equal (funcall (eval fun t)) '(c d)))
(should (equal (funcall (byte-compile fun)) '(c d))))))
(ert-deftest bytecomp-reify-function ()
"Check that closures that modify their bound variables are
compiled correctly."
(cl-letf ((lexical-binding t)
((symbol-function 'counter) nil))
(let ((x 0))
(defun counter () (cl-incf x))
(should (equal (counter) 1))
(should (equal (counter) 2))
;; byte compiling should not cause counter to always return the
;; same value (bug#46834)
(byte-compile 'counter)
(should (equal (counter) 3))
(should (equal (counter) 4)))
(let ((x 0))
(let ((x 1))
(defun counter () x)
(should (equal (counter) 1))
;; byte compiling should not cause the outer binding to shadow
;; the inner one (bug#46834)
(byte-compile 'counter)
(should (equal (counter) 1))))))
;; Local Variables:
;; no-byte-compile: t
;; End: