Subject: Re: bug#64494: 30.0.50; Recursive load error with native-compile

From: Stefan Monnier <[email-masked]>
Date: Fri, 07 Jul 2023 13:02:55 -0400

> Andrea, Stefan: any ideas for how to allow emacs-lisp-compilation-mode
> in async compilation buffers without triggering this recursive-load
> nastiness?

I'm wondering if instead of having the C code do:

    /* This is so deferred compilation is able to compile comp
       dependencies breaking circularity.  */
    if (comp__compilable)
      {
        /* Startup is done, comp is usable.  */
        CALL0I (startup--require-comp-safely);
        CALLN (Ffuncall, intern_c_string ("native--compile-async"),
               src, Qnil, Qlate);
      }
    else
      Vcomp__delayed_sources = Fcons (src, Vcomp__delayed_sources);

we shouldn't just do something like

    pending_funcalls
        = Fcons (list (Qnative__compile_async, src, Qnil, Qlate),
                 pending_funcalls);

I.e. never call native--compile-async synchronously.
The patch below seems to work so far, tho I haven't tried bootstrapping yet.

        Stefan

diff --git a/lisp/emacs-lisp/comp.el b/lisp/emacs-lisp/comp.el
index 77584b692a4..4892733d456 100644
--- a/lisp/emacs-lisp/comp.el
+++ b/lisp/emacs-lisp/comp.el
@@ -4230,6 +4230,7 @@ native-compile-async-skip-p
                       (string-match-p re file))
                     native-comp-jit-compilation-deny-list))))
 
+;;;###autoload
 (defun native--compile-async (files &optional recursively load selector)
   ;; BEWARE, this function is also called directly from C.
   "Compile FILES asynchronously.
diff --git a/lisp/startup.el b/lisp/startup.el
index 5a389294e78..7f601668369 100644
--- a/lisp/startup.el
+++ b/lisp/startup.el
@@ -520,27 +520,6 @@ startup--xdg-or-homedot
       xdg-dir)
      (t emacs-d-dir))))
 
-(defvar comp--compilable)
-(defvar comp--delayed-sources)
-(defun startup--require-comp-safely ()
-  "Require the native compiler avoiding circular dependencies."
-  (when (featurep 'native-compile)
-    ;; Require comp with `comp--compilable' set to nil to break
-    ;; circularity.
-    (let ((comp--compilable nil))
-      (require 'comp))
-    (native--compile-async comp--delayed-sources nil 'late)
-    (setq comp--delayed-sources nil)))
-
-(declare-function native--compile-async "comp.el"
-                  (files &optional recursively load selector))
-(defun startup--honor-delayed-native-compilations ()
-  "Honor pending delayed deferred native compilations."
-  (when (and (native-comp-available-p)
-             comp--delayed-sources)
-    (startup--require-comp-safely))
-  (setq comp--compilable t))
-
 (defvar native-comp-eln-load-path)
 (defvar native-comp-jit-compilation)
 (defvar native-comp-enable-subr-trampolines)
@@ -846,8 +825,7 @@ normal-top-level
                                 nil)))
             (setq env (cdr env)))))
       (when display
-        (setq process-environment (delete display process-environment)))))
-  (startup--honor-delayed-native-compilations))
+        (setq process-environment (delete display process-environment))))))
 
 ;; Precompute the keyboard equivalents in the menu bar items.
 ;; Command-line options supported by tty's:
diff --git a/src/comp.c b/src/comp.c
index 013ac6358c1..003bec38efa 100644
--- a/src/comp.c
+++ b/src/comp.c
@@ -5199,17 +5199,9 @@ maybe_defer_native_compilation (Lisp_Object function_name,
 
   Fputhash (function_name, definition, Vcomp_deferred_pending_h);
 
-  /* This is so deferred compilation is able to compile comp
-     dependencies breaking circularity.  */
-  if (comp__compilable)
-    {
-      /* Startup is done, comp is usable.  */
-      CALL0I (startup--require-comp-safely);
-      CALLN (Ffuncall, intern_c_string ("native--compile-async"),
-	     src, Qnil, Qlate);
-    }
-  else
-    Vcomp__delayed_sources = Fcons (src, Vcomp__delayed_sources);
+  pending_funcalls
+    = Fcons (list (Qnative__compile_async, src, Qnil, Qlate),
+             pending_funcalls);
 }
 
 
@@ -5798,6 +5790,8 @@ syms_of_comp (void)
         build_pure_c_string ("eln file inconsistent with current runtime "
 			     "configuration, please recompile"));
 
+  DEFSYM (Qnative__compile_async, "native--compile-async");
+  
   defsubr (&Scomp__subr_signature);
   defsubr (&Scomp_el_to_eln_rel_filename);
   defsubr (&Scomp_el_to_eln_filename);