Subject: bug#43100: 28.0.50; pcase not binding variables conditionally

From: Pip Cet <[email-masked]>
Date: Sat, 29 Aug 2020 14:27:51 +0000

On Sat, Aug 29, 2020 at 12:01 PM Philipp Stephani
<[email-masked]> wrote:
> Am Sa., 29. Aug. 2020 um 11:42 Uhr schrieb Pip Cet <[email-masked]>:
> > I'm having trouble with pcase's behavior.
> >
> > (pcase "a"
> >   ((or (pred symbolp) name)
> >    (let ((foo 'bar)) name)))
> >
> > throws an error. It shouldn't.
>
> Isn't this case documented in the manual? The last section of
> https://www.gnu.org/software/emacs/manual/html_node/elisp/pcase-Macro.html
> states:
> "It makes no sense for each sub-pattern [in an `or' sequence] to
> let-bind a different set of symbols because the body forms have no way
> to distinguish which sub-pattern matched and choose among the
> different sets."

Thanks for pointing this out.

I disagree with what the documentation says there: it does make
perfect sense, to me, to conditionally shadow a (lexical) let binding
with a pcase-let one, and body forms will have no trouble in practice
distinguishing between the outer and the inner let-binding. The code
obviously attempts to handle this case, too, it just doesn't get it
perfectly right.

I agree and accept that pcase is very limited, and it probably always
will be, but I think those limitations shouldn't be entirely
arbitrary, and hitting them shouldn't cause silently malfunctioning
code but error messages.

Things like the behavior of

(pcase-let ((`(,a ,@b) (list 3 4)))
  a)

just seem puzzling to me. There are good reasons not to implement
sublist matching (though I don't think those reasons are sufficient
not to have a simple implementation anyway), so an error message would
be acceptable, but the current implementation treats \,@ as an
ordinary symbol, which doesn't help anyone.

Sorry for complaining. Here's a patch.
From c52708d92dec4a6d7810d634dd4ff23a2cbca1ec Mon Sep 17 00:00:00 2001
From: Pip Cet <[email-masked]>
Date: Sat, 29 Aug 2020 14:21:29 +0000
Subject: [PATCH] Complain about ,@ in backquote pcase patterns

* lisp/emacs-lisp/pcase.el (\`): Complain upon encountering \,@ in
backquote pcase patterns.
---
 lisp/emacs-lisp/pcase.el | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
index a8ce23284c..642ff0f73a 100644
--- a/lisp/emacs-lisp/pcase.el
+++ b/lisp/emacs-lisp/pcase.el
@@ -971,6 +971,7 @@ \`
   (declare (debug (pcase-QPAT)))
   (cond
    ((eq (car-safe qpat) '\,) (cadr qpat))
+   ((eq (car-safe qpat) '\,@) (error "sublist matching not supported"))
    ((vectorp qpat)
     `(and (pred vectorp)
           (app length ,(length qpat))
-- 
2.28.0