From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ihor Radchenko Newsgroups: gmane.emacs.bugs Subject: bug#68509: 30.0.50; pcase-dolist matches backquote pattern incorrectly Date: Mon, 19 Feb 2024 10:05:33 +0000 Message-ID: <871q98hiqa.fsf@localhost> References: <87a5p5l3jm.fsf@localhost> <9bc2bd05-5fdb-9d5a-3d98-c344c7275027@gmail.com> <87sf2xjezi.fsf@localhost> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5864"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 68509@debbugs.gnu.org To: Jim Porter Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Feb 19 11:03:20 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rc0Ua-0001EP-9o for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 19 Feb 2024 11:03:20 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rc0Tz-0006bR-13; Mon, 19 Feb 2024 05:02:43 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rc0Tx-0006ax-9Z for bug-gnu-emacs@gnu.org; Mon, 19 Feb 2024 05:02:41 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rc0Tx-0001fS-17 for bug-gnu-emacs@gnu.org; Mon, 19 Feb 2024 05:02:41 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rc0UH-0002Y4-Vk for bug-gnu-emacs@gnu.org; Mon, 19 Feb 2024 05:03:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Ihor Radchenko Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 19 Feb 2024 10:03:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68509 X-GNU-PR-Package: emacs Original-Received: via spool by 68509-submit@debbugs.gnu.org id=B68509.17083369419749 (code B ref 68509); Mon, 19 Feb 2024 10:03:01 +0000 Original-Received: (at 68509) by debbugs.gnu.org; 19 Feb 2024 10:02:21 +0000 Original-Received: from localhost ([127.0.0.1]:40392 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rc0Td-0002XB-3e for submit@debbugs.gnu.org; Mon, 19 Feb 2024 05:02:21 -0500 Original-Received: from mout01.posteo.de ([185.67.36.65]:45437) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rc0TZ-0002Ww-Lz for 68509@debbugs.gnu.org; Mon, 19 Feb 2024 05:02:19 -0500 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 5DFFF24002B for <68509@debbugs.gnu.org>; Mon, 19 Feb 2024 11:01:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1708336910; bh=6dO1eGIdqETlXJiX0pKaqJk+AGWPlQ1gJdOeKJhtq1M=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type: From; b=h+RQAKsm+hOwqFdn+IEk2d4GtGCHfiGfxq6fav050waGtKkdemSgXnsLikH6iIoQ2 vWKh8NmaIvApjTRYtG65mGzFvmVYo2aaif7n6TxeH39MsUpM3zkY3enyLyumDzT5b/ 8W3/n7wEclu25E+lXxgDbeZARWc3GJ4+sqviNTNNR0Minrg/CXMMwKWK0biH/8KG8Z CSOfZ5JfVYsetHojr2Jkxm0niaBzO01TxBkmvAHGqlCqWP0y5g1wfBnYT2z95Z2Hd4 QRATS3RPV9sK+pI+dXOiPnxKSFujhRgXM7GdHdT4eds2LxUo2qVnP83Ry3RXHRXdhr vndRbPKf1mBoA== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4TddNs2MKVz6tvJ; Mon, 19 Feb 2024 11:01:49 +0100 (CET) In-Reply-To: <87sf2xjezi.fsf@localhost> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:280242 Archived-At: --=-=-= Content-Type: text/plain Ihor Radchenko 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. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-pcase-let-Skip-LIST-element-that-do-not-match-the-PA.patch >From f3e1362f7687c731e0ba4e410f005252309ffc3f Mon Sep 17 00:00:00 2001 Message-ID: From: Ihor Radchenko 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 --=-=-= Content-Type: text/plain -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at . Support Org development at , or support my work at --=-=-=--