aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNoam Postavsky2020-04-30 07:54:49 -0400
committerNoam Postavsky2020-05-07 08:23:56 -0400
commitde7158598fcd5440c0180ff6f83052c29e490bcd (patch)
tree8e0c7b89a7c0e6b738228a9b4cba3708bd9d8afe
parent2c905fb8a1d95a72e4b8a9b138458c86b099ced1 (diff)
downloademacs-de7158598fcd5440c0180ff6f83052c29e490bcd.tar.gz
emacs-de7158598fcd5440c0180ff6f83052c29e490bcd.zip
Revert "cl-loop: Calculate the array length just once"
It fails when using 'and' (parallel bindings) for arrays (Bug#40727). * lisp/emacs-lisp/cl-macs.el (cl--parse-loop-clause): Revert to recomputing array length. * test/lisp/emacs-lisp/cl-macs-tests.el (cl-macs-loop-and-arrays): New test.
-rw-r--r--lisp/emacs-lisp/cl-macs.el14
-rw-r--r--test/lisp/emacs-lisp/cl-macs-tests.el6
2 files changed, 10 insertions, 10 deletions
diff --git a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el
index 4408bb58464..fef8786b599 100644
--- a/lisp/emacs-lisp/cl-macs.el
+++ b/lisp/emacs-lisp/cl-macs.el
@@ -1317,13 +1317,11 @@ For more details, see Info node `(cl)Loop Facility'.
1317 1317
1318 ((memq word '(across across-ref)) 1318 ((memq word '(across across-ref))
1319 (let ((temp-vec (make-symbol "--cl-vec--")) 1319 (let ((temp-vec (make-symbol "--cl-vec--"))
1320 (temp-len (make-symbol "--cl-len--"))
1321 (temp-idx (make-symbol "--cl-idx--"))) 1320 (temp-idx (make-symbol "--cl-idx--")))
1322 (push (list temp-vec (pop cl--loop-args)) loop-for-bindings) 1321 (push (list temp-vec (pop cl--loop-args)) loop-for-bindings)
1323 (push (list temp-len `(length ,temp-vec)) loop-for-bindings)
1324 (push (list temp-idx -1) loop-for-bindings) 1322 (push (list temp-idx -1) loop-for-bindings)
1325 (cl--push-clause-loop-body 1323 (cl--push-clause-loop-body
1326 `(< (setq ,temp-idx (1+ ,temp-idx)) ,temp-len)) 1324 `(< (setq ,temp-idx (1+ ,temp-idx)) (length ,temp-vec)))
1327 (if (eq word 'across-ref) 1325 (if (eq word 'across-ref)
1328 (push (list var `(aref ,temp-vec ,temp-idx)) 1326 (push (list var `(aref ,temp-vec ,temp-idx))
1329 cl--loop-symbol-macs) 1327 cl--loop-symbol-macs)
@@ -1337,7 +1335,6 @@ For more details, see Info node `(cl)Loop Facility'.
1337 (error "Expected `of'")))) 1335 (error "Expected `of'"))))
1338 (seq (cl--pop2 cl--loop-args)) 1336 (seq (cl--pop2 cl--loop-args))
1339 (temp-seq (make-symbol "--cl-seq--")) 1337 (temp-seq (make-symbol "--cl-seq--"))
1340 (temp-len (make-symbol "--cl-len--"))
1341 (temp-idx 1338 (temp-idx
1342 (if (eq (car cl--loop-args) 'using) 1339 (if (eq (car cl--loop-args) 'using)
1343 (if (and (= (length (cadr cl--loop-args)) 2) 1340 (if (and (= (length (cadr cl--loop-args)) 2)
@@ -1348,19 +1345,16 @@ For more details, see Info node `(cl)Loop Facility'.
1348 (push (list temp-seq seq) loop-for-bindings) 1345 (push (list temp-seq seq) loop-for-bindings)
1349 (push (list temp-idx 0) loop-for-bindings) 1346 (push (list temp-idx 0) loop-for-bindings)
1350 (if ref 1347 (if ref
1351 (progn 1348 (let ((temp-len (make-symbol "--cl-len--")))
1352 (push (list temp-len `(length ,temp-seq)) 1349 (push (list temp-len `(length ,temp-seq))
1353 loop-for-bindings) 1350 loop-for-bindings)
1354 (push (list var `(elt ,temp-seq ,temp-idx)) 1351 (push (list var `(elt ,temp-seq ,temp-idx))
1355 cl--loop-symbol-macs) 1352 cl--loop-symbol-macs)
1356 (cl--push-clause-loop-body `(< ,temp-idx ,temp-len))) 1353 (cl--push-clause-loop-body `(< ,temp-idx ,temp-len)))
1357 ;; Evaluate seq length just if needed, that is, when seq is not a cons.
1358 (push (list temp-len (or (consp seq) `(length ,temp-seq)))
1359 loop-for-bindings)
1360 (push (list var nil) loop-for-bindings) 1354 (push (list var nil) loop-for-bindings)
1361 (cl--push-clause-loop-body `(and ,temp-seq 1355 (cl--push-clause-loop-body `(and ,temp-seq
1362 (or (consp ,temp-seq) 1356 (or (consp ,temp-seq)
1363 (< ,temp-idx ,temp-len)))) 1357 (< ,temp-idx (length ,temp-seq)))))
1364 (push (list var `(if (consp ,temp-seq) 1358 (push (list var `(if (consp ,temp-seq)
1365 (pop ,temp-seq) 1359 (pop ,temp-seq)
1366 (aref ,temp-seq ,temp-idx))) 1360 (aref ,temp-seq ,temp-idx)))
diff --git a/test/lisp/emacs-lisp/cl-macs-tests.el b/test/lisp/emacs-lisp/cl-macs-tests.el
index 9ca84f156a0..77609a42a99 100644
--- a/test/lisp/emacs-lisp/cl-macs-tests.el
+++ b/test/lisp/emacs-lisp/cl-macs-tests.el
@@ -39,6 +39,12 @@
39 collect (list c b a)) 39 collect (list c b a))
40 '((4.0 2 1) (8.3 6 5) (10.4 9 8))))) 40 '((4.0 2 1) (8.3 6 5) (10.4 9 8)))))
41 41
42(ert-deftest cl-macs-loop-and-arrays ()
43 "Bug#40727"
44 (should (equal (cl-loop for y = (- (or x 0)) and x across [1 2]
45 collect (cons x y))
46 '((1 . 0) (2 . -1)))))
47
42(ert-deftest cl-macs-loop-destructure () 48(ert-deftest cl-macs-loop-destructure ()
43 (should (equal (cl-loop for (a b c) in '((1 2 4.0) (5 6 8.3) (8 9 10.4)) 49 (should (equal (cl-loop for (a b c) in '((1 2 4.0) (5 6 8.3) (8 9 10.4))
44 collect (list c b a)) 50 collect (list c b a))