Subject: bug#33731: closed (Fwd: 26.1; cl-make-random-state copying not working)

From: [email-masked] (GNU bug Tracking System)
Date: Wed, 19 Dec 2018 22:29:02 +0000

Your bug report

#33731: 26.1; cl-make-random-state copying not working

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
33731: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=33731
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Fwd: 26.1; cl-make-random-state copying not working
From: Paul Eggert <[email-masked]>
To: [email-masked]
Date: Wed, 19 Dec 2018 14:27:59 -0800
List-Id: <debbugs-submit.debbugs.gnu.org>

Thanks for reporting this bug; it is a regression introduced when we 
separated records from vectors. I installed the attached patch into the 
emacs-26 branch.

>From 78c883cc1c30ab82732eac02fb07bb747436854a Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Wed, 19 Dec 2018 13:42:21 -0800
Subject: [PATCH] cl-make-random-state was not copying its arg

Problem reported by Xu Chunyang (Bug#33731).
* lisp/emacs-lisp/cl-extra.el (cl-make-random-state):
Use copy-sequence, not copy-tree, so that the record is copied.
* test/lisp/emacs-lisp/cl-extra-tests.el:
(cl-extra-test-cl-make-random-state): New test.
---
 lisp/emacs-lisp/cl-extra.el            | 2 +-
 test/lisp/emacs-lisp/cl-extra-tests.el | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/lisp/emacs-lisp/cl-extra.el b/lisp/emacs-lisp/cl-extra.el
index 36b65f97b0..c38b4957fc 100644
--- a/lisp/emacs-lisp/cl-extra.el
+++ b/lisp/emacs-lisp/cl-extra.el
@@ -484,7 +484,7 @@ cl-make-random-state
 If STATE is t, return a new state object seeded from the time of day."
   (unless state (setq state cl--random-state))
   (if (cl-random-state-p state)
-      (copy-tree state t)
+      (copy-sequence state)
     (cl--make-random-state (if (integerp state) state (cl--random-time)))))
 
 ;; Implementation limits.
diff --git a/test/lisp/emacs-lisp/cl-extra-tests.el b/test/lisp/emacs-lisp/cl-extra-tests.el
index baad8eb8e6..fe59703530 100644
--- a/test/lisp/emacs-lisp/cl-extra-tests.el
+++ b/test/lisp/emacs-lisp/cl-extra-tests.el
@@ -94,4 +94,9 @@
     (should (equal (list lst3 (cdr lst3) (cddr lst3))
                    (cl-maplist fn3 lst lst2 lst3)))))
 
+(ert-deftest cl-extra-test-cl-make-random-state ()
+  (let ((s (cl-make-random-state)))
+    ;; Test for Bug#33731.
+    (should-not (eq s (cl-make-random-state s)))))
+
 ;;; cl-extra-tests.el ends here
-- 
2.19.2

Subject: 26.1; cl-make-random-state copying not working
From: Xu Chunyang <[email-masked]>
To: [email-masked]
Date: Fri, 14 Dec 2018 05:12:35 +0800
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi,

I am reading (info "(cl) Random Numbers") on cl-make-random-state. I
expect the following returns two equal numbers, because s should be a
copy of cl--random-state thus two cl-random should use two equal
state.

(let ((s (cl-make-random-state)))
  (list (cl-random 10000)
        (cl-random 10000 s)))
;; => (6955 916)

If I didn't misunderstand this, I think it is because
cl-make-random-state uses copy-tree, however it doesn't work for
cl-structure at least in Emacs 26.1, it doesn't copy at all.

(eq (cl-make-random-state 123)
    (cl-make-random-state 123))
;; => nil

(let* ((s1 (cl-make-random-state 123))
       (s2 (cl-make-random-state s1)))
  (eq s1 s2))
;; => t