Subject: Re: bug#46722: test-map-into fails

From: Stefan Monnier <[email-masked]>
Date: Wed, 05 Jan 2022 12:51:09 -0500

I think I tracked it down.

The patch below should fix it.  Boy do I hate side effects!
Could others confirm that it fixes the problem for them as well?

Eli, do you think this is appropriate for `emacs-28`?
OT1H this bug probably has been with us since Emacs-25, but OTOH the
patch should be very safe.

        Stefan

PS: We don't need the `copy-sequence` when we do the actual `gethash`,
we only need it if that `gethash` returns nil and we end up putting
a new entry in the hash-table, but avoiding those extra `copy-sequence`
requires manually macroexpanding `with-memoization`, so I favored
keeping the patch as short as possible.

diff --git a/lisp/emacs-lisp/cl-generic.el b/lisp/emacs-lisp/cl-generic.el
index 983694204df..5090e060372 100644
--- a/lisp/emacs-lisp/cl-generic.el
+++ b/lisp/emacs-lisp/cl-generic.el
@@ -604,7 +604,9 @@ cl--generic-dispatchers
 
 (defun cl--generic-get-dispatcher (dispatch)
   (with-memoization
-      (gethash dispatch cl--generic-dispatchers)
+      ;; We need `copy-sequence` here because this `dispatch' object might be
+      ;; modified by side-effect in `cl-generic-define-method' (bug#46722).
+      (gethash (copy-sequence dispatch) cl--generic-dispatchers)
     ;; (message "cl--generic-get-dispatcher (%S)" dispatch)
     (let* ((dispatch-arg (car dispatch))
            (generalizers (cdr dispatch))