unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@posteo.net>
To: Jim Porter <jporterbugs@gmail.com>
Cc: 68509@debbugs.gnu.org
Subject: bug#68509: 30.0.50; pcase-dolist matches backquote pattern incorrectly
Date: Mon, 19 Feb 2024 10:05:33 +0000	[thread overview]
Message-ID: <871q98hiqa.fsf@localhost> (raw)
In-Reply-To: <87sf2xjezi.fsf@localhost>

[-- Attachment #1: Type: text/plain, Size: 434 bytes --]

Ihor Radchenko <yantar92@posteo.net> writes:

> Not sure about pcase-let, but pcase-dolist specifically may be
> simplified not to use pcase-let:
>
> (if (pcase--trivial-upat-p (car spec))
>       `(dolist ,spec ,@body)
>     (let ((tmpvar (gensym "x")))
>       `(dolist (,tmpvar ,@(cdr spec))
>          (pcase ,tmpvar (,(car spec) ,@body)))))

See the attached patch.
If the patch is acceptable, we also need to update the manual.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-pcase-let-Skip-LIST-element-that-do-not-match-the-PA.patch --]
[-- Type: text/x-patch, Size: 3056 bytes --]

From f3e1362f7687c731e0ba4e410f005252309ffc3f Mon Sep 17 00:00:00 2001
Message-ID: <f3e1362f7687c731e0ba4e410f005252309ffc3f.1708337064.git.yantar92@posteo.net>
From: Ihor Radchenko <yantar92@posteo.net>
Date: Mon, 19 Feb 2024 13:02:21 +0300
Subject: [PATCH] pcase-let: Skip LIST element that do not match the PATTERN
 (bug#68509)

* lisp/emacs-lisp/pcase.el (pcase-dolist): Use `pcase' rather than
`pcase-let*' to match the list elements.  Update the docstring,
describing the behavior when list elements to not match the pattern.
The previous undefined behavior is removed.
* test/lisp/emacs-lisp/pcase-tests.el (pcase-tests-pcase-dolist): Add
new test.
---
 lisp/emacs-lisp/pcase.el            | 13 ++++++-------
 test/lisp/emacs-lisp/pcase-tests.el | 12 ++++++++++++
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
index ae9bd87997c..8dc11b20a6f 100644
--- a/lisp/emacs-lisp/pcase.el
+++ b/lisp/emacs-lisp/pcase.el
@@ -329,21 +329,20 @@ pcase-let
 (defmacro pcase-dolist (spec &rest body)
   "Eval BODY once for each set of bindings defined by PATTERN and LIST elements.
 PATTERN should be a `pcase' pattern describing the structure of
-LIST elements, and LIST is a list of objects that match PATTERN,
-i.e. have a structure that is compatible with PATTERN.
+LIST elements, and LIST is a list of objects.
 For each element of LIST, this macro binds the variables in
 PATTERN to the corresponding subfields of the LIST element, and
-then evaluates BODY with these bindings in effect.  The
-destructuring bindings of variables in PATTERN to the subfields
-of the elements of LIST is performed as if by `pcase-let'.
+then evaluates BODY with these bindings in effect.  When an element does
+not match the pattern, such element is skipped.
+The destructuring bindings of variables in PATTERN to the subfields
+of the elements of LIST is performed as if by `pcase'.
 \n(fn (PATTERN LIST) BODY...)"
   (declare (indent 1) (debug ((pcase-PAT form) body)))
   (if (pcase--trivial-upat-p (car spec))
       `(dolist ,spec ,@body)
     (let ((tmpvar (gensym "x")))
       `(dolist (,tmpvar ,@(cdr spec))
-         (pcase-let* ((,(car spec) ,tmpvar))
-           ,@body)))))
+	 (pcase ,tmpvar (,(car spec) ,@body))))))
 
 ;;;###autoload
 (defmacro pcase-setq (pat val &rest args)
diff --git a/test/lisp/emacs-lisp/pcase-tests.el b/test/lisp/emacs-lisp/pcase-tests.el
index d062965952a..241729c108a 100644
--- a/test/lisp/emacs-lisp/pcase-tests.el
+++ b/test/lisp/emacs-lisp/pcase-tests.el
@@ -160,4 +160,16 @@ pcase-tests-setq
   (should-error (pcase-setq a)
                 :type '(wrong-number-of-arguments)))
 
+(ert-deftest pcase-tests-pcase-dolist ()
+  ;; Ignore non-matching elements.
+  (should
+   (equal
+    '(("DONE" . "a"))
+    (let (result)
+      (pcase-dolist (`(,(and (pred stringp) a) .
+		       ,(and (pred stringp) b))
+	             '(("TODO") ("DONE" . "a")))
+        (push (cons a b) result))
+      (nreverse result)))))
+
 ;;; pcase-tests.el ends here.
-- 
2.43.0


[-- Attachment #3: Type: text/plain, Size: 224 bytes --]


-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

  reply	other threads:[~2024-02-19 10:05 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-16 15:07 bug#68509: 30.0.50; pcase-dolist matches backquote pattern incorrectly Ihor Radchenko
2024-01-16 16:52 ` Jim Porter
2024-01-16 18:43   ` Ihor Radchenko
2024-02-19 10:05     ` Ihor Radchenko [this message]
2024-02-19 12:59       ` Eli Zaretskii
2024-02-19 15:53       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-19 18:14         ` Ihor Radchenko
2024-02-20  2:41           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-20 13:40             ` Ihor Radchenko
2024-02-20 14:35               ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-20 15:16                 ` Ihor Radchenko
2024-02-20 17:51                   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-21 11:23                     ` Ihor Radchenko
2024-02-21 14:17                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-23 14:05                         ` Ihor Radchenko
2024-02-23 14:58                           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-24 13:28                             ` Ihor Radchenko
2024-02-24 14:57                               ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-19 15:51   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-19 18:16     ` Ihor Radchenko
2024-02-20  2:46       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871q98hiqa.fsf@localhost \
    --to=yantar92@posteo.net \
    --cc=68509@debbugs.gnu.org \
    --cc=jporterbugs@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).