From 62d4626a1684ec7fd7d560cdf75f1b6a5f726c37 Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Thu, 30 Apr 2020 18:55:40 -0400 Subject: [PATCH 2/2] Don't increment array index in cl-loop twice (Bug#40727) * lisp/emacs-lisp/cl-macs.el (cl--parse-loop-clause): Put the temp-idx increment in cl--loop-body, leaving just the side-effect free testing of the index for both cl--loop-body and cl--loop-conditions. * test/lisp/emacs-lisp/cl-macs-tests.el (cl-macs-loop-and-arrays): Extend test to cover this case. --- lisp/emacs-lisp/cl-macs.el | 3 ++- test/lisp/emacs-lisp/cl-macs-tests.el | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el index 20bd1883c3..d6997ce0d2 100644 --- a/lisp/emacs-lisp/cl-macs.el +++ b/lisp/emacs-lisp/cl-macs.el @@ -1325,8 +1325,9 @@ cl--parse-loop-clause (temp-idx (make-symbol "--cl-idx--"))) (push (list temp-vec (pop cl--loop-args)) loop-for-bindings) (push (list temp-idx -1) loop-for-bindings) + (push `(setq ,temp-idx (1+ ,temp-idx)) cl--loop-body) (cl--push-clause-loop-body - `(< (setq ,temp-idx (1+ ,temp-idx)) (length ,temp-vec))) + `(< ,temp-idx (length ,temp-vec))) (if (eq word 'across-ref) (push (list var `(aref ,temp-vec ,temp-idx)) cl--loop-symbol-macs) diff --git a/test/lisp/emacs-lisp/cl-macs-tests.el b/test/lisp/emacs-lisp/cl-macs-tests.el index 77609a42a9..983e79ac57 100644 --- a/test/lisp/emacs-lisp/cl-macs-tests.el +++ b/test/lisp/emacs-lisp/cl-macs-tests.el @@ -43,6 +43,9 @@ cl-macs-loop-and-arrays "Bug#40727" (should (equal (cl-loop for y = (- (or x 0)) and x across [1 2] collect (cons x y)) + '((1 . 0) (2 . -1)))) + (should (equal (cl-loop for x across [1 2] and y = (- (or x 0)) + collect (cons x y)) '((1 . 0) (2 . -1))))) (ert-deftest cl-macs-loop-destructure () -- 2.11.0