* lisp/emacs-lisp/pcase.el (pcase--macroexpand): Normalise atom etc.

Transform (pred P) for P in {atom, nlistp, identity, not} into
predicates that pcase already understands in type terms.
This doesn't affect the behaviour but generates better code.
This commit is contained in:
Mattias Engdegård 2026-01-22 15:32:13 +01:00
parent cd8c85c4fd
commit 3b547e4f5d

View file

@ -523,7 +523,17 @@ how many time this CODEGEN is called."
(cond
((null head)
(if (pcase--self-quoting-p pat) `',pat pat))
((memq head '(pred guard quote)) pat)
((memq head '(guard quote)) pat)
((eq head 'pred)
;; Ad-hoc expansion of some predicates that are the complement of another.
;; Not required for correctness but results in better code.
(let* ((expr (cadr pat))
(compl (assq expr '((atom . consp)
(nlistp . listp)
(identity . null)))))
(cond (compl `(,head (not ,(cdr compl))))
((eq expr 'not) `(,head null)) ; normalise
(t pat))))
((memq head '(or and)) `(,head ,@(mapcar #'pcase--macroexpand (cdr pat))))
((eq head 'app) `(app ,(nth 1 pat) ,(pcase--macroexpand (nth 2 pat))))
(t