From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.bugs Subject: bug#48477: 28.0.50; Seemingly incorrect codegen with multiple string-matching pcase patterns Date: Tue, 18 May 2021 12:44:30 +0200 Message-ID: References: Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.20\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_695038D0-78A7-4415-AE7F-F736A9109526" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40785"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 48477@debbugs.gnu.org, Stefan Monnier To: Philipp Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue May 18 12:46:17 2021 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 1lixEt-000ALA-U9 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 18 May 2021 12:46:16 +0200 Original-Received: from localhost ([::1]:52528 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lixEs-0003v1-OX for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 18 May 2021 06:46:14 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:52780) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lixDj-00023Z-AM for bug-gnu-emacs@gnu.org; Tue, 18 May 2021 06:45:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:42979) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lixDj-0004Dd-2K for bug-gnu-emacs@gnu.org; Tue, 18 May 2021 06:45:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lixDh-0007Kt-VD for bug-gnu-emacs@gnu.org; Tue, 18 May 2021 06:45:01 -0400 X-Loop: help-debbugs@gnu.org In-Reply-To: Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 18 May 2021 10:45:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 48477 X-GNU-PR-Package: emacs Original-Received: via spool by 48477-submit@debbugs.gnu.org id=B48477.162133467828151 (code B ref 48477); Tue, 18 May 2021 10:45:01 +0000 Original-Received: (at 48477) by debbugs.gnu.org; 18 May 2021 10:44:38 +0000 Original-Received: from localhost ([127.0.0.1]:54525 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lixDK-0007Jx-C2 for submit@debbugs.gnu.org; Tue, 18 May 2021 06:44:38 -0400 Original-Received: from mail171c50.megamailservers.eu ([91.136.10.181]:54572 helo=mail92c50.megamailservers.eu) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lixDH-0007Jm-Ek for 48477@debbugs.gnu.org; Tue, 18 May 2021 06:44:36 -0400 X-Authenticated-User: mattiase@bredband.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=megamailservers.eu; s=maildub; t=1621334673; bh=xeJtkOzsOZ/ak8hsXN4VGNgw01Is4b8rk3fUB5D+j50=; h=From:Subject:Date:Cc:To:From; b=Of16fnIQg6b+IGQPHL/kADY7QRAJW0xH1rJE0zXavzD8V1C/cVWts5opB5XWyIw2Y /mhrv22q7fXIHRpxVpMuTv9nANMKOG+qUKXIC6PQW8/NuYCAmwKtbGzErYh0LhT8mL 0gRHS70Fotp0yxzpJqV4G5m8CZpTV+/hBICZfIvs= Feedback-ID: mattiase@acm.or Original-Received: from stanniol.lan (c-b952e353.032-75-73746f71.bbcust.telenor.se [83.227.82.185]) (authenticated bits=0) by mail92c50.megamailservers.eu (8.14.9/8.13.1) with ESMTP id 14IAiUoM019226; Tue, 18 May 2021 10:44:32 +0000 X-Mailer: Apple Mail (2.3445.104.20) X-CTCH-RefID: str=0001.0A742F1F.60A39A91.003C, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0 X-CTCH-VOD: Unknown X-CTCH-Spam: Unknown X-CTCH-Score: 0.000 X-CTCH-Flags: 0 X-CTCH-ScoreCust: 0.000 X-CSC: 0 X-CHA: v=2.3 cv=UJ+j4xXy c=1 sm=1 tr=0 a=von4qPfY+hyqc0zmWf0tYQ==:117 a=von4qPfY+hyqc0zmWf0tYQ==:17 a=M51BFTxLslgA:10 a=BU4cgGofng8_JXU45KkA:9 a=CjuIK1q_8ugA:10 a=CjFneMiAhERw_nCuohkA:9 a=B2y7HmGcmWMA:10 X-Origin-Country: SE 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" Xref: news.gmane.io gmane.emacs.bugs:206766 Archived-At: --Apple-Mail=_695038D0-78A7-4415-AE7F-F736A9109526 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Serves me right for trying to be clever! Very sorry about that. Matches would always succeed because the outcome was erroneously = transformed into a match against a plain pcase variable which never = fails. For example, the pattern (rx (let x "a")) would expand to =20 (and (pred stringp) (app (lambda (s) (and (string-match (rx (group-n 1 "a")) s) (match-string 1 s))) x)) which cannot fail (as long as the input is a string). Patterns with two = or more named submatches are not affected because of the structural = match used, and zero submatches were treated specially anyway. Please try the attached patch. It encodes non-matches as the number 0 = (any non-nil non-string value would have done; 0 is cheap to create and = test). The above pattern now expands to (and (pred stringp) (app (lambda (s) (if (string-match (rx (group-n 1 "a")) s) (match-string 1 s) 0)) (and x (pred (not numberp))))) --Apple-Mail=_695038D0-78A7-4415-AE7F-F736A9109526 Content-Disposition: attachment; filename=0001-Fix-pcase-rx-patterns-with-a-single-named-submatch-b.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="0001-Fix-pcase-rx-patterns-with-a-single-named-submatch-b.patch" Content-Transfer-Encoding: quoted-printable =46rom=20be9db2b94d31a0afe3f93302558b3a78605244c7=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20=3D?UTF-8?q?Mattias=3D20Engdeg=3DC3=3DA5rd?=3D=20= =0ADate:=20Tue,=2018=20May=202021=2012:03:11=20+0200=0A= Subject:=20[PATCH]=20Fix=20pcase=20'rx'=20patterns=20with=20a=20single=20= named=20submatch=0A=20(bug#48477)=0A=0Apcase=20'rx'=20patterns=20with=20= a=20single=20named=20submatch,=20like=0A=0A=20=20(rx=20(let=20x=20"a"))=0A= =0Awould=20always=20succeed=20because=20of=20an=20over-optimistic=20= transformation.=0APatterns=20with=200=20or=20more=20than=201=20named=20= submatches=20were=20not=20affected.=0A=0AReported=20by=20Philipp=20= Stephani.=0A=0A*=20lisp/emacs-lisp/rx.el=20(rx--pcase-macroexpander):=0A= Special=20case=20for=20a=20single=20named=20submatch.=0A*=20= test/lisp/emacs-lisp/rx-tests.el=20(rx-pcase):=20Add=20tests.=0A---=0A=20= lisp/emacs-lisp/rx.el=20=20=20=20=20=20=20=20=20=20=20=20|=2021=20= ++++++++++++++++-----=0A=20test/lisp/emacs-lisp/rx-tests.el=20|=2014=20= ++++++++++++++=0A=202=20files=20changed,=2030=20insertions(+),=205=20= deletions(-)=0A=0Adiff=20--git=20a/lisp/emacs-lisp/rx.el=20= b/lisp/emacs-lisp/rx.el=0Aindex=201e3eb9c12b..43bd84d999=20100644=0A---=20= a/lisp/emacs-lisp/rx.el=0A+++=20b/lisp/emacs-lisp/rx.el=0A@@=20-1445,12=20= +1445,23=20@@=20rx=0A=20=20=20=20=20=20=20=20=20=20(regexp=20= (rx--to-expr=20(rx--pcase-transform=20(cons=20'seq=20regexps))))=0A=20=20= =20=20=20=20=20=20=20=20(nvars=20(length=20rx--pcase-vars)))=0A=20=20=20=20= =20`(and=20(pred=20stringp)=0A-=20=20=20=20=20=20=20=20=20=20,(if=20= (zerop=20nvars)=0A-=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20;;=20No=20= variables=20bound:=20a=20single=20predicate=20suffices.=0A-=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20`(pred=20(string-match=20,regexp))=0A+=20=20= =20=20=20=20=20=20=20=20,(pcase=20nvars=0A+=20=20=20=20=20=20=20=20=20=20= =20=20(0=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20;;=20No=20variables=20= bound:=20a=20single=20predicate=20suffices.=0A+=20=20=20=20=20=20=20=20=20= =20=20=20=20`(pred=20(string-match=20,regexp)))=0A+=20=20=20=20=20=20=20=20= =20=20=20=20(1=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20;;=20Create=20a=20= match=20value=20that=20on=20a=20successful=20regexp=20match=0A+=20=20=20=20= =20=20=20=20=20=20=20=20=20;;=20is=20the=20submatch=20value,=200=20on=20= failure.=20=20We=20can't=20use=20nil=0A+=20=20=20=20=20=20=20=20=20=20=20= =20=20;;=20for=20failure=20because=20it=20is=20a=20valid=20submatch=20= value.=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20`(app=20(lambda=20(s)=0A= +=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(if=20= (string-match=20,regexp=20s)=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20(match-string=201=20s)=0A+=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=200))=0A+=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(and=20,(car=20= rx--pcase-vars)=20(pred=20(not=20numberp)))))=0A+=20=20=20=20=20=20=20=20= =20=20=20=20(_=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20;;=20Pack=20= the=20submatches=20into=20a=20dotted=20list=20which=20is=20then=0A=20=20=20= =20=20=20=20=20=20=20=20=20=20=20;;=20immediately=20destructured=20into=20= individual=20variables=20again.=0A-=20=20=20=20=20=20=20=20=20=20=20=20=20= ;;=20This=20is=20of=20course=20slightly=20inefficient=20when=20NVARS=20>=20= 1.=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20;;=20This=20is=20of=20= course=20slightly=20inefficient.=0A=20=20=20=20=20=20=20=20=20=20=20=20=20= =20;;=20A=20dotted=20list=20is=20used=20to=20reduce=20the=20number=20of=20= conses=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20;;=20to=20create=20= and=20take=20apart.=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20`(app=20= (lambda=20(s)=0A@@=20-1463,7=20+1474,7=20@@=20rx=0A=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= (rx--reduce-right=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20#'cons=0A=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(mapcar=20(lambda=20= (name)=20(list=20'\,=20name))=0A-=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(reverse=20= rx--pcase-vars)))))))))=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(reverse=20= rx--pcase-vars))))))))))=0A=20=0A=20;;=20Obsolete=20internal=20symbol,=20= used=20in=20old=20versions=20of=20the=20`flycheck'=20package.=0A=20= (define-obsolete-function-alias=20'rx-submatch-n=20'rx-to-string=20= "27.1")=0Adiff=20--git=20a/test/lisp/emacs-lisp/rx-tests.el=20= b/test/lisp/emacs-lisp/rx-tests.el=0Aindex=202dd1bca22d..4828df0de9=20= 100644=0A---=20a/test/lisp/emacs-lisp/rx-tests.el=0A+++=20= b/test/lisp/emacs-lisp/rx-tests.el=0A@@=20-166,6=20+166,20=20@@=20= rx-pcase=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20(backref=201))=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20(list=20u=20v)))=0A=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20'("1"=20"3")))=0A+=20=20(should=20(equal=20(pcase=20= "bz"=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20((rx=20= "a"=20(let=20x=20nonl))=20(list=201=20x))=0A+=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20(_=20'no))=0A+=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20'no))=0A+=20=20(should=20(equal=20(pcase=20"az"=0A+=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20((rx=20"a"=20(let=20x=20= nonl))=20(list=201=20x))=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20((rx=20"b"=20(let=20x=20nonl))=20(list=202=20x))=0A+=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(_=20'no))=0A+=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20'(1=20"z")))=0A+=20=20(should=20= (equal=20(pcase=20"bz"=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20((rx=20"a"=20(let=20x=20nonl))=20(list=201=20x))=0A+=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20((rx=20"b"=20(let=20x=20= nonl))=20(list=202=20x))=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20(_=20'no))=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20'(2=20"z")))=0A=20=20=20(let=20((k=20"blue"))=0A=20=20=20=20=20= (should=20(equal=20(pcase=20""=0A=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20((rx=20"<"=20(literal=20k)=20">")=20'ok))=0A= --=20=0A2.21.1=20(Apple=20Git-122.3)=0A=0A= --Apple-Mail=_695038D0-78A7-4415-AE7F-F736A9109526--