Subject: bug#60102: Move gv-expander of substring to cl-lib

From: Stefan Monnier <[email-masked]>
Date: Thu, 15 Dec 2022 15:12:06 -0500

Tags: patch

Tags: patch

The `gv-expander` of `substring` uses the `cl--set-substring` function
that's defined only in `cl-lib`, so currently, you can compile

    (setf (substring ...) ...)

without requiring `cl-lib` but at run time it will tend to signal
a `void-function` error.
We could autoload `cl--set-substring`, but I think a better choice is to
move this `gv-expander` to `cl-lib.el`.

There are 2 other place definitions in `gv.el` which similarly rely on
helper functions defined in `cl-lib` (namely `buffer-substring` and
`frame-visible-p`) which we could move as well, but since we marked
those as obsolete anyway I think we can "let them die" where they are,
with their quirks left alone.  If you feel otherwise, I can update the
patch to move them to `cl-lib.el` as well.

        Stefan

 In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnux32, GTK+ Version
 3.24.35, cairo version 1.16.0) of 2022-12-09 built on alfajor
Repository revision: b134e7e6abf18286d38e1b589f0fdae523cf1e73
Repository branch: work
Windowing system distributor 'The X.Org Foundation', version 11.0.12101003
System Description: Debian GNU/Linux bookworm/sid

Configured using:
 'configure -C --enable-checking --enable-check-lisp-object-type --with-modules --with-cairo --with-tiff=ifavailable
 'CFLAGS=-Wall -g3 -Og -Wno-pointer-sign'
 PKG_CONFIG_PATH=/home/monnier/lib/pkgconfig'

diff --git a/lisp/emacs-lisp/cl-lib.el b/lisp/emacs-lisp/cl-lib.el
index b83b44974d3..ceb5c21baf1 100644
--- a/lisp/emacs-lisp/cl-lib.el
+++ b/lisp/emacs-lisp/cl-lib.el
@@ -170,6 +170,17 @@ cl--set-substring
 	  val
 	  (and (< end (length str)) (substring str end))))
 
+(gv-define-expander substring
+  (lambda (do place from &optional to)
+    (gv-letplace (getter setter) place
+      (macroexp-let2* nil ((start from) (end to))
+        (funcall do `(substring ,getter ,start ,end)
+                 (lambda (v)
+                   (macroexp-let2 nil v v
+                     `(progn
+                        ,(funcall setter `(cl--set-substring
+                                           ,getter ,start ,end ,v))
+                        ,v))))))))
 
 ;;; Blocks and exits.
 
diff --git a/lisp/emacs-lisp/gv.el b/lisp/emacs-lisp/gv.el
index 11251d7a963..29cfca9cb62 100644
--- a/lisp/emacs-lisp/gv.el
+++ b/lisp/emacs-lisp/gv.el
@@ -815,17 +815,5 @@ eq
                      ((eq ,getter ,val) ,(funcall setter `(not ,val))))))))))
 (make-obsolete-generalized-variable 'eq nil "29.1")
 
-(gv-define-expander substring
-  (lambda (do place from &optional to)
-    (gv-letplace (getter setter) place
-      (macroexp-let2* nil ((start from) (end to))
-        (funcall do `(substring ,getter ,start ,end)
-                 (lambda (v)
-                   (macroexp-let2 nil v v
-                     `(progn
-                        ,(funcall setter `(cl--set-substring
-                                           ,getter ,start ,end ,v))
-                        ,v))))))))
-
 (provide 'gv)
 ;;; gv.el ends here