Subject: Re: bug#68509: 30.0.50; pcase-dolist matches backquote pattern incorrectly

From: Ihor Radchenko <[email-masked]>
Date: Mon, 19 Feb 2024 18:14:24 +0000

Stefan Monnier <[email-masked]> writes:

>> * lisp/emacs-lisp/pcase.el (pcase-dolist): Use `pcase' rather than
>> `pcase-let*' to match the list elements.  Update the docstring,
>
> That changes its behavior.  It *will* break code.
> This is basically not the same macro any more.  Instead of being a macro
> that iterates over all the elements of the list, it becomes a macro
> which iterates only over the matching elements.

Currently, the docstring states:

    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.

It is undefined what happens when LIST objects do not match PATTERN.

So, any code relying upon iterating over non-matching elements is
relying upon undefined, undocumented behaviour.

Moreover, current implementation binds /partially matching/ list
elements, which is even more fragile. From `pcase-let' docstring:

    a mismatch may signal an error or may go
    undetected, binding variables to arbitrary values, such as nil.

What I suggest is to change what happens when PATTERN does not much more
explicitly.

In my patch, I proposed to skip such elements.
An alternative could be throwing an error.

-- 
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>