Subject: bug#38195: 27.0.50; `edebug-remove-instrumentation' doesn't work for adviced functions

From: Michael Heerdegen <[email-masked]>
Date: Thu, 21 Nov 2019 12:49:26 +0100

Stefan Monnier <[email-masked]> writes:

> I don't think you need the `advice--p` here since `advice--cd*r` uses the
> `*` meaning of regexps: "*zero* or more".

Ok, removed.

> > +     ;; `defalias' takes care of any advises so we can just strip them
>
> Actually, you *have* to strip them (otherwise you'd end up copying them).

Sure, I made that comment clearer.

New patch:

From aab2cd47da230993e374d378c434989c98ce68ed Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <[email-masked]>
Date: Thu, 14 Nov 2019 17:47:51 +0100
Subject: [PATCH] Fix edebug instrumentation removing from advised functions

* lisp/emacs-lisp/edebug.el (edebug-remove-instrumentation): Handle
advised functions correctly.
---
 lisp/emacs-lisp/edebug.el | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/lisp/emacs-lisp/edebug.el b/lisp/emacs-lisp/edebug.el
index 5d52704410..d68ed966f8 100644
--- a/lisp/emacs-lisp/edebug.el
+++ b/lisp/emacs-lisp/edebug.el
@@ -4571,6 +4571,21 @@ edebug-unload-function
   ;; Continue standard unloading.
   nil)

+(defun edebug--unwrap*-symbol-function (symbol)
+  ;; Try to unwrap SYMBOL's `symbol-function'.  The result is suitable
+  ;; to be fbound back to SYMBOL with `defalias'.  When no unwrapping
+  ;; could be done return nil.
+  (pcase (symbol-function symbol)
+    ((or (and `(macro . ,f) (let was-macro t))
+         (and  f            (let was-macro nil)))
+     ;; `defalias' takes care of advises so we must strip them
+     (let* ((orig-f (advice--cd*r f))
+            (unwrapped (edebug-unwrap* orig-f)))
+       (cond
+        ((equal unwrapped orig-f) nil)
+        (was-macro               `(macro . ,unwrapped))
+        (t                       unwrapped))))))
+
 (defun edebug-remove-instrumentation (functions)
   "Remove Edebug instrumentation from FUNCTIONS.
 Interactively, the user is prompted for the function to remove
@@ -4582,10 +4597,10 @@ edebug-remove-instrumentation
        (lambda (symbol)
          (when (and (get symbol 'edebug)
                     (or (functionp symbol)
-                        (macrop symbol)))
-           (let ((unwrapped (edebug-unwrap* (symbol-function symbol))))
-             (unless (equal unwrapped (symbol-function symbol))
-               (push symbol functions)))))
+                        (macrop symbol))
+                    (edebug--unwrap*-symbol-function
+                     symbol))
+           (push symbol functions)))
        obarray)
       (unless functions
         (error "Found no functions to remove instrumentation from"))
@@ -4599,8 +4614,9 @@ edebug-remove-instrumentation
           functions)))))
   ;; Remove instrumentation.
   (dolist (symbol functions)
-    (setf (symbol-function symbol)
-          (edebug-unwrap* (symbol-function symbol))))
+    (when-let ((unwrapped
+                (edebug--unwrap*-symbol-function symbol)))
+      (defalias symbol unwrapped)))
   (message "Removed edebug instrumentation from %s"
            (mapconcat #'symbol-name functions ", ")))

--
2.24.0

Ok to install?

BTW, I also wonder if we should enhance the command
`edebug-remove-instrumentation' so that it is able to reload source
files.  It could look at the SYMOL's `symbol-file's, collect these, load
the files, and only do what it does now for the symbols that are still
wrapped.  Could be controlled via prefix argument.

Regards,

Michael.