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

From: Michael Heerdegen <[email-masked]>
Date: Thu, 14 Nov 2019 21:27:18 +0100

Stefan Monnier <[email-masked]> writes:

> > +(defun edebug--advised-p (symbol)
> > +  ;; Non-nil when SYMBOL's `symbol-function' is advised.  The non-nil
> > +  ;; return value is the unwrapped base function if it was wrapped,
> > +  ;; and the symbol t else.
>
> Do you really mean "advised"?  It seems that this tests whether the code
> is *instrumented* by looking past the (potential) pieces of advice.

It tests both - sorry, a bit ugly - for the sake of fitting into the
existing code.  As a predicate it tests whether the thing is advised,
and the return value is the unwrapped original function when it can be
unwrapped, or t if it can't.

> > +  (pcase (symbol-function symbol)
> > +    ((and (pred advice--p)
> > +          (app advice--cd*r orig-f)
> > +          (let unwrapped (edebug-unwrap* orig-f)))
> > +     (if (equal unwrapped orig-f) t unwrapped))
> > +    (`(macro . ,(and (pred advice--p)
> > +                     (app advice--cd*r orig-f)
> > +                     (let unwrapped (edebug-unwrap* orig-f))))
> > +     (if (equal unwrapped orig-f) t `(macro . ,unwrapped)))))
>
> [ That's pretty ugly.  I think I'd move the `app` and the `let` outside
>   of those patterns and into the code of the corresponding branch.  ]

Hmm, that's what I had first, and I find this better...I guess nobody
else does, so I'll do what you suggest.

> >    ;; Remove instrumentation.
> >    (dolist (symbol functions)
> > -    (setf (symbol-function symbol)
> > -          (edebug-unwrap* (symbol-function symbol))))
> > +    (if-let ((advised (edebug--advised-p symbol)))
> > +        (unless (eq advised t)
> > +          (funcall (or (get symbol 'defalias-fset-function) #'fset)
> > +                   symbol advised))
> > +      (setf (symbol-function symbol)
> > +            (edebug-unwrap* (symbol-function symbol)))))
>
> Yuck!
>
> Can't we just use `defalias` rather than `fset` (and that should
> take care of calling `defalias-fset-function` when needed)?

That's what I want to know too!  I guess we can, but I thought defalias
would change the source file association as a side effect?

> That's because if I have `foo` as an alias for `bar` and I advise `foo`
> I don't want it to affect `bar`: if you want to affect both, then you
> should advise `bar`.

I hoped it would be like that.  So after advising `foo` the
`symbol-function' of `foo` is no longer a symbol, so there is no need to
call `indirect-function' in my change.  That wasn't clear to me.

Michael.