Subject: bug#55205: 28.1.50; completion--replace illegally mutates completion candidates

From: Stefan Monnier <[email-masked]>
Date: Sun, 01 May 2022 13:07:31 -0400

Lars Ingebrigtsen [2022-05-01 13:53:59] wrote:
> Daniel Mendler <[email-masked]> writes:
>> The function completion--replace mutates the replacement string, it
>> strips the text properties.
> I don't think that's, strictly speaking, illegal.  :-)

[ Notice the smiley.  ]

> Anyway, I agree that it's unfortunate that completion destructively
> modifies the strings it's handed, and this has been discussed
> extensively over the years (and there's probably several bug reports
> open about that, although I can't find them now).
>
> I don't remember why we're doing that, but I seem to vaguely recall that
> there's a reason...  Anybody?

I'm pretty sure there's a reason, and I'm pretty sure this reason is
"sloppiness".  I blame the author of commit 1d00653d9e (and the author
of commit 14486c44 might be considered as an accessory to the crime).

> We should (at least) document this in all the relevant functions.

I think it's much better to fix the bug, so I just pushed the patch
below to `master`.

I think it's safe enough for `emacs-28`, but I can't claim it's
"obviously safe", the way I could about that same `copy-sequence` in
`cl-generic.el`.

        Stefan

commit 788694d026b401715330576633a98542623978ff (HEAD -> main, origin/master, origin/HEAD)
Author: Stefan Monnier <[email-masked]>
Date:   Sun May 1 13:04:44 2022 -0400

    * lisp/minibuffer.el (completion--replace): Fix bug#55205

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index ef71b4e6be6..fb473cf71b0 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1140,6 +1140,7 @@ completion--replace
   ;; The properties on `newtext' include things like the
   ;; `completions-first-difference' face, which we don't want to
   ;; include upon insertion.
+  (setq newtext (copy-sequence newtext)) ;Don't modify the arg by side-effect.
   (if minibuffer-allow-text-properties
       ;; If we're preserving properties, then just remove the faces
       ;; and other properties added by the completion machinery.