Subject: bug#72328: [PATCH] Nested backquote in pcase

From: Thuna <[email-masked]>
Date: Sat, 03 Aug 2024 02:07:59 +0200

> I think I'd be interested to hear about "real life" cases out there
> where this choice would make a difference (in either direction).

For what its worth, a very imprecise search through all melpa packages
revealed exactly zero pcase patterns which this would effect.

I will write down the process in case someone wants to try and replicate
it themselves:

- Clone all repositories in melpa via `make', `package-build-all', or if
  you are in the horrible situation that I am where every other query
  doesn't resolve do (in the /path/to/melpa/package-build/ directory)

  emacs --batch --eval '(push default-directory load-path)'            \
    -l package-build -l package-recipe                                 \
    --eval '(mapc (lambda (file)
          (message "cloning %s" file)
          (ignore-errors
            (package-build--run-process "git" "clone"
                                        (package-recipe--upstream-url
                                         (package-recipe-lookup file))
                                        (concat "../working/" file))))
        (cl-remove-if
         (lambda (file) (file-exists-p (concat "../working/" file)))
         (package-recipe-packages)))'

- Obtain a list of .el files which contain both "pcase" and "`".

- Obtain the pcase forms in those files via

  (let ((print-level nil) (print-circle nil)
        (original-buffer (current-buffer)))
    (with-temp-file "~/pcase-output.eld"
      (let ((output-buffer (current-buffer)))
        (with-current-buffer original-buffer
          (mapc (lambda (file)
                  (kill-buffer
                   (with-current-buffer (find-file-noselect file)
                     (while (re-search-forward "\\_<pcase" nil t)
                       (unless (or (nth 3 (syntax-ppss))
                                   (nth 4 (syntax-ppss)))
                         (save-excursion
                           (backward-up-list)
                           (condition-case err
                               (prin1 (read (current-buffer)) output-buffer)
                             (error (print `(error ',file ,err)))))))
                     (current-buffer))))
                (split-string (buffer-string) "\n"))))))

- Replace those forms (when recognized) with a quoted list of the
  patterns via

  (let ((print-length nil)
        (print-level nil))
    (while-let ((form (ignore-errors (read (current-buffer)))))
      (when (consp form)
        (cl-case (car form)
          ((pcase pcase-exhaustive)
           (replace-region-contents
            (scan-sexps (point) -1) (point)
            (cl-constantly
             (prin1-to-string `'(,@(mapcar #'car (cddr form)))))))
          ((pcase-let pcase-let*)
           (replace-region-contents
            (scan-sexps (point) -1) (point)
            (cl-constantly
             (prin1-to-string `'(,@(mapcar #'car-safe (cadr form)))))))
          ((pcase-lambda)
           (replace-region-contents
            (scan-sexps (point) -1) (point)
            (cl-constantly
             (prin1-to-string
              `'(,@(remq '&optional (remq '&rest (cadr form))))))))
          ((pcase-dolist)
           (replace-region-contents
            (scan-sexps (point) -1) (point)
            (cl-constantly
             (prin1-to-string `'(,@(caadr form))))))
          ((require pcase-defmacro)
           (delete-region (scan-sexps (point) -1) (point)))))))

- Filter out all patterns which do not contain at least two backquotes
  via M-x keep-lines RET .*`.*`.*

- Manually filter out all patterns which do not qualify

- Observe the empty buffer