Subject: bug#60081: 29.0.60; compiler-macros in keymap.el accept wrong number of arguments

From: Zehao Hu <[email-masked]>
Date: Thu, 15 Dec 2022 16:54:26 +0800

Hi all,

I'm sure that this is the same bug as bug #50043, #58148, #58396

In bug #58356,
Stefan Monnier <[email-masked]> writes:
>Lars Ingebrigtsen [2022-10-11 20:27:42] wrote:
>> Stefan Monnier <[email-masked]> writes:
>>> Because that's the sort of error you can get if you try to macro-expand
>>> code that's syntactically invalid.  E.g. try to compile a file with
>>>
>>>     (add-to-list x)
>>>
>>> and you should see a similar error.  For `elisp--local-variables`,
>>> syntactically invalid code is the normal case (more or less) both
>>> because it's used when we're in the middle of writing the code, and also
>>> because of how it works (it takes the code from beginning-of-defun up
>>> to point and throws away whatever follows, just adding enough closing
>>> parens that the resutling string is `read`able).
>>
>> I see.
>>
>> Shouldn't this function then be suppressing all errors while it's doing
>> this exploratory macro-expansion stuff?
>
>Yes (plus silence the messages plus avoid compiler macros).

Here is a simple workaround:

(require 'loadhist)

(defun cmacro-ignore-elisp-witness (oldfn form &rest args)
  "Add this advicing function around compiler macros to ignore
`elisp--witness--lisp'"
  (unless (eq 'elisp--witness--lisp (car args))
    (apply oldfn form args)))

(defun cmacro-add-advice (file-or-entry)
  "Add `cmacro-ignore-elisp-witness' to compiler macros in FILE or in ENTRY
from `load-history'"
  (let ((entry (if (stringp file-or-entry)
                   (file-loadhist-lookup file-or-entry)
                 file-or-entry)))
    (dolist (cell (cdr entry))
      (when-let*
          (((consp cell))
           ((eq 'defun (car cell)))
           (symbol
            (cdr cell))
           ((symbolp symbol))
           (cmacro
            (get symbol 'compiler-macro))
           ((not (advice-member-p #'cmacro-ignore-elisp-witness symbol))))
        (advice-add cmacro :around #'cmacro-ignore-elisp-witness)))))

;; Advicing existing compiler macros
(dolist (entry load-history)
  (cmacro-add-advice entry))

;; And the ones in the future
(add-hook 'after-load-functions #'cmacro-add-advice)
(dolist (entry load-history)
  (cmacro-add-advice entry))

(add-hook 'after-load-functions #'cmacro-add-advice)

Or maybe edit `byte-run--set-compiler-macro', to change the `*--anon-cmacro'
and to advice the `compiler-function'.