Fix potential native compiler circular dependencies during load
authorAndrea Corallo <akrl@sdf.org>
Thu, 27 Jan 2022 17:18:13 +0000 (18:18 +0100)
committerAndrea Corallo <akrl@sdf.org>
Thu, 27 Jan 2022 22:02:31 +0000 (23:02 +0100)
* lisp/startup.el (startup--require-comp-safetly): New function.
(startup--honor-delayed-native-compilations): Make use of
`startup--require-comp-safetly'.
* src/comp.c (CALL0I): New define.
(maybe_defer_native_compilation): Make use of
`startup--require-comp-safetly'.

lisp/startup.el
src/comp.c

index 05d829396d60ffb27fc1d2f651519e18db445acf..856d600e38ccd8b70904d0a01f263a6712ad84a6 100644 (file)
@@ -519,22 +519,26 @@ DIRS are relative."
       xdg-dir)
      (t emacs-d-dir))))
 
-(defvar comp--delayed-sources)
 (defvar comp--loadable)
+(defvar comp--delayed-sources)
+(defun startup--require-comp-safetly ()
+  "Require the native compiler avoiding circular dependencies."
+  (unless (featurep 'comp)
+    ;; Require comp with `comp--loadable' set to nil to break
+    ;; circularity.
+    (let ((comp--loadable 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."
-  (if (and (native-comp-available-p)
-           comp--delayed-sources)
-      (progn
-        ;; Require comp before setting `comp--loadable' to break
-        ;; circularity.
-        (require 'comp)
-        (setq comp--loadable t)
-        (native--compile-async comp--delayed-sources nil 'late)
-        (setq comp--delayed-sources nil))
-    (setq comp--loadable t)))
+  (when (and (native-comp-available-p)
+             comp--delayed-sources)
+    (startup--require-comp-safetly))
+  (setq comp--loadable t))
 
 (defvar native-comp-eln-load-path)
 (defun normal-top-level ()
index d755df802f7ef2acada5205f64e43fcd66e321c5..66288988fd8fe4b7248967eb23159465d2670265 100644 (file)
@@ -480,6 +480,10 @@ load_gccjit_if_necessary (bool mandatory)
 #define THIRD(x)                               \
   XCAR (XCDR (XCDR (x)))
 
+/* Like call0 but stringify and intern.  */
+#define CALL0I(fun)                            \
+  CALLN (Ffuncall, intern_c_string (STR (fun)))
+
 /* Like call1 but stringify and intern.  */
 #define CALL1I(fun, arg)                               \
   CALLN (Ffuncall, intern_c_string (STR (fun)), arg)
@@ -5128,7 +5132,7 @@ maybe_defer_native_compilation (Lisp_Object function_name,
   if (comp__loadable)
     {
       /* Startup is done, comp is usable.  */
-      Frequire (Qcomp, Qnil, Qnil);
+      CALL0I(startup--require-comp-safetly);
       Fputhash (function_name, definition, Vcomp_deferred_pending_h);
       CALLN (Ffuncall, intern_c_string ("native--compile-async"),
             src, Qnil, Qlate);