Subject: bug#66187: read-file-name unexpected behavior when MUSTMATCH is a function

From: Stefan Monnier <[email-masked]>
Date: Fri, 06 Oct 2023 12:43:51 -0400

>> Code which wants the 29.1 behavior can easily get it by modifying their
>> `require-match` function accordingly (and such code then works
>> correctly both with Emacs-29.1 and with the new behavior).
> Do we have any users of this feature in the tree?

We do, and it turns out to be a good argument in favor of the change.

The use of a function for `completing-read`s REQUIRE-MATCH and
`read-file-name`s MUSTMATCH was added in

    commit 49e06183f5972817d93dad6acf5351c204e61cc5
    Author: Lars Ingebrigtsen <[email-masked]>
    Date:   Fri Jun 10 10:16:57 2022 +0200
    
        Allow REQUIRE-MATCH to be a function
        
        * doc/lispref/minibuf.texi (Minibuffer Completion): Document it.
        
        * lisp/minibuffer.el (completion--complete-and-exit): Allow
        REQUIRE-MATCH to be a function.
        (read-file-name): Mention it.
        
        * src/minibuf.c (Fcompleting_read): Mention it.

and the surrounding commits point to its motivation in:

    commit 7ee736a884766f2017a934d936bfbfa4c70b5099
    Author: Lars Ingebrigtsen <[email-masked]>
    Date:   Fri Jun 10 10:19:15 2022 +0200
    
        Allow specifying a wildcard argument to list-directory again
        
        * lisp/files.el (list-directory): Allow specifying a wildcard
        argument interactively again (bug#55877).

But the code in that patch uses MUSTMATCH to *restrict* the possible inputs,
whereas Joseph's point is that the code currently does not allow using
MUSTMATCH to restrict the possible inputs.

And indeed we can currently do `M-x list-directory RET /etc/passwd RET`
and Emacs happily runs `list-directory` on that non-directory, contrary
to the intention of the code (IIUC):

     [...]
     (list (read-file-name
            (if pfx "List directory (verbose): "
	      "List directory (brief): ")
	    nil default-directory
            (lambda (file)
              (or (file-directory-p file)
                  (insert-directory-wildcard-in-dir-p
                   (file-name-as-directory (expand-file-name file))))))
     [...]

-- Stefan