Subject: Re: bug#21466: [PATCH] Avoid defining (temporarily) vars and functions

From: Lars Ingebrigtsen <[email-masked]>
Date: Wed, 23 Mar 2022 21:13:35 +0100

Alan Mackenzie <[email-masked]> writes:

> So, I don't object on principle to the principle of the patch, just
> there are these little irritations about it, and it will need me to
> study it more closely to check nothing subtle would get lost (I doubt it
> would).

The patch no longer applied to Emacs 29, so I've respun it.

diff --git a/lisp/progmodes/cc-bytecomp.el b/lisp/progmodes/cc-bytecomp.el
index 4b8154dafe..277ba29a89 100644
--- a/lisp/progmodes/cc-bytecomp.el
+++ b/lisp/progmodes/cc-bytecomp.el
@@ -75,12 +75,10 @@
 
 (defvar cc-bytecomp-unbound-variables nil)
 (defvar cc-bytecomp-original-functions nil)
-(defvar cc-bytecomp-original-properties nil)
 (defvar cc-bytecomp-loaded-files nil)
 
 (setq cc-bytecomp-unbound-variables nil)
 (setq cc-bytecomp-original-functions nil)
-(setq cc-bytecomp-original-properties nil)
 (setq cc-bytecomp-loaded-files nil)
 
 (defvar cc-bytecomp-environment-set nil)
@@ -177,16 +175,6 @@ cc-bytecomp-setup-environment
 		  (cc-bytecomp-debug-msg
 		   "cc-bytecomp-setup-environment: Covered function %s" fun))))
 	  (setq p (cdr p)))
-	(setq p cc-bytecomp-original-properties)
-	(while p
-	  (let ((sym (car (car (car p))))
-		(prop (cdr (car (car p))))
-		(tempdef (car (cdr (car p)))))
-	    (put sym prop tempdef)
-	    (cc-bytecomp-debug-msg
-	     "cc-bytecomp-setup-environment: Bound property %s for %s to %s"
-	     prop sym tempdef))
-	  (setq p (cdr p)))
 	(setq cc-bytecomp-environment-set t)
 	(cc-bytecomp-debug-msg
 	 "cc-bytecomp-setup-environment: Done"))))
@@ -236,22 +224,6 @@ cc-bytecomp-restore-environment
 		   "cc-bytecomp-restore-environment: Not restoring function %s"
 		   fun))))
 	  (setq p (cdr p)))
-	(setq p cc-bytecomp-original-properties)
-	(while p
-	  (let ((sym (car (car (car p))))
-		(prop (cdr (car (car p))))
-		(tempdef (car (cdr (car p))))
-		(origdef (cdr (cdr (car p)))))
-	    (if (eq (get sym prop) tempdef)
-		(progn
-		  (put sym prop origdef)
-		  (cc-bytecomp-debug-msg
-		   "cc-bytecomp-restore-environment: Restored property %s for %s to %s"
-		   prop sym origdef))
-	      (cc-bytecomp-debug-msg
-	       "cc-bytecomp-restore-environment: Not restoring property %s for %s"
-	       prop sym)))
-	  (setq p (cdr p)))
 	(setq cc-bytecomp-environment-set nil)
 	(cc-bytecomp-debug-msg
 	 "cc-bytecomp-restore-environment: Done"))))
