Subject: [PATCH] Fix compilation of defalias with a constant macro cons pair

From: zach shaftel <[email-masked]>
Date: Sat, 14 Jun 2025 21:40:08 -0400

Tags: patch

byte-compile-file-form-defalias destructures (cons 'macro 'fn) and
'(macro . fn) the same way, so in the latter case fn would be unquoted,
and then eval would later signal (void-variable fn). this patch just
re-quotes fn, and adds a test for this scenario.

In GNU Emacs 31.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.49, cairo version 1.18.4) of 2025-06-11 built on bigbox
Repository revision: f90cee6f761b0c2bffcfa64556284884c0f7348f
Repository branch: feature/igc
System Description: Arch Linux

Configured using:
 'configure --with-modules --with-native-compilation --with-tree-sitter
 --without-gsettings --without-gconf --without-gpm --with-pgtk
 --without-compress-install --with-mps 'CFLAGS=-mtune=native
 -march=native -O3 -fuse-ld=mold''

>From f0b9f227c5711c1cbf7a38c20fd05b184536c12a Mon Sep 17 00:00:00 2001
From: Zach Shaftel <[email-masked]>
Date: Sat, 14 Jun 2025 20:57:21 -0400
Subject: [PATCH] Fix compilation of defalias with a constant macro cons pair

* lisp/emacs-lisp/bytecomp.el (byte-compile-file-form-defalias): Quote
the function name in '(macro . function-name), since we eval it later.
* test/lisp/emacs-lisp/bytecomp-tests.el
(test-eager-load-macro-expand-defalias): New test.
---
 lisp/emacs-lisp/bytecomp.el            |  3 ++-
 test/lisp/emacs-lisp/bytecomp-tests.el | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el
index 0ec8db214bc..973792c4daa 100644
--- a/lisp/emacs-lisp/bytecomp.el
+++ b/lisp/emacs-lisp/bytecomp.el
@@ -5159,7 +5159,8 @@ byte-compile-file-form-defalias
        (pcase-let*
            ;; `macro' is non-nil if it defines a macro.
            ;; `fun' is the function part of `arg' (defaults to `arg').
-           (((or (and (or `(cons 'macro ,fun) `'(macro . ,fun)) (let macro t))
+           (((or (and (or `(cons 'macro ,fun) `'(macro . ,(app (list 'quote) fun)))
+                      (let macro t))
                  (and (let fun arg) (let macro nil)))
              arg)
             ;; `lam' is the lambda expression in `fun' (or nil if not
diff --git a/test/lisp/emacs-lisp/bytecomp-tests.el b/test/lisp/emacs-lisp/bytecomp-tests.el
index 8b0c1dad4c0..b14f10d6eb7 100644
--- a/test/lisp/emacs-lisp/bytecomp-tests.el
+++ b/test/lisp/emacs-lisp/bytecomp-tests.el
@@ -1322,6 +1322,24 @@ test-eager-load-macro-expand-lexical-override
       (defun def () (m))))
   (should (equal (funcall 'def) 4)))
 
+(ert-deftest test-eager-load-macro-expand-defalias ()
+  (ert-with-temp-file elfile
+    :suffix ".el"
+    (write-region
+     (concat ";;; -*- lexical-binding: t -*-\n"
+             (mapconcat #'prin1-to-string
+                        '((defalias 'nothing '(macro . ignore))
+                          (defalias 'something (cons 'macro #'identity))
+                          (defalias 'five (cons 'macro (lambda (&rest _) 5)))
+                          (eval-when-compile
+                            (defun def () (or (nothing t) (something (five nil))))))
+                        "\n"))
+     nil elfile)
+    (let* ((byte-compile-debug t)
+           (byte-compile-dest-file-function #'ignore))
+      (byte-compile-file elfile)
+      (should (equal (funcall 'def) 5)))))
+
 (defmacro bytecomp-tests--with-temp-file (file-name-var &rest body)
   (declare (indent 1))
   (cl-check-type file-name-var symbol)
-- 
2.49.0