Subject: replace-regexp lisp replacement bug

From: Nicholas Drozd <[email-masked]>
Date: Mon, 18 Jan 2021 18:15:09 -0600

Here is a string representing a Turing machine program:

  1RB 1LC 1RC 1RB 1RD 0LE 1LA 1LD 1RH 0LA

Say I have a file full of lines like this, and I want to swap all the
Ls for Rs and vice versa. `replace-regexp` is a good way to do it. For
the regexp, I use `\(\(L\)\|\(R\)\)`, a matching group with two nested
matching alternatives; for the replacement, I use a Lisp expression:
`\,(if \2 "R" "L")`. This expression says: if the second match is
found (the `L`), replace it with "R", else replace it with "L".

In Emacs 27 (b58fd1eab9), this gives the expected result:

  1LB 1RC 1LC 1LB 1LD 0RE 1RA 1RD 1LH 0RA

But in Emacs 28 (20add1cd22), it gives an error:

  replace-highlight: Wrong type argument: integer-or-marker-p, nil

A simpler replacement does work: `\(L\)\|R` and `\,(if \1 "R" "L")`.
But the longer one should work too.

I tried to make a Lisp test to reproduce this, but the `\,`
replacement feature is interactive-only, and I don't know how to test
it non-interactively.