Subject: Re: bug#39546: 28.0.50; Do not require subr-x at run time

From: Tino Calancha <[email-masked]>
Date: Mon, 10 Feb 2020 21:34:46 +0100

"Tassilo Horn" <[email-masked]> writes:

> Am Mo, 10. Feb 2020, um 20:30, schrieb Tino Calancha:
>> replace.el is a good agreement between subr-x.el (it's a defun, so 
>> it doesn't fit well there) and subr.el.
>> 
>> As subr.el, replace.el is also loaded at start time, but it's ~ half size,
>> and more important than that, it's all about replacing in Emacs.
>
> Sounds good to me!

Here is the updated patch:
- It deletes the cookie from subr-x (yes, we have one for when-let)
- Move replace-region-contents inside replace.el (loaded at startup)
- Add all required (eval-when...(req.. 'subr-x)) at tramp files
- Replaced loading subr-x at run time with compile time for some other files

I have run the testsuite without surprises.

--8<-----------------------------cut here---------------start------------->8---
commit f6a7a4fc673f77724ccf0d7ae1fdb164328afc68
Author: Constantino Calancha <[email-masked]>
Date:   Mon Feb 10 21:19:29 2020 +0100

    Do not require subr-x at run time
    
    Move back `replace-region-contents' to subr.el according
    with the convention for subr-x (Bug#39546).
    
    * lisp/subr-x.el (replace-region-contents): Delete it.
      (when-let): Delete autoload cookie.
    
    * calendar/time-date.el
    * net/net-utils.el
    * net/nsm.el
    * net/tramp-compat.el
    * lisp/net/mailcap.el
    * lisp/net/tramp-adb.el
    * lisp/net/tramp-cmds.el
    * lisp/net/tramp-gvfs.el
    * lisp/net/tramp-rclone.el
    * lisp/net/tramp-sh.el
    * lisp/net/tramp-smb.el
    * lisp/net/tramp-sudoedit.el
    * lisp/net/tramp.el:
    Require subr-x at compile time.
    
    * lisp/replace.el (replace-region-contents): Move it from subr-x.el.
    
    * lisp/emacs-lisp/json.el:  Do not require subr-x since now
    `replace-region-contents' is loaded at start up.

diff --git a/lisp/calendar/time-date.el b/lisp/calendar/time-date.el
index e2402de801..b4b13037bf 100644
--- a/lisp/calendar/time-date.el
+++ b/lisp/calendar/time-date.el
@@ -37,7 +37,7 @@
 ;;; Code:
 
 (require 'cl-lib)
-(require 'subr-x)
+(eval-when-compile (require 'subr-x))
 
 (defmacro with-decoded-time-value (varlist &rest body)
   "Decode a time value and bind it according to VARLIST, then eval BODY.
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 044c9aada0..2c1415ab2a 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -182,7 +182,6 @@ if-let
     (setq spec (list spec)))
   (list 'if-let* spec then (macroexp-progn else)))
 
-;;;###autoload
 (defmacro when-let (spec &rest body)
   "Bind variables according to SPEC and conditionally evaluate BODY.
 Evaluate each binding in turn, stopping if a binding value is nil.
@@ -254,34 +253,6 @@ string-remove-suffix
       (substring string 0 (- (length string) (length suffix)))
     string))
 
-(defun replace-region-contents (beg end replace-fn
-                                    &optional max-secs max-costs)
-  "Replace the region between BEG and END using REPLACE-FN.
-REPLACE-FN runs on the current buffer narrowed to the region.  It
-should return either a string or a buffer replacing the region.
-
-The replacement is performed using `replace-buffer-contents'
-which also describes the MAX-SECS and MAX-COSTS arguments and the
-return value.
-
-Note: If the replacement is a string, it'll be placed in a
-temporary buffer so that `replace-buffer-contents' can operate on
-it.  Therefore, if you already have the replacement in a buffer,
-it makes no sense to convert it to a string using
-`buffer-substring' or similar."
-  (save-excursion
-    (save-restriction
-      (narrow-to-region beg end)
-      (goto-char (point-min))
-      (let ((repl (funcall replace-fn)))
-	(if (bufferp repl)
-	    (replace-buffer-contents repl max-secs max-costs)
-	  (let ((source-buffer (current-buffer)))
-	    (with-temp-buffer
-	      (insert repl)
-	      (let ((tmp-buffer (current-buffer)))
-		(set-buffer source-buffer)
-		(replace-buffer-contents tmp-buffer max-secs max-costs)))))))))
 
 (provide 'subr-x)
 
diff --git a/lisp/json.el b/lisp/json.el
index 18d7fda882..e31928e825 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -55,7 +55,6 @@
 ;;; Code:
 
 (require 'map)
-(require 'subr-x)
 
 ;; Parameters
 
diff --git a/lisp/net/mailcap.el b/lisp/net/mailcap.el
index 5fe5b4d3a5..75c91946e4 100644
--- a/lisp/net/mailcap.el
+++ b/lisp/net/mailcap.el
@@ -29,6 +29,7 @@
 
 ;;; Code:
 
+(eval-when-compile (require 'subr-x))
 (autoload 'mail-header-parse-content-type "mail-parse")
 
 (defgroup mailcap nil
diff --git a/lisp/net/net-utils.el b/lisp/net/net-utils.el
index ef3651b033..780f34b028 100644
--- a/lisp/net/net-utils.el
+++ b/lisp/net/net-utils.el
@@ -44,7 +44,7 @@
 ;; days is for /sbin to be a symlink to /usr/sbin, but we still need to
 ;; search both for older systems.
 
-(require 'subr-x)
+(eval-when-compile (require 'subr-x))
 (require 'cl-lib)
 
 (defun net-utils--executable-find-sbin (command)
diff --git a/lisp/net/nsm.el b/lisp/net/nsm.el
index 2d36c5e257..f85529f726 100644
--- a/lisp/net/nsm.el
+++ b/lisp/net/nsm.el
@@ -26,7 +26,7 @@
 
 (require 'cl-lib)
 (require 'rmc)                       ; read-multiple-choice
-(require 'subr-x)
+(eval-when-compile (require 'subr-x))
 (require 'seq)
 (require 'map)
 
diff --git a/lisp/net/tramp-adb.el b/lisp/net/tramp-adb.el
index aa7fe147c2..60c46f1224 100644
--- a/lisp/net/tramp-adb.el
+++ b/lisp/net/tramp-adb.el
@@ -33,6 +33,7 @@
 
 ;;; Code:
 
+(eval-when-compile (require 'subr-x))
 (require 'tramp)
 
 ;;;###tramp-autoload
diff --git a/lisp/net/tramp-cmds.el b/lisp/net/tramp-cmds.el
index b4dca2321c..871c215e87 100644
--- a/lisp/net/tramp-cmds.el
+++ b/lisp/net/tramp-cmds.el
@@ -29,6 +29,7 @@
 ;;; Code:
 
 (require 'tramp)
+(eval-when-compile (require 'subr-x))
 
 ;; Pacify byte-compiler.
 (declare-function mml-mode "mml")
diff --git a/lisp/net/tramp-compat.el b/lisp/net/tramp-compat.el
index 87bcd08b85..8f74683dee 100644
--- a/lisp/net/tramp-compat.el
+++ b/lisp/net/tramp-compat.el
@@ -39,7 +39,7 @@ tramp-unload-file-name-handlers
 (require 'ls-lisp)  ;; Due to `tramp-handle-insert-directory'.
 (require 'parse-time)
 (require 'shell)
-(require 'subr-x)
+(eval-when-compile (require 'subr-x))
 
 ;; `temporary-file-directory' as function is introduced with Emacs 26.1.
 (declare-function tramp-handle-temporary-file-directory "tramp")
diff --git a/lisp/net/tramp-gvfs.el b/lisp/net/tramp-gvfs.el
index 0d800cb42b..5b037fb94b 100644
--- a/lisp/net/tramp-gvfs.el
+++ b/lisp/net/tramp-gvfs.el
@@ -102,6 +102,7 @@
 (declare-function dbus-get-unique-name "dbusbind.c")
 
 (eval-when-compile (require 'cl-lib))
+(eval-when-compile (require 'subr-x))
 (require 'tramp)
 (require 'dbus)
 (require 'url-parse)
diff --git a/lisp/net/tramp-rclone.el b/lisp/net/tramp-rclone.el
index 445098a5bc..b7c99829ed 100644
--- a/lisp/net/tramp-rclone.el
+++ b/lisp/net/tramp-rclone.el
@@ -36,6 +36,7 @@
 ;;; Code:
 
 (eval-when-compile (require 'cl-lib))
+(eval-when-compile (require 'subr-x))
 (require 'tramp)
 
 ;;;###tramp-autoload
diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el
index 5a3abc31ea..0c5bc72deb 100644
--- a/lisp/net/tramp-sh.el
+++ b/lisp/net/tramp-sh.el
@@ -32,6 +32,7 @@
 ;;; Code:
 
 (eval-when-compile (require 'cl-lib))
+(eval-when-compile (require 'subr-x))
 (require 'tramp)
 
 (declare-function dired-remove-file "dired-aux")
diff --git a/lisp/net/tramp-smb.el b/lisp/net/tramp-smb.el
index f02be394a7..43e775b921 100644
--- a/lisp/net/tramp-smb.el
+++ b/lisp/net/tramp-smb.el
@@ -28,6 +28,7 @@
 ;;; Code:
 
 (eval-when-compile (require 'cl-lib))
+(eval-when-compile (require 'subr-x))
 (require 'tramp)
 
 ;; Define SMB method ...
diff --git a/lisp/net/tramp-sudoedit.el b/lisp/net/tramp-sudoedit.el
index f258ad6b93..6ea52bedf8 100644
--- a/lisp/net/tramp-sudoedit.el
+++ b/lisp/net/tramp-sudoedit.el
@@ -34,6 +34,7 @@
 
 ;;; Code:
 
+(eval-when-compile (require 'subr-x))
 (require 'tramp)
 
 ;;;###tramp-autoload
diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index 19d36c3a97..0783eda09e 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -60,6 +60,7 @@
 (require 'tramp-compat)
 (require 'tramp-integration)
 (require 'trampver)
+(eval-when-compile (require 'subr-x))
 
 ;; Pacify byte-compiler.
 (require 'cl-lib)
diff --git a/lisp/replace.el b/lisp/replace.el
index a0b050637e..74c54a0b80 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -743,6 +743,35 @@ replace-regexp
 	   (if (use-region-p) (region-noncontiguous-p)))))
   (perform-replace regexp to-string nil t delimited nil nil start end backward region-noncontiguous-p))
 
+(defun replace-region-contents (beg end replace-fn
+                                    &optional max-secs max-costs)
+  "Replace the region between BEG and END using REPLACE-FN.
+REPLACE-FN runs on the current buffer narrowed to the region.  It
+should return either a string or a buffer replacing the region.
+
+The replacement is performed using `replace-buffer-contents'
+which also describes the MAX-SECS and MAX-COSTS arguments and the
+return value.
+
+Note: If the replacement is a string, it'll be placed in a
+temporary buffer so that `replace-buffer-contents' can operate on
+it.  Therefore, if you already have the replacement in a buffer,
+it makes no sense to convert it to a string using
+`buffer-substring' or similar."
+  (save-excursion
+    (save-restriction
+      (narrow-to-region beg end)
+      (goto-char (point-min))
+      (let ((repl (funcall replace-fn)))
+	(if (bufferp repl)
+	    (replace-buffer-contents repl max-secs max-costs)
+	  (let ((source-buffer (current-buffer)))
+	    (with-temp-buffer
+	      (insert repl)
+	      (let ((tmp-buffer (current-buffer)))
+		(set-buffer source-buffer)
+		(replace-buffer-contents tmp-buffer max-secs max-costs)))))))))
+
 
 (defvar regexp-history nil
   "History list for some commands that read regular expressions.

--8<-----------------------------cut here---------------end--------------->8---