Fix pcase 'rx' patterns with a single named submatch (bug#48477)

pcase 'rx' patterns with a single named submatch, like

  (rx (let x "a"))

would always succeed because of an over-optimistic transformation.
Patterns with 0 or more than 1 named submatches were not affected.

Reported by Philipp Stephani.

* lisp/emacs-lisp/rx.el (rx--pcase-macroexpander):
Special case for a single named submatch.
* test/lisp/emacs-lisp/rx-tests.el (rx-pcase): Add tests.
This commit is contained in:
Mattias Engdegård 2021-05-18 12:03:11 +02:00
parent ed8c3303f9
commit be9db2b94d
2 changed files with 30 additions and 5 deletions

View file

@ -166,6 +166,20 @@
(backref 1))
(list u v)))
'("1" "3")))
(should (equal (pcase "bz"
((rx "a" (let x nonl)) (list 1 x))
(_ 'no))
'no))
(should (equal (pcase "az"
((rx "a" (let x nonl)) (list 1 x))
((rx "b" (let x nonl)) (list 2 x))
(_ 'no))
'(1 "z")))
(should (equal (pcase "bz"
((rx "a" (let x nonl)) (list 1 x))
((rx "b" (let x nonl)) (list 2 x))
(_ 'no))
'(2 "z")))
(let ((k "blue"))
(should (equal (pcase "<blue>"
((rx "<" (literal k) ">") 'ok))