@@ -385,23 +357,26 @@ cc-bytecomp-defvar
 This can be used to silence the byte compiler.  Don't use within
 `eval-when-compile'."
   (declare (debug nil))
-  `(eval-when-compile
-     (if (boundp ',var)
-	 (cc-bytecomp-debug-msg
-	  "cc-bytecomp-defvar: %s bound already as variable" ',var)
-       (if (not (memq ',var cc-bytecomp-unbound-variables))
-	   (progn
-	     (cc-bytecomp-debug-msg
-	      "cc-bytecomp-defvar: Saving %s (as unbound)" ',var)
-	     (setq cc-bytecomp-unbound-variables
-		   (cons ',var cc-bytecomp-unbound-variables))))
-       (if (cc-bytecomp-is-compiling)
-	   (progn
-	     (defvar ,var)
-	     (set ',var (intern (concat "cc-bytecomp-ignore-var:"
-					(symbol-name ',var))))
-	     (cc-bytecomp-debug-msg
-	      "cc-bytecomp-defvar: Covered variable %s" ',var))))))
+  (if (not (featurep 'xemacs))
+      `(defvar ,var)
+    ;; Not sure if XEmacs's `defvar' works in the same way.
+    `(eval-when-compile
+       (if (boundp ',var)
+	   (cc-bytecomp-debug-msg
+	    "cc-bytecomp-defvar: %s bound already as variable" ',var)
+	 (if (not (memq ',var cc-bytecomp-unbound-variables))
+	     (progn
+	       (cc-bytecomp-debug-msg
+		"cc-bytecomp-defvar: Saving %s (as unbound)" ',var)
+	       (setq cc-bytecomp-unbound-variables
+		     (cons ',var cc-bytecomp-unbound-variables))))
+	 (if (cc-bytecomp-is-compiling)
+	     (progn
+	       (defvar ,var)
+	       (set ',var (intern (concat "cc-bytecomp-ignore-var:"
+					  (symbol-name ',var))))
+	       (cc-bytecomp-debug-msg
+		"cc-bytecomp-defvar: Covered variable %s" ',var)))))))
 
 (defmacro cc-bytecomp-defun (fun)
   "Bind the symbol FUN as a function during compilation of the file.
@@ -414,42 +389,25 @@ cc-bytecomp-defun
 existing functions since the byte compiler might need the definition
 at compile time, e.g. for macros and inline functions."
   (declare (debug nil))
-  `(eval-when-compile
-     (if (fboundp ',fun)
-	 (cc-bytecomp-debug-msg
-	  "cc-bytecomp-defun: %s bound already as function" ',fun)
-       (if (not (assq ',fun cc-bytecomp-original-functions))
-	   (progn
-	     (cc-bytecomp-debug-msg
-	      "cc-bytecomp-defun: Saving %s (as unbound)" ',fun)
-	     (setq cc-bytecomp-original-functions
-		   (cons (list ',fun nil 'unbound)
-			 cc-bytecomp-original-functions))))
-       (if (cc-bytecomp-is-compiling)
-	   (progn
-	     (fset ',fun (intern (concat "cc-bytecomp-ignore-fun:"
-					 (symbol-name ',fun))))
-	     (cc-bytecomp-debug-msg
-	      "cc-bytecomp-defun: Covered function %s" ',fun))))))
-
-(defmacro cc-bytecomp-put (symbol propname value)
-  "Set a property on SYMBOL during compilation (and evaluation) of the file.
-Don't use outside `eval-when-compile'."
-  (declare (debug t))
-  `(eval-when-compile
-     (if (not (assoc (cons ,symbol ,propname) cc-bytecomp-original-properties))
-	 (progn
+  (if (fboundp 'declare-function)
+      `(declare-function ,fun nil)
+    `(eval-when-compile
+       (if (fboundp ',fun)
 	   (cc-bytecomp-debug-msg
-	    "cc-bytecomp-put: Saving property %s for %s with value %s"
-	    ,propname ,symbol (get ,symbol ,propname))
-	   (setq cc-bytecomp-original-properties
-		 (cons (cons (cons ,symbol ,propname)
-			     (cons ,value (get ,symbol ,propname)))
-		       cc-bytecomp-original-properties))))
-     (put ,symbol ,propname ,value)
-     (cc-bytecomp-debug-msg
-      "cc-bytecomp-put: Bound property %s for %s to %s"
-      ,propname ,symbol ,value)))
+	    "cc-bytecomp-defun: %s bound already as function" ',fun)
+	 (if (not (assq ',fun cc-bytecomp-original-functions))
+	     (progn
+	       (cc-bytecomp-debug-msg
+		"cc-bytecomp-defun: Saving %s (as unbound)" ',fun)
+	       (setq cc-bytecomp-original-functions
+		     (cons (list ',fun nil 'unbound)
+			   cc-bytecomp-original-functions))))
+	 (if (cc-bytecomp-is-compiling)
+	     (progn
+	       (fset ',fun (intern (concat "cc-bytecomp-ignore-fun:"
+					   (symbol-name ',fun))))
+	       (cc-bytecomp-debug-msg
+		"cc-bytecomp-defun: Covered function %s" ',fun)))))))
 
 (defmacro cc-bytecomp-boundp (symbol)
   "Return non-nil if SYMBOL is bound as a variable outside the compilation.
@@ -457,7 +415,8 @@ cc-bytecomp-boundp
 variables that have been bound during compilation with
 `cc-bytecomp-defvar'."
   (declare (debug t))
-  (if (and (cc-bytecomp-is-compiling)
+  (if (and (featurep 'xemacs)
+	   (cc-bytecomp-is-compiling)
 	   (memq (car (cdr symbol)) cc-bytecomp-unbound-variables))
       nil
     `(boundp ,symbol)))
@@ -469,7 +428,8 @@ cc-bytecomp-fboundp
 `cc-bytecomp-defun'."
   (declare (debug t))
   (let (fun-elem)
-    (if (and (cc-bytecomp-is-compiling)
+    (if (and (not (fboundp 'declare-function))
+	     (cc-bytecomp-is-compiling)
 	     (setq fun-elem (assq (car (cdr symbol))
 				  cc-bytecomp-original-functions))
 	     (eq (elt fun-elem 2) 'unbound))

However, it leads to a number of warnings when compiling, so either my
respin is faulty, or something's changed to make it not work any more?

In c-fontify-recorded-types-and-refs:
progmodes/cc-fonts.el:491:8: Warning: function `c-fontify-recorded-types-and-refs' defined multiple times in this file
  ELC      progmodes/cc-styles.elc
In c-font-lock-declarators:
progmodes/cc-fonts.el:1063:8: Warning: function `c-font-lock-declarators' defined multiple times in this file
progmodes/cc-fonts.el:2300:35: Warning: reference to free variable `c-preprocessor-face-name'
progmodes/cc-fonts.el:2300:35: Warning: reference to free variable `c-preprocessor-face-name'
progmodes/cc-fonts.el:2496:37: Warning: reference to free variable `c-preprocessor-face-name'
progmodes/cc-fonts.el:2496:37: Warning: reference to free variable `c-preprocessor-face-name'
progmodes/cc-fonts.el:2501:37: Warning: reference to free variable `c-reference-face-name'
In c-font-lock-objc-method:
progmodes/cc-fonts.el:2527:8: Warning: function `c-font-lock-objc-method' defined multiple times in this file
progmodes/cc-fonts.el:2602:38: Warning: reference to free variable `c-preprocessor-face-name'
progmodes/cc-fonts.el:2602:38: Warning: reference to free variable `c-preprocessor-face-name'
progmodes/cc-fonts.el:2650:38: Warning: reference to free variable `c-reference-face-name'
progmodes/cc-fonts.el:2678:37: Warning: reference to free variable `c-preprocessor-face-name'
progmodes/cc-fonts.el:2678:37: Warning: reference to free variable `c-preprocessor-face-name'
progmodes/cc-fonts.el:2683:37: Warning: reference to free variable `c-reference-face-name'
progmodes/cc-fonts.el:2711:38: Warning: reference to free variable `c-preprocessor-face-name'
progmodes/cc-fonts.el:2711:38: Warning: reference to free variable `c-preprocessor-face-name'
progmodes/cc-fonts.el:2716:38: Warning: reference to free variable `c-reference-face-name'