unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#37400: Patch for bug in cl-reduce: function called with no arguments when list is empty
@ 2019-09-13  8:50 Adrià Garriga
  2019-09-13 15:18 ` Noam Postavsky
  0 siblings, 1 reply; 3+ messages in thread
From: Adrià Garriga @ 2019-09-13  8:50 UTC (permalink / raw)
  To: 37400

[-- Attachment #1: Type: text/plain, Size: 381 bytes --]

Hello Emacs developers,

I found a bug in cl-reduce, which should be reproducible by running the test in the patch. I'm running 26.2 but the bug is also present in master. Please let me know if there are any problems with the patch (including that it is useless :) or if I need to do something else.

Thank you for your hard work maintaining Emacs!

Adrià Garriga-Alonso


[-- Attachment #2: 0001-cl-reduce-avoids-calling-the-function-when-list-is-n.patch --]
[-- Type: application/octet-stream, Size: 2978 bytes --]

From 3c5df36904c8a5e9137a90309795b3eb9f90dd73 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A0=20Garriga-Alonso?= <adria.garriga@gmail.com>
Date: Wed, 4 Sep 2019 11:36:48 +0100
Subject: [PATCH] cl-reduce avoids calling the function when list is nil

---
 lisp/emacs-lisp/cl-seq.el            | 31 ++++++++++++++--------------
 test/lisp/emacs-lisp/cl-seq-tests.el |  6 ++++++
 2 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/lisp/emacs-lisp/cl-seq.el b/lisp/emacs-lisp/cl-seq.el
index a15c994bc1..8da6ec5a2a 100644
--- a/lisp/emacs-lisp/cl-seq.el
+++ b/lisp/emacs-lisp/cl-seq.el
@@ -134,21 +134,22 @@ If SEQ is empty, return :INITIAL-VALUE and FUNCTION is not
 called.
 
 \n(fn FUNCTION SEQ [KEYWORD VALUE]...)"
-  (cl--parsing-keywords (:from-end (:start 0) :end :initial-value :key) ()
-    (or (listp cl-seq) (setq cl-seq (append cl-seq nil)))
-    (setq cl-seq (cl-subseq cl-seq cl-start cl-end))
-    (if cl-from-end (setq cl-seq (nreverse cl-seq)))
-    (let ((cl-accum (cond ((memq :initial-value cl-keys) cl-initial-value)
-			  (cl-seq (cl--check-key (pop cl-seq)))
-			  (t (funcall cl-func)))))
-      (if cl-from-end
-	  (while cl-seq
-	    (setq cl-accum (funcall cl-func (cl--check-key (pop cl-seq))
-				    cl-accum)))
-	(while cl-seq
-	  (setq cl-accum (funcall cl-func cl-accum
-				  (cl--check-key (pop cl-seq))))))
-      cl-accum)))
+   (when cl-seq
+    (cl--parsing-keywords (:from-end (:start 0) :end :initial-value :key) ()
+      (or (listp cl-seq) (setq cl-seq (append cl-seq nil)))
+      (setq cl-seq (cl-subseq cl-seq cl-start cl-end))
+      (if cl-from-end (setq cl-seq (nreverse cl-seq)))
+      (let ((cl-accum (cond ((memq :initial-value cl-keys) cl-initial-value)
+                            (cl-seq (cl--check-key (pop cl-seq)))
+                            (t (funcall cl-func)))))
+        (if cl-from-end
+            (while cl-seq
+              (setq cl-accum (funcall cl-func (cl--check-key (pop cl-seq))
+                                      cl-accum)))
+          (while cl-seq
+            (setq cl-accum (funcall cl-func cl-accum
+                                    (cl--check-key (pop cl-seq))))))
+        cl-accum))))
 
 ;;;###autoload
 (defun cl-fill (cl-seq cl-item &rest cl-keys)
diff --git a/test/lisp/emacs-lisp/cl-seq-tests.el b/test/lisp/emacs-lisp/cl-seq-tests.el
index 6515eee9f2..56c1826483 100644
--- a/test/lisp/emacs-lisp/cl-seq-tests.el
+++ b/test/lisp/emacs-lisp/cl-seq-tests.el
@@ -311,5 +311,11 @@ Body are forms defining the test."
       (should (eq (cl-assoc x a) (car a)))
       (should (eq (cl-rassoc x a) (cadr a))))))
 
+(ert-deftest cl-seq-reduce ()
+  (let ((bad-add (lambda (x y) (+ x y))))
+    (should-not (cl-reduce bad-add nil))
+    (should (equal 1 (cl-reduce bad-add '(1))))
+    (should (equal 3 (cl-reduce bad-add '(1 2))))))
+
 (provide 'cl-seq-tests)
 ;;; cl-seq-tests.el ends here
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* bug#37400: Patch for bug in cl-reduce: function called with no arguments when list is empty
  2019-09-13  8:50 bug#37400: Patch for bug in cl-reduce: function called with no arguments when list is empty Adrià Garriga
@ 2019-09-13 15:18 ` Noam Postavsky
  2019-10-07  4:33   ` Lars Ingebrigtsen
  0 siblings, 1 reply; 3+ messages in thread
From: Noam Postavsky @ 2019-09-13 15:18 UTC (permalink / raw)
  To: Adrià Garriga; +Cc: 37400

Adrià Garriga <adria.garriga@gmail.com> writes:

> +(ert-deftest cl-seq-reduce ()
> +  (let ((bad-add (lambda (x y) (+ x y))))
> +    (should-not (cl-reduce bad-add nil))

Actually, I think it's a docstring bug.  The Common Lisp Hyperspec says:

    If the subsequence is empty and no initial-value is given, then the
    function is called with zero arguments, and reduce returns whatever
    function does. This is the only case where the function is called
    with other than two arguments.

It looks like Emacs' current implementation is correct, but the
docstring doesn't explain it accurately.

https://www.cs.cmu.edu/Groups/AI/html/hyperspec/HyperSpec/Body/fun_reduce.html





^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#37400: Patch for bug in cl-reduce: function called with no arguments when list is empty
  2019-09-13 15:18 ` Noam Postavsky
@ 2019-10-07  4:33   ` Lars Ingebrigtsen
  0 siblings, 0 replies; 3+ messages in thread
From: Lars Ingebrigtsen @ 2019-10-07  4:33 UTC (permalink / raw)
  To: Noam Postavsky; +Cc: 37400, Adrià Garriga

Noam Postavsky <npostavs@gmail.com> writes:

> Actually, I think it's a docstring bug.  The Common Lisp Hyperspec says:
>
>     If the subsequence is empty and no initial-value is given, then the
>     function is called with zero arguments, and reduce returns whatever
>     function does. This is the only case where the function is called
>     with other than two arguments.

I've now added a variation of this to the doc string, and I'm closing
this bug report.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-10-07  4:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-13  8:50 bug#37400: Patch for bug in cl-reduce: function called with no arguments when list is empty Adrià Garriga
2019-09-13 15:18 ` Noam Postavsky
2019-10-07  4:33   ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).